Magento2.1.5 How can i add multipul custom fields on customer registration form(admin panel) - magento-2.0

I have added the single field in the customer registration form in admin end.
But i want to add the one more (multipul fields ).This is my code .
$customerSetup->addAttribute(Customer::ENTITY, 'Company_name', [
'type' => 'varchar',
'label' => 'Company Name tst3',
'input' => 'text',
'required' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'Company_name')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
'used_in_forms' => ['customer_account_create'],
]);
This is the code for addded single fields and it is working fine .
Now I am trying the code for add one more field.
$customerSetup->addAttribute(Customer::ENTITY, 'Company_name', [
'type' => 'varchar',
'label' => 'Company Name tst3',
'input' => 'text',
'required' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
],'Admin_email', [
'type' => 'varchar',
'label' => 'Admin Email',
'input' => 'input',
'required' => true,
'sort_order' => 1000,
'position' => 1000,
]);
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'Company_name','Admin_email')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer'],
'used_in_forms' => ['customer_account_create'],
]);

you can go at it by putting the desired attributes in an array like so
$attributesInfo = [
'attribut1' => [
'type' => 'varchar',
'label' => 'Company Name tst3',
'input' => 'text',
'required' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
],
'attribut2' => [
'type' => 'varchar',
'label' => 'Admin Email',
'input' => 'text',
'required' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
],
];
foreach ($attributesInfo as $attributeCode => $attributeParams) {
$customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
}
and then the other part involving eav
$companyAttribute= $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute1');
$companyAttribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['handle1','handle2'],
]);
$adminEmailAttribute= $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute2');
$adminEmailAttribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['handle1','handle2'],
]);
here's a decent tutorial (2.1 or higher)
http://www.extensions.sashas.org/blog/magento-2-1-3-how-to-make-customer-attribute-update.html
should've asked in magento.stackexchange

Related

According to the payment method, display the information that needs to be filled in

I have a different payment methods,
How can, according to the payment method, display the information that needs to be filled in on the Checkout Page?
For Example, when choose Credit Card, will show Input box in Billing area
add_filter( 'woocommerce_checkout_fields', "custom_override_checkout_fieldss");
function custom_override_checkout_fieldss( $fields ) {
$fields['billing']['card_type'] = [
'type' => 'text',
'label' => 'Card Numner',
'required' => true,
'placeholder' => _x('Card Numner', 'placeholder', 'woocommerce'),
'priority' => -3,
];
$fields['billing']['card_type_code'] = [
'type' => 'text',
'label' => 'code',
'required' => true,
'placeholder' => _x('CVV2', 'placeholder', 'woocommerce'),
'priority' => -2,
];
$fields['billing']['card_type_num'] = [
'type' => 'text',
'label' => 'date',
'required' => true,
'placeholder' => _x('expired date', 'placeholder', 'woocommerce'),
'priority' => -1,
];
return $fields;
};

Wordpress ACF add row to flexible content programmatically

