Woocommerce get activ value from selector - wordpress

I am trying to get the active value from selector box. I need the active value from the array. But it is not working.
The code I have written so far is:
add_action('woocommerce_after_order_notes', 'woo_add_custom_text_field_title_110');
function woo_add_custom_text_field_title_110($checkout)
{
$testarray = get_user_meta(get_current_user_id(), 'department');
echo '<div id="_select"><h3>'.__('Afdeling').'</h3>';
woocommerce_form_field ('showafdeling', array (
'id' => '_select',
'type' => 'select',
'label' => __('Afdeling'),
'required' => true,
'options' => $testarray), $checkout->get_value( '_select')); //My problem
}
// Update the order meta with active array 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) //$order_id
{
update_post_meta( $order_id, 'showafdeling', esc_attr($_POST['_select']));
}

Related

Add a custom dropdown field on My account > edit account in WooCommerce

I am using the following code to display an additional input field on the edit account page of WooCommerce.
/**
* Step 1. Add your field - Age Range
*/
add_action( 'woocommerce_edit_account_form', 'misha_add_age_range_field_account_form' );
function misha_add_age_range_field_account_form() {
echo "<h4> Please fill in the following details to complete your profile for review </h4>";
woocommerce_form_field(
'certified_age_range',
array(
'type' => 'text',
'required' => true, // remember, this doesn't make the field required, just adds an "*"
'label' => 'Your Age',
'description' => '',
),
get_user_meta( get_current_user_id(), 'certified_age_range', true ) // get the data
);
}
/**
* Step 2. Save field value
*/
add_action( 'woocommerce_save_account_details', 'misha_save_age_range_account_details' );
function misha_save_age_range_account_details( $user_id ) {
update_user_meta( $user_id, 'certified_age_range', sanitize_text_field( $_POST['certified_age_range'] ) );
}
/**
* Step 3. Make it required
*/
add_filter('woocommerce_save_account_details_required_fields', 'misha_make_field_required');
function misha_make_age_range_field_required( $required_fields ){
$required_fields['certified_age_range'] = 'Age';
return $required_fields;
}
add_filter( 'woocommerce_customer_meta_fields', 'misha_admin_age_range_field' );
function misha_admin_age_range_field( $admin_fields ) {
$admin_fields['billing']['fields']['certified_age_range'] = array(
'label' => 'Age',
'description' => 'Get Certified Form Field',
);
return $admin_fields;
}
The code above works perfectly, and this is how the 'Age' field appears on the page:
But now I need to make this field into a dropdown one instead of a simple text field.
This said, I tried doing the above customisation with the following code but without the desired result. Any advice?
add_filter( 'woocommerce_save_account_details_required_fields' , 'custom_override_age_field' );
function custom_override_age_field( $account_fields ) {
$option_age = array(
'' => __( 'Select your Age Range' ),
'18-24' => '18-24',
'25-34' => '25-34',
'35-44' => '35-44',
'45-54' => '45-54',
'55-64' => '55-64',
'65+' => '65+',
);
$account_fields['account_first_name']['type'] = 'select';
$account_fields['account_first_name']['options'] = $option_age;
return $account_fields;
}
Your code says: Step 1. Add your field - in your attempt you are using the hook from step 3.. while step 3 indicates Make it required so that's your first mistake.
The bottom line is that in the first step you have to edit the woocommerce_form_field settings. In your code the type is 'text' and you have to change this to 'select'.
So you get:
/**
* Step 1. Add your field - Age Range
*/
function action_woocommerce_edit_account_form() {
echo "<h4> Please fill in the following details to complete your profile for review </h4>";
// Select field
woocommerce_form_field( 'certified_age_range', array(
'type' => 'select',
'class' => array( 'form-row-wide' ),
'label' => __( 'Your age', 'woocommerce' ),
'required' => true, // remember, this doesn't make the field required, just adds an "*"
'options' => array(
'' => __( 'Select your age range', 'woocommerce' ),
'18-24' => '18-24',
'25-34' => '25-34',
'35-44' => '35-44',
'45-54' => '45-54',
'55-64' => '55-64',
'65+' => '65+',
)
), get_user_meta( get_current_user_id(), 'certified_age_range', true ) );
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form', 10, 0 );
/**
* Step 2. Make it required
*/
function filter_woocommerce_save_account_details_required_fields( $required_fields ) {
$required_fields['certified_age_range'] = __( 'Age', 'woocommerce' );
return $required_fields;
}
add_filter( 'woocommerce_save_account_details_required_fields', 'filter_woocommerce_save_account_details_required_fields', 10, 1 );
/**
* Step 3. Save field value
*/
function action_woocommerce_save_account_details( $user_id ) {
if ( isset( $_POST['certified_age_range'] ) ) {
// Update field
update_user_meta( $user_id, 'certified_age_range', sanitize_text_field( $_POST['certified_age_range'] ) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
/**
* Step 4. Get address fields for the edit user pages.
*/
function filter_woocommerce_customer_meta_fields( $args ) {
$args['billing']['fields']['certified_age_range'] = array(
'label' => __( 'Age', 'woocommerce' ),
'description' => __( 'Get Certified Form Field', 'woocommerce' ),
);
return $args;
}
add_filter( 'woocommerce_customer_meta_fields', 'filter_woocommerce_customer_meta_fields', 10, 1 );

Merge custom fields in checkout page with shipping fields

I'm trying to move custom fields that I created on my checkout page in the Shipping block.
What I've tried is this code: thise are the new fields
// Add a new checkout field
function filter_checkout_fields($fields){
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => false,
'label' => __( 'Field 1:' )
),
'another_field' => array(
'type' => 'text',
'required' => false,
'label' => __( 'Field 2:' )
),
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'filter_checkout_fields' );
// display the extra field on the checkout form
function extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'Title' ); ?></h3>
<?php
// because of this foreach, everything added to the array in the previous function will display automagically
foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'extra_checkout_fields' );
And this is how I'm trying to move them (replace current fields)
add_filter( 'woocommerce_checkout_fields', 'another_group' );
function another_group( $checkout_fields ){
// 1. We assign a field array to another group here
$checkout_fields['some_field'] = $checkout_fields['shipping']['shipping_first_name'];
$checkout_fields['another_field'] = $checkout_fields['shipping']['shipping_last_name'];
// 2. Remove a field from a previous location
unset( $checkout_fields['shipping']['shipping_first_name'] );
unset( $checkout_fields['shipping']['shipping_last_name'] );
return $checkout_fields;
}
What is happening is that the ['shipping_first_name'] and ['shipping_last_name'] are removed (unset) but nothing appeared on their place.
Is this possible to happen at all?
you can set priority of custom field with your old field priority like below
$fields['shipping']['some_field']['priority'] = 10;
$fields['shipping']['another_field']['priority'] = 20;
Just unset the fields that you don't need as you do already but don't try to override them in this function
add_filter( 'woocommerce_checkout_fields', 'another_group' );
function another_group( $checkout_fields ){
// 2. Remove a field from a previous location
unset( $checkout_fields['shipping']['shipping_first_name'] );
unset( $checkout_fields['shipping']['shipping_last_name'] );
// ... more fields for unset here
return $checkout_fields;
}
Then in your function where you have [extra_fields] change it to [shipping] this will place the custom fields in the shipping block.
// Add a new checkout field
function filter_checkout_fields($fields){
$fields['shipping'] = array(
'some_field' => array(
'type' => 'text',
'required' => false,
'label' => __( 'Field 1:' )
),
... so on

How change shipping method depends on custom checkout field data woocommerce?

I have added a custom checkout field to my woocommerce checkout page.
add_filter( 'woocommerce_checkout_fields' , 'dropdown' );
function dropdown( $fields ) {
$fields['billing']['ilce'] = array(
'label' => __('İlçe', 'woocommerce'),
'placeholder' => _x('İlçe', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array(
'form-row-wide',
'update_totals_on_change'
),
'clear' => true,
'id' => 'ilcepicker',
'type' => 'select',
'options' => array(
'Ataşehir' => __('Ataşehir', 'woocommerce' ),
'Şişli' => __('Şişli', 'woocommerce' )
)//end of options
);
return $fields;
}
I want to change shipping method according to options that is selected from my custom field. How can i achieve it? I could not find a way.
I haven't tried this but could give some pointers you could try. Look for the selected value using jquery and send it to woocommerce ajax functionality
function woocommerce_add_gift_box() {
if (is_checkout()) {
?>
<script type="text/javascript">
jQuery( document ).ready(function( $ ) {
$('#selectedfield').click(function(){
jQuery('body').trigger('update_checkout');
});
});
</script>
<?php
}
}
Ajax will return a post data value
if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {
return;
}
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST; // fallback for final checkout (non-ajax)
}
$varField = $post_data['selectedfield'];
You can use this to tinker with your shipping methods. Using if conditions.
Hope this helps.

WP Function echo a $var in Array

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.

How to get the values of Wordpress customize checkboxes

I can't figure out how to get the value - whether they are checked or not - from checkboxes in the WP customize manager.
This is the code in functions.php:
$wp_customize->add_setting('social_facebook', array(
'type' => 'option',
));
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'social_facebook',
array(
'label' => __( 'Facebook', 'theme_name' ),
'section' => 'social-icons',
'settings' => 'social_facebook',
'type' => 'checkbox',
)
)
);
And this is how I try to get the value:
<?php
$facebook = get_theme_mod('social_facebook');
if ($facebook != ''){?>
<style>
.facebook {display:inline!important;}
</style>
<?php }
?>
They values of the checkboxes are either "" (empty) or "1", so the system registers the checking of them. However, I don't know how to get the value through the get_theme_mod approach. Also, they don't have any name values, so I can't get the value through the usual way either.
$sticky_mod = get_theme_mod( 'wca_header_section_sticky' ) == '1' ? 'sticky' : '';
This is an example in my case - if that option is checked, it will echo a "sticky" class in my template.
if the setting 'my_theme_settings[social_facebook]' checkbox is unchecked:
<?php if( get_theme_mod( 'my_theme_settings[social_facebook]' ) == '') { ?>
//if setting is unchecked content here will be shown
<?php } // end if ?>
For full article see: http://themefoundation.com/wordpress-theme-customizer
Try use and customize this (tested, in functions.php:
function mytheme_customize_register( $wp_customize ){
$wp_customize->add_section(
// ID
'layout_section',
// Arguments array
array(
'title' => __( 'Layout', 'my_theme' ),
'capability' => 'edit_theme_options',
'description' => __( 'social needs ;)', 'my_theme' )
)
);
$wp_customize->add_setting(
// ID
'my_theme_settings[social_facebook]',
// Arguments array
array('type' => 'option')
);
$wp_customize->add_control(
// ID
'layout_control',
array(
'type' => 'checkbox',
'label' => __( 'Facebook', 'my_theme' ),
'section' => 'layout_section',
// This last one must match setting ID from above
'settings' => 'my_theme_settings[social_facebook]'
)
);
}
add_action( 'customize_register', 'mytheme_customize_register' );
to read in template
$my_theme_settings = get_option( 'my_theme_settings' );
echo $my_theme_settings['social_facebook'];
the problem is in the WP_Customize_Setting::value() it expect to return false
to uncheck the checkbox (or leave the checkbox unchecked) while some program will return '0' or ''.
In my case I have to extend the WP_Customize_Setting and override the value() method to force returning boolean.
<?php
class ForceBooleanSettings
extends WP_Customize_Setting {
public function value() {
return (bool) parent::value();
}
}
// Example on using this extend class
$customizer->add_setting(new ForceBooleanSettings(
$customizer,
'myuniquekey',
array(
'default' => false,
'transport' => 'refresh',
)));
?>
Here is my working solution:
function theme_name_custom_settings( $wp_customize ) {
$wp_customize->add_setting( 'social_facebook', array(
'default' => 1, // Set default value
'sanitize_callback' => 'esc_attr', // Sanitize input
)
);
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'social_facebook', // Setting ID
array(
'label' => __( 'Facebook', 'theme_name' ),
'section' => 'social_icons', // No hyphen
'settings' => 'social_facebook', // Setting ID
'type' => 'checkbox',
)
)
);
}
add_action( 'customize_register', 'theme_name_custom_settings' );
Then check it's value (0 or 1) like so:
if ( !get_theme_mod( 'social_facebook' ) ) { // false
return;
}
// or
if ( get_theme_mod( 'social_facebook' ) == 1 ) { // true
return;
}
<div class="facebook" <?php echo ( get_theme_mod( 'social_facebook' ) ) ? "style='display:none;'" : "" ?>>

Resources