• Resolved Chris Whiteley

    (@thewhite)


    I am attempting to modify the provided code that sends the merge tags from Woocommerce to Mailchimp.

    /**
     * @param array $merge_tags
     * @param MailChimp_WooCommerce_Order $order
     * @return array
     */
    function mailchimp_custom_order_merge_tags($merge_tags, $order) {
        /// add whatever you want to the merge tags
        $merge_tags['WHATEVER'] = 'value you want';
    
        return $merge_tags;
    }
    
    add_filter('mailchimp_get_ecommerce_merge_tags', 'mailchimp_custom_order_merge_tags', 10, 2);

    I can get this default version to work but whenever I make modifications such as to send the product name to a merge tag, it does not send.

    I have tested a lot of variations, but this is the version that I feel should be working:

    /**
     * @param array $merge_tags
     * @param MailChimp_WooCommerce_Order $order
     * @return array
     */
    function mailchimp_custom_order_merge_tags($merge_tags, $order) {
        // Retrieve the order items
        $items = $order->get_items();
    
        // Check if there are any items
        if (!empty($items)) {
            // Get the first item
            $first_item = reset($items);
    
            // Retrieve the product title
            $product_title = $first_item->get_name();
    
            // Add the product title as a merge tag
            $merge_tags['COURSENAME'] = $product_title;
        }
    
        return $merge_tags;
    }
    
    add_filter('mailchimp_get_ecommerce_merge_tags', 'mailchimp_custom_order_merge_tags', 10, 2);

    Can I please get a second eyeball on this to figure out what it is missing?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support khungate

    (@khungate)

    Hi @thewhite,

    From the details provided, it seems that you are attempting to modify the code to send product names from WooCommerce to Mailchimp as merge tags. The issue appears to be that while the default version of the function works, modifications to send the product name to a merge tag are not being sent.

    Your approach to retrieve the product name from the first item in the order seems to be correct. However, there could be a few reasons why your code is not working as expected.

    MailChimp_WooCommerce_Order: The $order object in your function is of type MailChimp_WooCommerce_Order. Are you sure that the get_items() method is available and returns the items in the expected format for this object? You might want to check the API documentation or the object methods to verify this. If get_items() is not a method on the MailChimp_WooCommerce_Order object, you will need to retrieve the order items differently.

    Product Name Retrieval: After obtaining the $first_item, you’re using the get_name() function to retrieve the product name. Again, you need to ensure that get_name() is a valid method on the item object you have.

    Filter Execution: Are you sure that the filter mailchimp_get_ecommerce_merge_tags is being executed? You can test this by putting a debug line at the start of your function to see if it gets triggered when you expect it to.

    Merge Tag Acceptance: Mailchimp might not accept or process all data that is sent as merge tags. You may want to confirm that sending a product name as a merge tag is supported.

    Here’s how you might add some debugging to your code:

    function mailchimp_custom_order_merge_tags($merge_tags, $order) { error_log('mailchimp_custom_order_merge_tags called'); // Retrieve the order items $order_id = $order->getId(); $woo_order = wc_get_order($order_id); $items = $woo_order->get_items(); error_log('Order items: ' . print_r($items, true)); // Check if there are any items if (!empty($items)) { // Get the first item $first_item = reset($items); // Retrieve the product title $product_title = $first_item->get_name(); error_log('Product title: ' . $product_title); // Add the product title as a merge tag $merge_tags['COURSENAME'] = $product_title; } return $merge_tags; } add_filter('mailchimp_get_ecommerce_merge_tags', 'mailchimp_custom_order_merge_tags', 10, 2);

    With this it will take the name of the first title, and you can check your PHP error logs to see the output. Be sure to remove or comment out the error_log lines when you’re done debugging.

    Remember that the error_log() function sends an error message to the web server’s error log or a designated file. Make sure your server is configured to handle error logging properly before using this function.

    Let me know if this helps you to troubleshoot the issue or if you have any more questions!

    Thread Starter Chris Whiteley

    (@thewhite)

    Thank you for your reply, I will review and start debugging as per your suggestions

    Regarding your question about the $order object in the function:

    MailChimp_WooCommerce_Order: The $order object in your function is of type MailChimp_WooCommerce_Order. Are you sure that the get_items() method is available and returns the items in the expected format for this object? You might want to check the API documentation or the object methods to verify this. If get_items() is not a method on the MailChimp_WooCommerce_Order object, you will need to retrieve the order items differently.

    I am using the function provided here: https://github.com/mailchimp/mc-woocommerce/wiki/Update-Merge-Tags-During-Order-Submissions

    The page shows you as having edited the page, so I am hoping that perhaps you may be able to let me know if the get_items is available in the $object in the MailChimp_WooCommerce_Order object

    Please do let me know.

    Plugin Support khungate

    (@khungate)

    Hi @thewhite, thanks for your patience and for reaching out. From the code you posted, it appears that you are trying to use the get_items method on the MailChimp_WooCommerce_Order object. However, the MailChimp_WooCommerce_Order object does not have the?get_items?method, or anything similar that you can use in this hook.

    Here’s how you can get the order items:

    $order_id = $order->getId(); $woo_order = wc_get_order($order_id); $items = $woo_order->get_items();

    In this block of code, $order->getId() gets the ID of the order, wc_get_order($order_id) gets the actual WooCommerce order associated with that ID, and $woo_order->get_items() retrieves the items for that order.

    I hope this helps. If you have any more questions or face any more issues, please feel free to ask. Thanks again for your patience.

    Plugin Support khungate

    (@khungate)

    Hi @thewhite, we’re going to close out this ticket for now since it’s been a few weeks since we’ve been in touch.

    Please let us know if you still need any help and we’ll be glad to reopen and troubleshoot further. Please note, that the best way to reach us is over at the GitHub plugin page: https://github.com/mailchimp/mc-woocommerce/. From there, you can receive direct responses from the development team, log new issues, download the latest version, and track existing support tickets.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sending Woocommerce Cart Items As Merge Tags’ is closed to new replies.