2
votes

I would like to style Woocommerce cart and order items differently when the related product uses Woocommerce Product add-ons 3+ plugin custom fields, so I need add class conditionally (to cart items and order items).

But I can not get it work, using _wc_pao_addon_name meta key to work with. Here is my code:

  add_filter( 'woocommerce_order_item_class', 'add_product_addon_classes', 10, 4 );
  add_filter( 'woocommerce_cart_item_class', 'add_cart_addon_classes', 10, 4 );

  function add_cart_addon_classes ( $class, $cart_item, $values  ) {

    if ( isset( $values['_wc_pao_addon_name'] ) ) {
        $class = $class . ' fl-addon-item';
    }

    return $class;
}

What I am doing wrong?

Related: Pricing options, image swatches, and more have arrived in Product Add-Ons 3.0

1

1 Answers

2
votes

You can't make one function that works for both hook, as they have different hook arguments:

  • The hook woocommerce_cart_item_class uses $class, $cart_item and $cart_item_key arguments
  • The hook woocommerce_order_item_class uses $class, $item and $order arguments

As you see your $values['_wc_pao_addon_name'] can't work in your code.

1) Cart items:

To check and get the correct meta_key used by Woocommerce Product add-ons plugin, you will use first the testing hooked function added at the end of this answer…

Once you will find the right meta_key you will replace in the following code addon_parent_id by the right meta_key:

add_filter( 'woocommerce_cart_item_class', 'additional_class_to_cart_item_classes', 10, 3 );
function additional_class_to_cart_item_classes ( $class, $cart_item, $cart_item_key ) {
    if ( isset( $cart_item['addon_parent_id'] ) ) {
        $class .= ' fl-addon-item';
    }

    return $class;
}

Code goes in function.php file of your active child theme (active theme). It should work for cart and checkout.


2) For orders items (Order received, order view, Order pay and email notifications):

It's more complicated as you need to know how _wc_pao_addon_name is saved in order items. For that you will need to take a look in your database table wp_woocommerce_order_itemmeta searching for a meta_key that match with _wc_pao_addon_name.

Once you will find the right meta_key you will replace in the following code _wc_pao_addon_name by the right meta_key:

add_filter( 'woocommerce_order_item_class', 'additional_class_to_order_item_classes', 10, 3 );
function additional_class_to_order_item_classes ( $class, $item, $order ) {
    if ( $item->get_meta('_wc_pao_addon_name') ) {
        $class .= ' fl-addon-item';
    }

    return $class;
}

Code goes in function.php file of your active child theme (active theme). It should work for order items.


Check and get the correct cart custom metadata (testing only):

To find out in cart object what is the correct meta key for custom meta data added by plugins as Woocommerce Product add-ons, you will use the following (that will display in cart page the cart items raw data):

// Testing and getting cart item raw data
add_action( 'woocommerce_before_cart', function(){
    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item) {
        // Output cart item raw data
        echo '<pre>'; print_r($cart_item); echo '</pre>';
    }
}, 987 );

Code goes in function.php file of your active child theme (active theme).