0

Due to the fact that we use custom checkouts on our WooCommerce store, Mailchimp official plugin for Woocommerce, is not picking up some of the details on the orders.

The one that we need to pass from each order to Mailchimp is the 2 letter country code, but for some reason i cant make it work.

On one of my tests, i "make it work" as it passed the 2 letter country code, but not the one for the order, but the store one, so I know most of my code is working, but i cant figure out how to make it work as intended:

One is for subscribed people:

function custom_mailchimp_sync_user_mergetags($merge_fields, $user) {
    /// add anything you would like to this array - and return it
    $merge_fields['COUNTRYCOD'] = $order->get_billing_country();
    mailchimp_log('trace', 'custom_fields', $merge_fields); 

    return $merge_fields;

}
add_filter('mailchimp_sync_user_mergetags', 'custom_mailchimp_sync_user_mergetags', 10, 2);

And the other one is for non subscribed people:

function custom_mailchimp_sync_user_mergetags_no_sub($merge_fields, $user) {
    /// add anything you would like to this array - and return it
    $merge_fields['COUNTRYCOD'] = $order->get_billing_country();
    mailchimp_log('trace', 'custom_fields', $merge_fields); 

    return $merge_fields;

}
add_filter('mailchimp_get_ecommerce_merge_tags', 'custom_mailchimp_sync_user_mergetags_no_sub', 10, 2);

I would appreciate any help

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50

1 Answers1

1

You don't have access to the $order object, so $order->get_billing_country(); will not work. However, you have access to the $user object.

So you get:

// Allow users to hook into the merge field submission
function custom_mailchimp_sync_user_mergetags( $merge_fields_system, $user ) {
    // Get userID
    $user_id = $user->ID;

    // Get the WooCommerce customer object
    $customer = new WC_Customer( $user_id );
    
    // Add anything you would like to this array - and return it
    $merge_fields_system['COUNTRYCOD'] = $customer->get_billing_country() ? $customer->get_billing_country() : $customer->get_shipping_country();
    
    //mailchimp_log( 'trace', 'custom_fields', $merge_fields_system ); 

    return $merge_fields_system;
}
add_filter( 'mailchimp_sync_user_mergetags', 'custom_mailchimp_sync_user_mergetags', 10, 2 );

The mailchimp_get_ecommerce_merge_tags filter hook, does contain the $order object as 2nd argument

function filter_mailchimp_get_ecommerce_merge_tags( $merge_fields, $order ) {
    // Do something    

    return $merge_fields;
}
add_filter( 'mailchimp_get_ecommerce_merge_tags', 'filter_mailchimp_get_ecommerce_merge_tags', 10, 2 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • Hi, Thanks for your answer. I have been trying to do some testings during this time, that is why it took a bit longer to reply. I cant seem to be able to send to mail chimp the country code using your code above. I managed to be able to send a string such as ES, but as soon as i use the variable for $customer->get_billing_country() nothing gets sent. Any ideas? – Andres Molina Perez-Tome Jun 08 '21 at 09:15
  • @AndresMolinaPerez-Tome I've updated my answer, if `get_billing_country()` is empty, the `get_shipping_country()` will be used instead. If that doesn't offer a solution, you will have to [debug](https://stackoverflow.com/a/61754061/11987538) step by step, so you can see when things 'go wrong'. Regards – 7uc1f3r Jun 08 '21 at 09:31