Clear shipping_*fields in WooCommerce Checkout - php

Trying to always have the "Ship to a different adress" cleared when a user is buying in WooCommerce Checkout. The following code is though doing aboslutely nothing. Does it have todo with cookies?
add_filter( 'woocommerce_checkout_get_value', 'remove_clear_checkout_shipping_fields', 10, 2 );
function remove_clear_checkout_shipping_fields( $value, $input ) {
if ( strpos( $input, 'shipping_' ) !== FALSE ) {
$value = '';
}
}

This way you can modify each field:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_first_name'] = '';
$fields['shipping']['shipping_last_name'] = '';
$fields['shipping']['shipping_company'] = '';
$fields['shipping']['shipping_address_1'] = '';
$fields['shipping']['shipping_address_2'] = '';
$fields['shipping']['shipping_city'] = '';
$fields['shipping']['shipping_postcode'] = '';
$fields['shipping']['shipping_country'] = '';
$fields['shipping']['shipping_state'] = '';
return $fields;
}
Just remove unnecessary fields.

Related

Dynamically added custom fields not displayed on WooCommerce email notifications

I have a different kind of scenario then the typical custom fields (I suppose). I am not getting custom values (fields) from user in the form rather I have an implementation which adds:
ColorName
Size
City
These are from a custom product flow which adds custom attributes to the cart, here is how I am doing that:
add_action('wp_ajax_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
add_action('wp_ajax_nopriv_wdm_add_user_custom_data_options', 'wdm_add_user_custom_data_options_callback');
function wdm_add_user_custom_data_options_callback()
{
// print_r($_POST);
$productIDM = $_POST['product_id'];
// case swith
switch ($productIDM) {
case "Ace Dura Silk":
$productID = 3254;
break;
case "Ace Matt Finish":
$productID = 3232;
break;
case "Ace Plastic Emulsion":
$productID = 3276;
break;
case "Ace Weather Defender":
$productID = 2991;
break;
case "Overall Plasticoat":
$productID = 3112;
break;
}
$colorname = $_POST['colorname'];
$cityname = $_POST['cityname'];
$size = $_POST['size'];
$price = $_POST['price'];
global $woocommerce;
$woocommerce->cart->add_to_cart( $productID, 1 );
// die();
// echo 'I am in...';
$result = array(
'status' => true
);
echo json_encode($result);
}
add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,10);
function wdm_add_item_data($cart_item_data, $product_id) {
global $woocommerce;
$new_value = array();
$new_value['_custom_options'] = $_POST['custom_options'];
if(empty($cart_item_data)) {
return $new_value;
} else {
return array_merge($cart_item_data, $new_value);
}
}
add_filter('woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
function wdm_get_cart_items_from_session($item,$values,$key) {
if (array_key_exists( '_custom_options', $values ) ) {
$item['_custom_options'] = $values['_custom_options'];
}
return $item;
}
add_filter('woocommerce_cart_item_name','add_usr_custom_session',1,3);
function add_usr_custom_session($product_name, $values, $cart_item_key ) {
$return_string = $product_name . "<div class='cart_subitems_custom'><br /><small>".$values['_custom_options']['colorname']."</small><br /><small>".$values['_custom_options']['cityname']."</small><br /><small>".$values['_custom_options']['size']."</small></div>" ; //. "<br />" . print_r($values['_custom_options']);
return $return_string;
}
add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
function wdm_add_values_to_order_item_meta($item_id, $values) {
global $woocommerce,$wpdb;
wc_add_order_item_meta($item_id,'_colorname',$values['_custom_options']['colorname']);
wc_add_order_item_meta($item_id,'_cityname',$values['_custom_options']['cityname']);
wc_add_order_item_meta($item_id,'_size',$values['_custom_options']['size']);
}
add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
function update_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {
// Version 2.x
//$value['data']->price = $value['_custom_options']['custom_price'];
// Version 3.x / 4.x
if($value['_custom_options']['price'] == null){
echo"";
}else{
$value['data']->set_price($value['_custom_options']['price']);
}
}
}
I am getting these custom values almost everywhere except Email Notification.
Here is what normal product order edit shows:
Here is how I am getting the custom product in order edit page:
I have tried all the solution I can possibly find (filter & action hooks) but nothing works for me.
I have tried first answer from this:
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if( isset( $values['colorname'] ) )
$item->add_meta_data( __('DCM Shade'), $values['_colorname'] );
}
Also the common method I found everywhere:
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
// Get meta
$color = $order->get_meta( 'colorname', true );
// NOT empty
if( ! empty( $color ) ) {
$fields['colorname'] = array(
'label' => __( 'Shade' ),
'value' => $color,
);
}
// Get (other) meta
$shipping_email = $order->get_meta( '_cityname', true );
// NOT empty
if ( ! empty( $shipping_email ) ) {
$fields['_cityname'] = array(
'label' => __( 'City' ),
'value' => $shipping_email,
);
}
return $fields;
}
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
But I can't get the custom fields.
What am I doing wrong can please anyone please guide me.

