copy city field value to be same as state field - wordpress

i use in my woocommerce website state field only and i (unset) city field but it is affect on payment requirements so i need once customer enter value in state field , city field fill with same state value
add_filter( 'woocommerce_states', 'Saudi_arabia_cities_woocommerce' );
function Saudi_arabia_cities_woocommerce( $states ) {
$states['SA'] = array(
'RUH' => __('Riyadh', 'woocommerce') ,
);
return $states;
}
i hope find solution

Related

woocommerce set user meta value as default field value if exist

I am trying to set the default field value of a custom confirm email field in checkout.
If billing_email exists in user meta I would like to set the meta value as default value for the field.
If billing_email not exists in user meta I want to set the meta value of user_email as default value for the field.
I tried this:
$fields['billing_email_confirm'] = array(
'label' => 'confirm email *',
'class' => array( 'form-row-last' ),
'priority' => 9999,
);
$current_user = wp_get_current_user();
$havemeta = get_user_meta($customer_id, 'billing_email', false);
if ($havemeta)
{
$fields['billing_email_confirm']['default'] = $current_user->user_email;
} else {
$fields['billing_email_confirm']['default'] = $current_user->billing_email;
}
return $fields;
}
It works if the billing_email exists in user meta. But if not the field is empty.
There are few logical mistakes in your code, here is cleaned code with explanations:
$fields['billing_email_confirm'] = array(
'label' => 'confirm email *',
'class' => array( 'form-row-last' ),
'priority' => 9999,
);
// Check is customer ID is available or not?
if ( ! empty( $customer_id ) ) {
// Get customer data using customer id.
$customer = get_userdata( $customer_id );
// Get billing email from user meta.
// 3rd param should be true as we need single value not an array.
$billing_email = get_user_meta( $customer_id, 'billing_email', true );
// Check if billing email is empty or not?
if ( ! empty( $billing_email ) ) {
// when billing email is not empty.
$fields['billing_email_confirm']['default'] = $billing_email;
} else {
// When billing email is empty use, user account email.
$fields['billing_email_confirm']['default'] = $customer->user_email;
}
}

How to add UTM parameters to WooCommerce order email?

I'm running some Facebook-Ads for a WooCommerce store and would like to add/pass the UTM parameters (utm_source, utm_medium and utm_campaign) to the WooCommerce order email (for the admin, not the customer). I did not find anything in the Woo documentation nor a plugin that is able to do that, maybe I am missing something.
I found a way how to add a custom field for the email, eg:
/**
* Add a custom field (in an order) to the emails
*/
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['hear_about_us'] = array(
'label' => __( 'Hear About Us' ),
'value' => get_post_meta( $order->id, 'hear_about_us', true ),
);
return $fields;
}
but I don't know how I'd be able to pass the parameter contents. Any help is greatly appreciated!

Populating multiple fields with dynamic user meta

I have a Wordpress site where each user is linked to an 'upline' user, with that upline user's ID stored in a custom meta field 'direct_upline'.
On a particular gravity form, I currently populate a hidden field with value of {user:direct_upline}.
For example, User A fills out the form. User A's direct_upline is User B. I would like to then populate User B's direct_upline, and so on (probable 10 levels deep).
Any idea how to most efficiently perform this? Below is a function to populate multiple fields in one function; I was thinking I may could do this in conjunction with $all_meta_for_user = get_user_meta( $user_id );
add_filter( 'gform_field_value', 'populate_fields', 10, 3 );
function populate_fields( $value, $field, $name ) {
$values = array(
'field_one' => 'value one',
'field_two' => 'value two',
'field_three' => 'value three',
);
return isset( $values[ $name ] ) ? $values[ $name ] : $value;
}

Wordpress: save custom billing field in account Billing address

Using Wordpress/Woocommerce.
I have this code which adds a custom billing field in checkout page called: "NIF/CIF". It works fine but its value it not saved in the customer account "Billing Address" data.
Once the customer makes a first order all billing address values are saved in his account: Address, State, Country, etc. But my custom field is not saved.
I guess that in my function is missing a line of code to save its value in the database, but I don't know how to start with this.
/*******************************
CUSTOM BILLING FIELD
*********************************/
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['nif_cif'] = array(
'label' => __('NIF/CIF', 'woocommerce'), // Add custom field label
'placeholder' => _x('NIF/CIF', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name
);
return $fields;
}
Here is an example of how to save your custom field:
add_action( 'woocommerce_checkout_order_processed', 'prefix_save_field_on_checkout', 11, 2 );
function checkout_order_processed_add_referral_answer( $order_id, $posted ) {
if ( ! isset( $_POST['nif_cif'] ) ) {
return;
}
$order = wc_get_order( $order_id );
// WC < 3.0
update_post_meta( $order->id, 'order_meta_field_name', wc_clean( $_POST['nif_cif'] ) );
// WC > 3.0
$order->add_meta_data( 'order_meta_field_name', wc_clean( $_POST['nif_cif'] ), true );
$order->save();
}
Adding an extra field through 'woocommerce_billing_fields' hook is not enough. you are missing out two things.
Proper validation of NIF/CIF field using
'woocommerce_after_checkout_validation' hook
Saving data in order
using 'woocommerce_checkout_order_processed' hook

How can I show the shipping cost in each product on checkout page. Woocommerce

I need to show shipping cost of each vendor in front of product at checkout page. enter image description here
I tried with
get_post_meta($cart_item['product_id'] , '_wcv_shipping_details', true);
But this return with blank value like:
Array ( [national] => [international] => [handling_fee] => [national_qty_override] => [national_disable] => [national_free] => [international_qty_override] => [international_disable] => [international_free] => )
I think you are trying to access cart object on order details page. Once the order is placed, the cart object will get empty. You will need to get items from the object of order and then fetch shipping cost for each item.
// You can use this piece of code on thank you page (after order is placed)
$order = new WC_Order( $order_id );
$order_item = $order->get_items();
foreach( $order_item as $product ) {
$prodct_name[] = $product['name'];
// here you will get product id and you can use that to get shipping details
get_post_meta($product['product_id'] , '_wcv_shipping_details', true);
}

Resources