• Resolved booprodev

    (@booprodev)


    Hi guys, I’m having a problem adding WooCommerce Product Attributes to a Merge Tag.

    For Example: I have 1 Product (WooCommerce Subscription) which has 2 variations:
    1. Duration(6 months, 1 year, lifetime)
    &
    2. Type(Couple, Etudiant, Normal)
    both has 3 attributes as above.

    I am trying to add something here but not sure how to make it work, any help is welcome, thanks ahead.

    function product_mailchimp_tag($tags, $email)
    {
      $new_tags = [];
      $query = new WC_Order_Query();
      $query->set('customer', $email);
      $customer_orders = $query->get_orders();
    
      if ($customer_orders) {
        $last_order = $customer_orders[0];
        $products = $last_order->get_items();    
    
        foreach ($products as $product) {
            // Get the product attribute first value.
            $duration = $product->get_attribute('pa_plan-duration');
    		$product_tags = [
            'name' => strpos($duration),
            'status' => 'active'
          ];
          array_push($new_tags, $product_tags);
        }
      }
    
      return array_merge($tags, $new_tags);
    
    add_filter('mailchimp_user_tags', 'product_mailchimp_tag', 10, 2);
    

    This is for “Duration” and I’m not sure it works well. How can I add both Duration and Type and insert 1 of 3 values that people choose under product variations into merge-tags and insert them into my audience?

    cheers

    • This topic was modified 1 year, 3 months ago by booprodev.
    • This topic was modified 1 year, 3 months ago by booprodev.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support khungate

    (@khungate)

    Hi @booprodev,

    Thank you for reaching out with your query regarding the integration of WooCommerce product attributes into merge tags. The code you’ve shared is a solid foundation for such an integration. It correctly retrieves order details and iterates through the products to extract their meta data.

    However, this kind of customization can be quite complex and specific to your WooCommerce setup. It’s important to consider engaging a professional WordPress developer, especially if the requirements are extensive or if the integration needs to be robust and compatible with other plugins or themes. A skilled developer can provide a tailored solution, ensuring smooth integration and future compatibility.

    For those who are comfortable with code and would like to attempt a basic integration themselves, here’s an enhanced version of your snippet that could serve as a starting point:

    add_action('your_custom_action', function ($formatted_tags, $order, $args) {
        if (!$order) {
            return $formatted_tags;
        }
    
        $wc_order = wc_get_order($order->getId());
        $order_products = $wc_order->get_items();
        $product_tags = [];
    
        foreach ($order_products as $order_product) {
            $product = $order_product->get_product();
            $attributes = $product->get_attributes();
    
            foreach ($attributes as $attr_name => $attr) {
                if ($attr->is_taxonomy()) {
                    $term_names = wc_get_product_terms($product->get_id(), $attr_name, ['fields' => 'names']);
                    $value = join(', ', $term_names);
                } else {
                    $value = $attr;
                }
    
                $product_tags[] = [
                    'name' => "{$attr_name} - {$value}",
                    'status' => 'active'
                ];
            }
        }
    
        return array_merge($formatted_tags, $product_tags);
    }, 10, 3);

    This modified code snippet aims to more directly access product attributes and handles both taxonomy-based and custom attributes. It should provide a more streamlined way to merge product attributes into tags.

    Please remember, while this code may serve as a starting point, for a full-fledged, reliable solution that fits seamlessly into your specific setup, consulting a professional developer is highly advisable. You can find qualified developers through www.remarpro.com’s Job Board, freelance platforms like Upwork or Freelancer, or through specialized WordPress development agencies.

    I hope this helps you get started and guides you in the right direction for your project!

    Thread Starter booprodev

    (@booprodev)

    Thanks for the explanation and your code improvement as well as an advice to hire a professional developer. This last will not be the case as I’m trying to learn as much as possible and I’m dedicated to it in last few months as I have very good results by now, started to learning WP and php few years back but at the moment I’m trying to get as much experience as possible and I rarely share any problems to communities like this as I’m trying to resolve myself even if they last for few days sometimes – I resolve them every time.

    I’m IT professional from IT Security branch who is trying to learn more advanced programming and development in order to wide-up my skill-set and fulfill my client needs as in last few years more and more of them are using CMS like WordPress so I’m adopting myself and my skill-set to their needs and also expanding my knowledge in so many ways.

    This is why I do not want to hire any professional to do something that I am trying to learn myself and I think I’m on the good path to accomplish it in the near future.

    Thanks anyways for your advices and many thanks for your code explanation and improvement. I will try to adapt it for my needs and see if the result will be what I need and get back to you tomorrow with the results.

    Mickey

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WC Product Attributes to Merge Tag’ is closed to new replies.