Woocommerce custom add to cart with unique id

I am not that proficient at coding so please bear with me.
I have this custom add to cart code:
public static function add_to_cart( $params, $uid ) {
$data = self::get_calc_data($uid);
foreach ($data as $calc_id => $calc_data) {
$product_id = isset($calc_data['woo_info']['product_id']) ? $calc_data['woo_info']['product_id'] : null;
if ( $product_id && intval($product_id) === intval($params['woo_info']['product_id']) ) {
$meta = [
'product_id' => $product_id,
'item_name' => isset($calc_data['item_name']) ? $calc_data['item_name'] : '',
];
if(!empty($calc_data['descriptions']) && is_array($calc_data['descriptions'])) {
foreach ($calc_data['descriptions'] as $calc_item) {
if ( $calc_item['hidden'] === true ){
continue;
}
if ( strpos($calc_item['alias'], 'datePicker_field_id_') !== false ) {
$val = $calc_item['converted'] ? ' ('. $calc_item['converted'] . ') ' . $calc_item['value'] : $calc_item['value'];
}else{
$labels = isset($calc_item['extra']) ? $calc_item['extra']: '';
if ( (strpos($calc_item['alias'], 'radio_field_id_') !== false
|| strpos($calc_item['alias'], 'dropDown_field_id_') !== false)
&& key_exists('options', $calc_item) ) {
$labels = CCBWooCheckout::getLabels($calc_item['options']);
}
if ( strpos($calc_item['alias'], 'multi_range_field_id_') !== false
&& key_exists('options', $calc_item) && count($calc_item['options']) > 0 ) {
$labels = key_exists('label', $calc_item['options'][0]) ?
$calc_item['options'][0]['label']: '';
}
$val = isset($labels) ? $labels . ' ' . $calc_item['converted'] : $calc_item['converted'];
}
$meta['calc_data'][$calc_item['label']] = $val;
}
}
/** add totals data */
if( !empty($calc_data['ccb_total_and_label']) && is_array($calc_data['ccb_total_and_label']) ) {
$meta['ccb_total'] = $calc_data['ccb_total_and_label']['total'];
}
WC()->cart->add_to_cart($product_id, 1, '', array(), array('ccb_calculator' => $meta));
}
}
}
It adds simple item from a plugin into Woocommerce Cart. I am trying to add a unique ID for every added item so that I can add multiples of the same item and they would show as unique items in the cart.
I was trying to create the unique ID with
public static function add_custom_cart_item_data( $cart_item_data, $cart_item_key ) {
$cart_item_data[custom_data]['unique_key'] = md5( microtime().rand() );
return $cart_item_data;
}
and then use the $cart_item_data; changing the 'product_id' => $product_id, to 'product_id' => $cart_item_data, but sadly this doesn't seem to work. What am I doing wrong?
Really appreciate all your help!

How to disable fields that are pre-filled in WooCommerce checkout?

