WP Function echo a $var in Array - wordpress

Im trying to echo a $var in a function array in WP. I need to have a field on the Woocommerce checkout (accomplished) with a VALUE from a $_SESSION. The checkout field looks like this:
// Our hooked in function - $fields is passed via the filter!
public function VAT_override_checkout_fields( $fields ) {
$fields['billing']['VAT_cui'] = array(
'label' => __('Domæne', 'woocommerce'),
'placeholder' => _x('Intet Valgt', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'type' => 'select',
'options' => array(
'kobdomain' => __('echo $MYVAR', 'woocommerce' ),
)
);
return $fields;
}
I understand i doesnt work, but im stuck :(
Problem is i need the return $fields input value to be filled with a string from my Session. I think its possible with the SELECT OPTION field, but as you can see, i dunno how to echo value in it.

Related

Make only one of the billing fields required (Email or Phone) in WooCommerce

Is there a way to give the customer a choice? So the customers can decide how we can contact them.
Email or phone. Both are required but it is enough to fill any of them. They do not need to fill these 2 fields.
#Martin Mirchev suggests a select menu.
add_filter( 'woocommerce_checkout_fields' ,
'wc_custom_email_phone_field_checkout' );
function wc_custom_email_phone_field_checkout( $fields ) {
$fields['billing']['billing_contact'] = array(
'label' => __('How we contact you?', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => array(
'email' => __('Email', 'woocommerce' ),
'phone' => __('Phone', 'woocommerce' )
)
);
return $fields;
}
This code adds a select menu. Then how can I do an if-else?
I found a solution for this.
I keep the email field and created a select
add_filter( 'woocommerce_checkout_fields', 'wc_custom_email_phone_field_checkout' );
function wc_custom_email_phone_field_checkout( $fields ) {
$fields['billing']['billing_contact'] = array(
'label' => __('How should we contact you?', 'woocommerce'),
'placeholder' => __('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => array(
'email' => __('Email', 'woocommerce' ),
'phone' => __('WhatsApp', 'woocommerce' )
)
);
return $fields;
}
I also created an input
add_filter('woocommerce_checkout_fields', 'custom_woocommerce_whatsapp_fields');
function custom_woocommerce_whatsapp_fields( $fields ){
$fields['billing']['billing_whatsapp'] = array(
'label' => __('Enter your WhatsApp number', 'woocommerce'),
'placeholder' => __('Enter your WhatsApp number', 'woocommerce'),
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('whatsapp'), // add class name
'priority' => 25, // Priority sorting option
);
return $fields;
}
The input field is invisible with css.
.form-row.whatsapp {
display: none;
}
If the option phone is selected, it becomes visible.
let selectEl = document.getElementById('billing_contact');
selectEl.addEventListener('change', (e) => {
if (e.target.value == 'phone') {
document.getElementById('billing_whatsapp_field').style.display = 'block';
document.getElementById('billing_whatsapp_field').classList.add('validate-required');
} else {
document.getElementById('billing_whatsapp_field').style.display = 'none';
document.getElementById('billing_whatsapp_field').classList.remove('validate-required');
}
});
And finally, if the phone option is selected and form sent with the phone field empty:
add_filter( 'woocommerce_checkout_fields' , 'whatsapp_checkout_fields', 9999 );
function whatsapp_checkout_fields( $fields ) {
if (($_POST['billing_contact'] == 'phone')) {
$fields['billing']['billing_whatsapp']['required'] = true;
} else {
$fields['billing']['billing_whatsapp']['required'] = false;
}
return $fields;
}
If the mail option is selected, the WhatsApp field is not required.
In both cases, I wanted to keep the email as a required field for several reasons. But you can make it false when the phone is selected.

Adding custom fields to woocommerce checkout page disables place order button function

I'm trying to add this basic code to add a new field to the check out page. However, it seems to disable the place order button. Here is the code:
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Were you assisted with this order?'),
'placeholder' => __('Please enter the name of your rep here'),
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
You just need to remember to add the start echo'<div>' tag for your field options div. Otherwise it disables the woo-commerce functionality on the page.
add_action('woocommerce_after_order_notes','my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
//need to remember echo start tag here!
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'name_of_child', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Name of Child'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'name_of_child' ));
echo '</div>';
}

Woocommerce postcode field placeholder

Adding placeholder to Postcode billing field in Woocommerce.
In core class-wc-countries.php
'postcode' => array(
'label' => __( 'Postcode / ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-last', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
),
So there is no way to override it woocommerce_billing_fields filter. Why some fields have placeholder key but other not? Some fields have only labels, some only placeholders and some have both. I don't get logic behind this.
So my question is: how to add placeholder text to Postcode field. I can do it with Javascript, but that seams not natural. Also I can modify core, which is even worse then Javascript solution. What else can I do?
As suggested by #Ash Patel we can do it like this:
add_filter( 'woocommerce_checkout_fields', function($fields){
$fields['billing']['billing_postcode']['placeholder'] = __('My Post Code', 'woocommerce');
return $fields;
} );
And for shipping
add_filter( 'woocommerce_shipping_fields', function($fields){
$fields['shipping_postcode']['placeholder'] = __('My Post Code', 'woocommerce');
return $fields;
} );
add below code into your function.php for adding/updating postal code for checkout and edit screen.
//on edit address screen
function filter_woocommerce_billing_fields( $wooccm_billing_fields, $int ) {
$wooccm_billing_fields['billing_postcode']['placeholder'] = __('My Post Code', 'woocommerce');;
return $wooccm_billing_fields;
};
add_filter( 'woocommerce_billing_fields', 'filter_woocommerce_billing_fields', 10, 2 );
add_action('woocommerce_checkout_fields', 'update_placeholder_checkout_form_billing');
function update_placeholder_checkout_form_billing($wcCheckout_fields) {
$wcCheckout_fields['billing']['billing_postcode'] = array(
'label' => 'Postcode / ZIP',
'placeholder' => _x('My Post Code', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('checkout-billing-postal-code')
);
return $wcCheckout_fields;
}

How to get the value instead of order_id with get_post_meta()

I've had the following code to add a custom order field in the woocommerce checkout:
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_reason_for_purchase'] = array(
'label' => __('Reason for purchase', 'woocommerce'),
'placeholder' => _x('Reason for purchase', 'placeholder', 'woocommerce'),
'required' => false,
'type' => 'select',
'class' => array('form-row-first'),
'options' => array(
'option_1' => __('Personal', 'woocommerce'),
'option_2' => __('Academic', 'woocommerce'),
'option_3' => __('Small Business', 'woocommerce'),
'option_4' => __('Large Organization', 'woocommerce')
)
);
return $fields;
}
Then, followed by the below code to update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta($order_id) {
if(!empty($_POST['billing_reason_for_purchase'])) {
update_post_meta($order_id,'Reason for purchase',sanitize_text_field($_POST['billing_reason_for_purchase']));
}
}
Next, display the field on the order edit page:
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta',10,1);
function my_custom_checkout_field_display_admin_order_meta($order){
//echo '<p><strong>'.__('Reason for purchase').':<strong>'.get_post_meta($order->id, 'Reason for purchase',true).'</p>';
echo '<p><strong>'.__('Reason for purchase').':<strong>'.get_post_meta(get_the_ID(), 'Reason for purchase',true).'</p>';
}
The problem I'm having is if I created a dummy order with choosing "academic" as the reason for purchase, I'd get "option_2" instead of "academic" in the order edit page.
Please help point me in the right direction.
It happens because the value of the selected option (and so of $_POST['billing_reason_for_purchase']) is actually the key of the array (in your example option_2) and not the text related. In fact this is the option tag created:
<option value="option_2">Academic</option>
You're saving only the key of your array.
So you need to retrieve the array of options even on the my_custom_checkout_field_display_admin_order_meta function to get the proper text.
You could copy the array in each function (but is not convenient to duplicate code), or put it in a global variable to be able to access it from anywhere (but then something else could change it), so instead you can use a function that returns the array:
function reasons_for_purchase () {
return array(
'option_1' => __('Personal', 'woocommerce'),
'option_2' => __('Academic', 'woocommerce'),
'option_3' => __('Small Business', 'woocommerce'),
'option_4' => __('Large Organization', 'woocommerce')
);
}
And then use it where you need it:
add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_reason_for_purchase'] = array(
'label' => __('Reason for purchase', 'woocommerce'),
'placeholder' => _x('Reason for purchase', 'placeholder', 'woocommerce'),
'required' => false,
'type' => 'select',
'class' => array('form-row-first'),
'options' => reasons_for_purchase()
);
return $fields;
}
add_action('woocommerce_admin_order_data_after_billing_address','my_custom_checkout_field_display_admin_order_meta',10,1);
function my_custom_checkout_field_display_admin_order_meta($order){
$reasons = reasons_for_purchase();
$reason = get_post_meta($order->id, 'Reason for purchase', true);
if( isset($reasons[$reason]) )
echo '<p><strong>'.__('Reason for purchase').':</strong> '. $reasons[$reason] .'</p>';
}

drupal field widget not saving submitted data

I'm trying to create a custom widget but when I submit, Drupal doesn't seem to save any data. When using hook_field_attach_submit() to display what data I've pasted, it is listed as null.
Strangely, if i change the #type to be a single textfield instead of a fieldset it will save only the first character of the string that has been entered.
This seems like a validation issue, but I'm not sure how to hook into it or to debug the problem. Where can I go from here?
<?php
function guide_field_widget_info(){
dpm("guide_field_widget_info");
return array(
'guide_text_textfield' => array(
'label' => t('test Text field'),
'field types' => array('text'),
'settings' => array('size' => 60),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
)
);
}
function guide_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$field_name = $instance['field_name'];
$required = $element['#required'];
$item =& $items[$delta];
$element += array(
'#type' => 'fieldset',
'#title' => t('helloooooooo'),
);
$required = $element['#required'];
$item =& $items[$delta];
$element['nametest'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => $required,
// use #default_value to prepopulate the element
// with the current saved value
'#default_value' => isset($item['nametest']) ? $item['nametest'] : '',
);
$element['checkme'] = array(
'#title' => t('Check this box or dont'),
'#type' => 'checkbox',
'#default_value' => isset($item['checkme']) ? $item['checkme'] : '',
);
//When changing the above code to have a single field, $value is no longer null but will display the first character of the string. I've pasted the code I used to test beloe
/*
$element+= array(
'#title' => t('Name'),
'#type' => 'textfield',
'#default_value' => isset($item['nametest']) ? $item['nametest'] : '',
);
*/
return $element;
}
//hooking this here is required given that after submit, the value is a multidimensional array, whereas the expected value of text is, well, text :-)
function guide_field_attach_submit($entity_type, $entity, $form, &$form_state){
dpm($form,"guide_field_attach_submit data"); //shows $form[field_test_field][und][0] [value] as being null
}
hook_field_is_empty is mandatory and has to be implement like following:
/**
* Implements hook_field_is_empty().
*/
function MODULENAME_field_is_empty($item, $field) {
if ($field['type'] == 'FIELDTYPE') {
if (empty($item[$field['type']]['YourField']) ) {
return (TRUE);
}
}
return (FALSE);
}

Resources