I want to add a row to my posts flexible content field.
I create my ACF fields using the code from the export tool, this is a part of it:
acf_add_local_field_group(array(
'key' => 'group_5c4c39a08349a',
'title' => 'Posts page',
'fields' => array(
array(
'key' => 'field_5c4c39aab1ed4',
'label' => 'Blokken positie',
'name' => 'block_position',
'type' => 'radio',
'instructions' => 'Geef aan waar de pagina blokken geplaatst moeten worden',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
0 => 'Boven de berichten',
1 => 'Onder de berichten',
),
'allow_null' => 0,
'other_choice' => 0,
'default_value' => 1,
'layout' => 'vertical',
'return_format' => 'value',
'save_other_choice' => 0,
),
),
'location' => array(
array(
array(
'param' => 'page_type',
'operator' => '==',
'value' => 'posts_page',
),
),
),
'menu_order' => 6,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
acf_add_local_field_group(array(
'key' => 'group_5c4a061a6be68',
'title' => 'Page/ Post blocks',
'fields' => array(
array(
'key' => 'field_5c4a0645c3baa',
'label' => 'Blocks',
'name' => 'blocks',
'type' => 'flexible_content',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'layouts' => array(
'layout_5c4a084c7953b' => array(
'key' => 'layout_5c4a084c7953b',
'name' => 'news_slider',
'label' => 'Nieuws slider',
'display' => 'block',
'sub_fields' => array(
array(
'key' => 'field_5c4a085f7953e',
'label' => 'Type',
'name' => 'type',
'type' => 'select',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
0 => 'Laatste berichten',
1 => 'Berichten uit een categorie',
2 => 'Zelf de berichten selecteren',
),
'default_value' => array(
),
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'return_format' => 'value',
'ajax' => 0,
'placeholder' => '',
),
array(
'key' => 'field_5c4ac2d1986b9',
'label' => 'Aantal nieuws artikelen',
'name' => 'news_items',
'type' => 'number',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_5c4a085f7953e',
'operator' => '==',
'value' => '0',
),
),
array(
array(
'field' => 'field_5c4a085f7953e',
'operator' => '==',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => 9,
'placeholder' => '',
'prepend' => '',
'append' => '',
'min' => 3,
'max' => 18,
'step' => 3,
),
array(
'key' => 'field_5c4a093b2ba54',
'label' => 'Toon nieuws uit categorie',
'name' => 'news_category',
'type' => 'taxonomy',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_5c4a085f7953e',
'operator' => '==',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'taxonomy' => 'category',
'field_type' => 'select',
'allow_null' => 0,
'add_term' => 1,
'save_terms' => 0,
'load_terms' => 0,
'return_format' => 'object',
'multiple' => 0,
),
array(
'key' => 'field_5c4a09e7ee82f',
'label' => 'Selecteer nieuws berichten',
'name' => 'news_item_collection',
'type' => 'repeater',
'instructions' => 'De slider laat 3 items per slide zien. Zorg dat het aantal items deelbaar is door 3.',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_5c4a085f7953e',
'operator' => '==',
'value' => '2',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => '',
'sub_fields' => array(
array(
'key' => 'field_5c4a0a08ee830',
'label' => 'Nieuws item',
'name' => 'news_item',
'type' => 'post_object',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array(
0 => 'post',
),
'taxonomy' => '',
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'object',
'ui' => 1,
),
),
),
),
'min' => '',
'max' => '',
),
'layout_5c4a065576f71' => array(
'key' => '5c4a065576f71',
'name' => 'text',
'label' => 'Tekst (1 blok per rij)',
'display' => 'block',
'sub_fields' => array(
array(
'key' => 'field_5c4a0666c3bab',
'label' => 'Tekst',
'name' => 'text_content',
'type' => 'wysiwyg',
'instructions' => 'Het wordt afgeraden om in deze editor afbeeldingen in te voegen. Gebruik bij voorkeur het block "Afbeelding" hiervoor.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'tabs' => 'visual',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 0,
),
),
'min' => '',
'max' => '',
),
When I create or edit a post I can see and use these fields.
Now I have a script to import posts and I want to add the field Tekst (1 blok per rij) as a new row programmatically.
I've tried using add_sub_row and update_field but none worked properly.
When I use add_sub_row like this I always get a return value of false:
add_sub_row([ 'field_5c4a0645c3baa', 1, 'field_5c4a0666c3bab'], [
'field_5c4a0666c3bab' => 'test'
], $post)
When I use update_field I do get a response which makes it look like the field has been added, but I can't see it when I go to wp-admin and open the post:
update_field('field_5c4a0645c3baa', [
'acf_fc_layout' => 'text',
'text_content' => 'test'
], $post)
How should I use these methods? Because I've tried to use the manual of ACF for it but I don't really understand how to put it to use on flexible content.
I've contacted the ACF support.
They told me this:
Thanks for reaching out to us.
Unfortunately, I'm afraid it's not possible to add using the add_row
function with the flexible content field.
However, you could implement a custom solution using the update_field
function to add sub_rows to the flexible content field.
The developer already has it in his todo and probably this option will
be available in future updates.
Apologies for the inconvenience.
Later they replied with this:
Thanks for the follow up.
Ideally, you need to load the field, modify the field values and
update the field using update_field.
Kindly have a look at the following article from the support forums.
https://support.advancedcustomfields.com/forums/topic/issues-with-add_rows-and-flexible-content/
Hope this helps.
The link helped me to create a new row in the flexible content.
Using this code:
$row_i = add_row(
'field_55ce0e107c687',
[
'acf_fc_layout' => 'headline',
'field_55df18296e4b3' => 'headline text'
],
$ID
);
So I had to adjust it to my field names:
add_row(
'field_5c4a0645c3baa', [
'acf_fc_layout' => 'text',
'field_5c4a0666c3bab' => $block[ 'value' ],
], $post);
Now it works fine.

Allow spaces when using Select2 tags symfony

I try to create tags if not exist by using select2 entity. When I try to add a tag that contains spaces I can't.
For example :
"Yupi yola" --> not work because tag contains spaces, is getting only first word "Yupi".
"Test" ---> work
"Test_test" ---> work
I added the Select2Entity in my form type
->add('groupe', Select2EntityType::class, [
'remote_route' => 'route',
'class' => Class::class,
'primary_key' => 'id',
'text_property' => 'name',
'minimum_input_length' => 2,
'page_limit' => 10,
'allow_clear' => true,
'delay' => 250,
'cache' => true,
'cache_timeout' => 60000, // if 'cache' is true
'language' => 'en',
'placeholder' => 'Select a group',
'allow_add' => array(
'enabled' => true,
'new_tag_text' => '',
'tokenSeparators'=> '[",", " "]'
),
'multiple' => false,
'attr' => ['style' => 'width:100%'],
'scroll' => true,
])
Please help me. Thx in advance.
Instead of 'tokenSeparators', use 'tag_separators'
'allow_add' => array(
'enabled' => true,
'new_tag_text' => '',
'new_tag_prefix' => '__',
'tag_separators'=> '[",", " "]'
)

How to add custom fields in ACF programmatically?

I want to programmatically add a tab with repeater inside but I can't seem to find a solution, I've googled all available resources but still not working.
I already tried using acf_add_local_field_group and acf_add_local_field but still no luck.
Well I can create a tab using acf_add_local_field but when I tried to add a child which in this case a repeater OR even a text field it still doesn't work.
Here's my code to create a tab and its child but the child doesn't work.
acf_add_local_field(array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => '',
'parent' => 'field_5bd14c9349930',
'fields' => array (
array(
'key' => 'field_2',
'label' => 'This is a test',
'name' => 'my_test',
'type' => 'text',
)
)
));
You should use acf_add_local_field_group to construct the whole field group.
Here's the proper code for adding a group and a custom tab with single repeater field inside:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_1',
'title' => 'My Group',
'fields' => array (
array (
'key' => 'field_unique_key',
'label' => 'First Tab',
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'placement' => 'top',
'endpoint' => 0,
),
array (
'key' => 'field_unique_key',
'label' => 'Simple Repeater',
'name' => 'simple_repeater',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 10,
'layout' => 'table',
'button_label' => 'Add row',
'sub_fields' => array ( // Here you can add as many subfields for this repeater as you want
array (
'key' => 'field_unique_key',
'label' => 'Link',
'name' => 'link',
'type' => 'link', // example link type
'instructions' => 'Link name and URL',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'array',
),
),
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
));
endif;

create custom content type with custom table fields in drupal 7

I want to create a module in which, when I install that module, it should create custom table defined in homepage_blocks_schema(), create content-type defined in homepage_blocks_install() and in that content type, create custom fields same as my schema.
Here is function,
function homepage_blocks_schema() {
$schema['homepage_blocks'] = array(
'fields' => array(
'hid' => array(
'type' => 'int',
'length' => 11,
'unsigned' => TRUE,
'not null' => TRUE,
),
'title' => array(
'type' => 'varchar',
'length' => 255,
),
'image' => array(
'type' => 'varchar',
'length' => 255,
),
),
'primary key' => array('hid'),
);
return $schema;
}
homepage_blocks_install() {
$homepage_blocks = array(
'type' => 'homepage_blocks',
'name' => $t('Homepage Blocks'),
//'base' => 'node_content',
'base' => 'homepage_blocks',
'custom' => true,
'modified' => true,
'locked' => false,
'title_label' => $t('Homepage Blocks')
);
$content_type = node_type_set_defaults($homepage_blocks);
//node_add_body_field($content_type, 'Body'); // add the body field to the content type
node_type_save($content_type); // create the content type
variable_set('node_options_homepage_blocks', array('status'));
variable_set('comment_homepage_blocks', 'COMMENT_NODE_HIDDEN'); // hide comments for this node.
variable_set('node_submitted_homepage_blocks', false); // Hide date and author information
//drupal_install_schema('homepage_blocks');
foreach (_homepage_blocks_installed_fields() as $field) { // Create all the fields we are adding to our content type.
//krumo(field_create_field($field));
field_create_field($field);
}
foreach (_homepage_blocks_installed_instances() as $instance) { // Create all the instances for our fields.
$instance['entity_type'] = 'node';
$instance['bundle'] = 'homepage_blocks';
field_create_instance($instance);
}
}
function _homepage_blocks_installed_fields() {
$t = get_t();
$fields = array(
'hmblock_title' => array(
'field_name' => 'hmblock_title',
'label' => $t('Title'),
//'cardinality' => 1,
'type' => 'text',
'settings' => array(
'max_length' => 255,
),
),
'hmblock_image' => array(
'field_name' => 'hmblock_image',
'label' => $t('Image'),
//'cardinality' => 1,
'type' => 'image',
'settings' => array(
'default_image' => 0,
'uri_scheme' => 'public',
),
),
);
//$fields = drupal_get_schema('homepage_blocks');
return $fields;
}
function _homepage_blocks_installed_instances() {
$t = get_t();
$instances = array(
'hmblock_title' => array(
'field_name' => 'hmblock_title',
'label' => $t('Title'),
'cardinality' => 1,
'widget' => array(
'type' => 'text_textfield',
'settings' => array('size' => 255),
),
),
'hmblock_image' => array(
'field_name' => 'hmblock_image',
'label' => $t('Image'),
'cardinality' => 1,
'type' => 'image',
'settings' => array(
'alt_field' => 1,
'file_directory' => 'image',
'file_extensions' => 'png gif jpg jpeg',
'max_filesize' => '50mb',
'max_resolution' => '',
'min_resolution' => '',
'title_field' => 1,
'user_register_form' => FALSE,
),
'widget' => array(
'settings' => array(
'preview_image_style' => 'thumbnail',
'progress_indicator' => 'throbber',
),
)
),
);
return $instances;
}
This code creates table and content type but not its fields..
Can anyone help ?
There is an easier way
you can create your custom type using drupal core and fields module and then export it into a module using Features module.
You can find a full guide here : Bundling site settings using Features

Resources