I would like to prevent (make these fields readonly, for example) users from changing their billing information on the WooCommerce checkout form.
I'm currently using this code snippet:
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
function mycustom_woocommerce_billing_fields($fields)
{
$fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_email']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_phone']['custom_attributes'] = array('readonly'=>'readonly');
return $fields;
}
But the problem is: If the user has not filled in any of these fields in the registration, he is unable to insert his data in the checkout form because these fields are not editable.
My question is:
If the fields are not empty, how to make them readonly (or disabled)
Someone who can help me with this?
The answer of 7uc1f3r certainly works getting the user data… But since WooCommerce 3, you can use WC_Checkout get_value() dedicated method as follow:
add_filter( 'woocommerce_billing_fields', 'filter_wc_billing_fields', 10, 1 );
function filter_wc_billing_fields( $fields ) {
// On checkout and if user is logged in
if ( is_checkout() && is_user_logged_in() ) {
// Define your key fields below
$keys_fields = ['billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone'];
// Loop through your specific defined fields
foreach ( $keys_fields as $key ) {
// Check that a value exist for the current field
if( ( $value = WC()->checkout->get_value($key) ) && ! empty( $value ) ) {
// Make it readonly if a value exist
$fields[$key]['custom_attributes'] = ['readonly'=>'readonly'];
}
}
}
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
If you want this code to be also active in My account > Addresses > edit…, you will just have to remove is_checkout() && from the first IF statement.
I believe this is what you are looking for, comment with explanation added in code
function mycustom_woocommerce_billing_fields( $fields ) {
// Get current user
$user = wp_get_current_user();
// User id
$user_id = $user->ID;
// User id is found
if ( $user_id > 0 ) {
// Fields
$read_only_fields = array ( 'billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone' );
// Loop
foreach ( $fields as $key => $field ) {
if( in_array( $key, $read_only_fields ) ) {
// Get key value
$key_value = get_user_meta($user_id, $key, true);
if( strlen( $key_value ) > 0 ) {
$fields[$key]['custom_attributes'] = array(
'readonly'=>'readonly'
);
}
}
}
}
return $fields;
}
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );

Rearranging and customizing woocommerce checkout fields

