PHP adding count to function - php

I am building a new site in woo/ WordPress and a little stuck, basically the client wants to be able to add custom fields to the checkout (done) but they need to be in a repeatable field so the customer can add the field as many times as they need (done) but I am stuck on the actual integration into the database.
My code is at http://pastebin.com/vR4GWV7w
Basically I need to add a custom ID to each repeatable field input e.g.
child_name1
child_name2
If I was doing this in a template file I would use PHP count but this needs to work in the code below which creates the field.
function my_custom_checkout_field( $checkout ) {
echo '<div id="entry1" class="clonedInput"><h2>' . __('Child Details') . '</h2>';
woocommerce_form_field( 'child_name', array(
'type' => 'text',
'class' => array('child_field form-row-wide'),
'label' => __('Child Name'),
'placeholder' => __('Enter the child\'s name'),
), $checkout->get_value( 'child_name' ));
}
Any help / guidance would be much appreciated, I posted this on the WordPress exchange and got put on hold so trying here as it's a PHP question

Related

Update user repeater meta (ACF) with another user meta field value

My goal is to get a signed-in user to select a color( via front end form and saved in ACF user meta field group) that will be applied to another user meta field inside a repeater. The field must be the same for each row inside the repeater ( for front-end design reasons ). I am using ACF pro, and a plugin called ACF Front end admin (let's call it FEA from now on) for the front-end form. I'm pretty new to PHP and have referenced ACF & FEA's s documentation, which can be found below, spin up a custom function. I am currently running this through the code snippets plugin if this helps. I've run an error log and nothing related to this shows. Tried running this through wp-config and it crashes my site.
Front end admin documentation
ACF documentation - update sub field
ACF documentation - getting values from a user
Edit, Additional information:
ACF Field composition of 'button color' (color of the buttons that user wants displayed in front end, and saved as user meta):
acf_add_local_field_group(array(
'key' => 'group_60000aaa00000',
'title' => 'Color Selector',
'fields' => array(
array(
'key' => 'field_60000aaa00000',
'label' => 'Button color',
'name' => 'button_color',
'type' => 'color_picker',
Button color in repeater field that needs to updated based on the above mentioned 'button_color' meta value:
acf_add_local_field_group(array(
'key' => 'group_70000bbb00000',
'title' => 'front end user profile',
'fields' => array(
array(
'key' => 'field_70000bbb00000',
'label' => 'Quick Link selector',
'name' => 'quick_link_selector',
'type' => 'repeater',
),
array(
'key' => 'field_70000ccc00000',
'label' => 'repeater button color',
'name' => 'repeater_button_color',
'type' => 'text',
The title of the forum is: "QL color selector form" (created through the FEA plugin) and has a short code of [frontend_admin form="3030"] if this helps.
I'm running the following code with no luck and would really appreciate any help!
/// Hooks into any(?) form and does something
add_action('acf_frontend/save_user', 'retrieve_colors_2_update', 10, 2);
function retrieve_colors_2_update( $form, $user_id ) {
$user = get_user_by('ID',$user_id);
//get important fields
$btn_color = get_field('button_color', 'user_' .$user_id);
//// update a repeater loop (ACF)
if( have_rows('quick_link_selector', 'user_'.$user_id) ) {
$i = 0;
while( have_rows('quick_link_selector') ) {
the_row();
$i++;
update_sub_field('repeater_button_color', $btn_color );
}
}
}

categoryChoiceTree in prestashop module configuration page

I'm developing a prestashop module and I'm trying to show a category tree in my backoffice configuration page.
I'm trying to follow this instructions below but I don't know exactly where to add this code.
It should be inside main module's php? or inside a separate .php file and call it from the main one (don't know how to do it either).
As much time I'm spending trying to figure out, how to implement the code in the link above, the more I think I'm losing my time.
I see that "use" files, and this JS, " /admin-dev/themes/new-theme/js/components/form/choice-tree.js " are not in any prestashop folders.
Well, you should invest some time and learn Symfony since this is what you need to build backend modules for Prestashop 1.7.
As a pointer, you need to create a form class extending the CommonAbstractType, add a build form method. e.g. :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->context = Context::getContext();
$parents = [
['id_category' => 2, 'name' => 'Home', 'children' => $this->getSubCategories(1, true, 2)]
];
$builder->add('category', CategoryChoiceTreeType::class, [
'choices_tree' => $parents,
'choice_value' => 'id_category',
'choice_children' => 'children',
'choice_label' => 'name',
'disabled_values' => $disabledCategories,
'label' => 'Choose a category'
])
then add methods for retrieving the data to populate the form fields.
Then use this class in your controller and display the form:
$form = $this->createForm(YourFormForm::class);
Also add a processForm to process data.
As mentioned, this is not a copy/paste situation you need to understand the Symfony workflow.
The only way that I found to "paint" the categorytree in my configuration page is adding this code to the inputs form array:
Can anyone tell me how to retrieve users selection data to my database?
It does not work as any other form field.
array(
'type' => 'categories',
'label' => $this->l('Destination Category'),
'desc' => $this->l('Select ONE Category'),
'name' => 'CATEGORY_CATEGORY_TO',
'tree' => [
// 'selected_categories' => [],
'disabled_categories' => null,
'use_search' => false,
'use_checkbox' => false,
'id' => 'id_category_tree',
],
'required' => true
),
Well, it is SOLVED!!!! Finally it was very simple, but you must get the correct info for you particular case.
#Robertino's answer might be the best implementation, I don't know, but it became impossible to solve for me,
I uses this code below, and called $categoryTree from the form input. This input must be type=> categories_select
Thanks for your time, and for the help of another post from this forum.
$root = Category::getRootCategory();
//Generating the tree
$tree = new HelperTreeCategories('categories_1'); //The string in param is the ID used by the generated tree
$tree->setUseCheckBox(false)
->setAttribute('is_category_filter', $root->id)
->setRootCategory($root->id)
->setSelectedCategories(array((int)Configuration::get('CATEGORY_1'))) //if you wanted to be pre-carged
->setInputName('CATEGORY_1'); //Set the name of input. The option "name" of $fields_form doesn't seem to work with "categories_select" type
$categoryTree = $tree->render();
And the Form:
array(
'type' => 'categories_select',
'label' => $this->l('Category'),
'desc' => $this->l('Select Category '),
'name' => 'CATEGORY_1', //No ho podem treure si no, no passa la variable al configuration
'category_tree' => $categoryTree, //This is the category_tree called in form.tpl
'required' => true

Customizing checkout Postcode field into a custom drop-down menu

I am building a WooCommerce based store. I have a list of postcodes, each one has a different shipping cost attached through Shipping Zones (some provide free shipping, some have a flat rate).
When the customer goes to the checkout page, he needs to type his postcode number in the input field. Depending on postcode, an order preview will show different shipping total (free or flat rate).
Here's how the input field looks like in class-wc-countries.php:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
),
);
However, what I want to do is to turn this field into a drop-down menu, so the customer could just select his postcode option rather than type it.
I managed to make it drop-down, but whenever I choose any option it doesn't seem to change shipping total as it would with input field.
Here's what I did:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
'type' => 'select',
'options' => array(
'opt1' => "001122", "112200", "334400")
),
);
But this don't work.
Am I missing something?
How do I make these drop-down options change shipping total?
Thanks
This will answer very partially to your question, and just show you the way to customize checkout fields.
Overriding core files is not really something to do, as you will loose everithing each time Woocommerce is going to be updated and is not recommended.
To override checkout fields in a clean way, first you need to use a custom function hooked in one of that 2 filter hooks:
woocommerce_default_address_fields (when customizing billing and shipping address default fields)
woocommerce_checkout_fields (when customizing billing or shipping address fields and also others fields).
Related official documentation: Customizing checkout fields using actions and filters
So here I have chose the first hook, and I have corrected your post codes array. You will get that:
Here is that functional and tested code:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_postcode_field' );
function custom_override_default_postcode_field( $address_fields ) {
// Your postcodes array
$postcode_array = array(
'opt1' => "001122",
'opt2' => "112200",
'opt3' => "334400"
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
As selecting a post-code is a live front-end event, you will need to use a complex javascript/ajax script with some remote php function to achieve what you want to do, and this is a real development... It also depends on your settings and is complex to handle as there is already some woocommerce ajax scripts operating in that checkout page.

Retrieving custom CMS field from Magento?

I have created a custom "Meta title" field in Magento for CMS pages. The field displays on the admin and saves to the db, but I'm having trouble retrieving this information. I'm using the model/observer method.
Observer class
public function cmsField($observer)
{
// $model = Mage::registry('cms_page');
$form = $observer->getEvent()->getForm();
$fieldset = $form->addFieldset('example_metainfo_fieldset', array('legend'=>Mage::helper('cms')->__('Meta Info'),'class'=>'fieldset-wide'));
//add new field
$fieldset->addField('metatitle', 'text', array(
'name' => 'metatitle',
'label' => Mage::helper('cms')->__('Metatitle'),
'title' => Mage::helper('cms')->__('Metatitle'),
'disabled' => false,
//set field value
// 'value' => $model->getMetatitle()
));
$form->setValues(Mage::registry('cms_page')->getData());
}
do I need to add some other method to retrieve this value then? Or what is the best/most efficient way to display this information on the frontend? I've tried various approaches, but none have worked so far.

wordpress how to add wp_editor in an array

I had a small problem in my wordpress code I need to show a wordpress wp_editor in my page where it has array of values.the values are defined like the following
$fields[] = array(
'name' => __('Class', 'my-theme'),
'desc' => __('', 'my-theme'),
'id' => 'class'.$n,
'std' => ( ( isset($class_text[$n]['class']) ) ? $class_text[$n]['class'] : '' ),
'type' => 'text');
When I define my wp_editor like the above array it doesn't display where I want. Instead all the editors displayed at the top before any content in all pages.
I tried like the following set of array for the editor:
$fields[] = array(
'name' => __('My Content', 'my-theme'),
'id' => 'sectioncontent'.$n,
'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
'type' => wp_editor( '', 'sectioncontent'.$n ));
Attached the image of my problem:
Cause
By default wp_editor prints the textarea that's why you cannot assign it to any variable or array.
Solution
you can use output buffering of php to get printed data in variable like this:
ob_start(); // Start output buffer
// Print the editor
wp_editor( '', 'sectioncontent'.$n );
// Store the printed data in $editor variable
$editor = ob_get_clean();
// And then you can assign that wp_editor to your array.
$fields[] = array(
'name' => __('My Content', 'my-theme'),
'id' => 'sectioncontent'.$n,
'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
'type' => $editor); // <-- HERE
Looks to me like you're using Redux Framework to set up your theme/plugin option page - If you are looking to add the default Wordpress WYSIWYG (What You See Is What You Get - the same editor from the edit post page in the backend) editor in there you'll need to use the type: 'editor'.
It can be confusing - the wp_editor() function you are using is the right place to start if you were setting up this options page from scratch, but you'd need to do quite a bit of work to have it display where and how you want it. Redux et al make this quite a bit easier for you by generating the editor for you, so instead of you using the wp_editor function at all you just tell Redux that you want an editor field called 'My Content' to be one of the fields on the page.
The documentation for the editor field is here: https://docs.reduxframework.com/core/fields/editor/
If I am correct that you are using redux, the correct code to replace what you have is:
$fields[] = array(
'name' => __('My Content', 'my-theme'),
'id' => 'sectioncontent'.$n,
'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
'type' => 'editor');
To explain the other parts of this field array:
'Name' will be what shows up in the label for this field. In this case you are using the localization functions in wordpress (__()) to get a phrase from the local dictionary in the 'my-theme' domain.
'id' is what you will use to retrieve what has been entered into this field. It will also affect the ID attribute assigned to the HTML elements in the options page.
'std' is the default for the field, which will be the value of the field when the options page is first displayed, before the user has set any options
On the editor documentation page linked above, you'll see the details of various other options you can define, like whether to show Media Upload buttons, and whether to run the input through wpautop to replace line breaks in the editor with <p> tags (by default both of these are true).

Categories