I need to modify the default woocommerce billing&shipping fields. I need to move some of them, set a different form-row-class and add a placeholder while removing the labels. My code is working but I am wondering if there is a cleaner solution aka if the code can be shortened and/or optimized. my code is
add_filter('woocommerce_default_address_fields', 'override_address_fields');
function override_address_fields( $address_fields ) {
$address_fields['first_name']['placeholder'] = 'yxz';
$address_fields['last_name']['placeholder'] = 'yxz';
$address_fields['address_1']['placeholder'] = 'yxz';
$address_fields['company']['placeholder'] = 'yxz';
$address_fields['postcode']['placeholder'] = 'yxz';
$address_fields['city']['placeholder'] = 'yxz';
return $address_fields;
}
add_filter( "woocommerce_checkout_fields", "reordering_checkout_fields", 15, 1 );
function reordering_checkout_fields( $fields ) {
$fields['billing']['billing_phone']['placeholder'] = 'yxz';
$fields['billing']['billing_email']['placeholder'] = 'yxz';
unset($fields['order']['order_comments']);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'rearrange_checkout_fields' );
function rearrange_checkout_fields( $checkout_fields ) {
$checkout_fields['billing']['billing_country']['priority'] = 80;
$checkout_fields['shipping']['shipping_country']['priority'] = 80;
return $checkout_fields;
}
You can include your third function in the first one, so your code will be like:
add_filter('woocommerce_default_address_fields', 'customize_default_address_fields', 20, 1 );
function customize_default_address_fields( $address_fields ) {
$address_fields['first_name']['placeholder'] = 'yxz';
$address_fields['last_name']['placeholder'] = 'yxz';
$address_fields['address_1']['placeholder'] = 'yxz';
$address_fields['company']['placeholder'] = 'yxz';
$address_fields['postcode']['placeholder'] = 'yxz';
$address_fields['city']['placeholder'] = 'yxz';
// Reorder billing and shipping "country" fields (on checkout page)
if ( is_checkout() )
$address_fields['country']['priority'] = 80; // Country
return $address_fields;
}
add_filter( "woocommerce_checkout_fields", "customize_other_checkout_fields", 20, 1 );
function customize_other_checkout_fields( $fields ) {
$fields['billing']['billing_phone']['placeholder'] = 'yxz';
$fields['billing']['billing_email']['placeholder'] = 'yxz';
$fields['billing']['billing_email']['class'] = array('form-row-first'); // HERE
$fields['billing']['billing_phone']['class'] = array('form-row-last'); // HERE
unset($fields['order']['order_comments']);
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Pre-fill Woocommerce checkout fields with Url variables saved in session

When people enter the my woocommerce shop following a link in an sales email with email and name as parameters I would like to prefill the name and email in the checkout page.
Therefore I created an action and filter. This works as expected but only if I do a hard refresh on the sales page (ctrl + f5)
I've excluded the sales page and the checkout page from the cache and varnish cache but this didn't fix the issue.
Am I missing something here? Do you have any idea why this only works with a hard refresh?
Any help is greatly appreciated.
Code:
function save()
{
if ( is_page( 'sales-page' ) )
{
if ( isset( $_GET['tu_em'] ) ) {
global $woocommerce;
$woocommerce->session->set( 'tu_em', $_GET['tu_em'] );
}
if ( isset( $_GET['tu_name'] ) ) {
global $woocommerce;
$woocommerce->session->set( 'tu_name', $_GET['tu_name'] );
}
}
}
add_action( 'wp_enqueue_scripts', 'save_email' , 1100);
function override_checkout_email_field( $fields ) {
global $woocommerce;
$email = $woocommerce->session->get('tu_em');
if(!is_null($email)) {
$fields['billing']['billing_email']['default'] = $email;
}
$name = $woocommerce->session->get('tu_name');
if(!is_null($name)) {
$fields['billing']['billing_first_name']['default'] = $name;
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'override_checkout_email_field' );
Here below, you will find the correct working code, to save user data from an URL (GET) into WooCommerce sessions and to autofill with that data the related checkout fields.
URL will be like: http://example.com/sales-page/?tu_em=name#example.com&tu_name=theFirstName
This can be done from any URL as the code will detect the URL variable, when they are set.
The code;
// Save user data from URL to Woocommerce session
add_action( 'template_redirect', 'set_custom_data_wc_session' );
function set_custom_data_wc_session () {
if ( isset( $_GET['tu_em'] ) || isset( $_GET['tu_name'] ) ) {
$em = isset( $_GET['tu_em'] ) ? esc_attr( $_GET['tu_em'] ) : '';
$name = isset( $_GET['tu_name'] ) ? esc_attr( $_GET['tu_name'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'email' => $em, 'name' => $name ) );
}
}
// Autofill checkout fields from user data saved in Woocommerce session
add_filter( 'woocommerce_billing_fields' , 'prefill_billing_fields' );
function prefill_billing_fields ( $address_fields ) {
// Get the session data
$data = WC()->session->get('custom_data');
// Email
if( isset($data['email']) && ! empty($data['email']) )
$address_fields['billing_email']['default'] = $data['email'];
// Name
if( isset($data['name']) && ! empty($data['name']) )
$address_fields['billing_first_name']['default'] = $data['name'];
return $address_fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Shipping information(send to another address) by default is auto we use that for autofill billing fildes
add_filter( 'woocommerce_checkout_fields' , 'ahmadyani_checkout_field_defaults', 20 );
function ahmadyani_checkout_field_defaults( $fields ) {
$user = get_user_meta(get_current_user_id());
$first_name = $user ? $user['shipping_first_name'][0] : '';
$last_name = $user ? $user['shipping_last_name'][0] : '';
$company = $user ? $user['shipping_company'][0] : '';
$shipping_address_1 = $user ? $user['shipping_address_1'][0] : '';
$shipping_address_2 = $user ? $user['shipping_address_2'][0] : '';
$shipping_city = $user ? $user['shipping_city'][0] : '';
$shipping_state = $user ? $user['shipping_state'][0] : '';
$shipping_postcode = $user ? $user['shipping_postcode'][0] : '';
$fields['billing']['billing_first_name']['default'] = $first_name;
$fields['billing']['billing_last_name']['default'] = $last_name;
$fields['billing']['billing_company']['default'] = $company;
$fields['billing']['billing_address_1']['default'] = $shipping_address_1;
$fields['billing']['billing_address_2']['default'] = $shipping_address_2;
$fields['billing']['billing_city']['default'] = $shipping_city;
$fields['billing']['billing_state']['default'] = $shipping_state;
$fields['billing']['billing_postcode']['default'] = $shipping_postcode;
return $fields;
}

Categories