Add conditionally a class to cart items and order items in Woocommerce












1














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










share|improve this question





























    1














    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










    share|improve this question



























      1












      1








      1







      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










      share|improve this question















      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







      php wordpress woocommerce order cart






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 18:19









      LoicTheAztec

      84.2k136095




      84.2k136095










      asked Nov 11 at 17:19









      Gore

      598




      598
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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).






          share|improve this answer























          • Thanks you! For mail / orders items code works fine (I looked at database and meta_key is exactly _wc_pao_addon_name). But code for cart doesnt work (Isnt it because order is not created yet?). Thanks you
            – Gore
            Nov 11 at 18:33






          • 1




            You are genius as everytime .) Right meta seems to be addon_parent_id. Thanks you very much!
            – Gore
            Nov 12 at 10:19











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251235%2fadd-conditionally-a-class-to-cart-items-and-order-items-in-woocommerce%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          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).






          share|improve this answer























          • Thanks you! For mail / orders items code works fine (I looked at database and meta_key is exactly _wc_pao_addon_name). But code for cart doesnt work (Isnt it because order is not created yet?). Thanks you
            – Gore
            Nov 11 at 18:33






          • 1




            You are genius as everytime .) Right meta seems to be addon_parent_id. Thanks you very much!
            – Gore
            Nov 12 at 10:19
















          1














          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).






          share|improve this answer























          • Thanks you! For mail / orders items code works fine (I looked at database and meta_key is exactly _wc_pao_addon_name). But code for cart doesnt work (Isnt it because order is not created yet?). Thanks you
            – Gore
            Nov 11 at 18:33






          • 1




            You are genius as everytime .) Right meta seems to be addon_parent_id. Thanks you very much!
            – Gore
            Nov 12 at 10:19














          1












          1








          1






          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).






          share|improve this answer














          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).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 10:30

























          answered Nov 11 at 18:07









          LoicTheAztec

          84.2k136095




          84.2k136095












          • Thanks you! For mail / orders items code works fine (I looked at database and meta_key is exactly _wc_pao_addon_name). But code for cart doesnt work (Isnt it because order is not created yet?). Thanks you
            – Gore
            Nov 11 at 18:33






          • 1




            You are genius as everytime .) Right meta seems to be addon_parent_id. Thanks you very much!
            – Gore
            Nov 12 at 10:19


















          • Thanks you! For mail / orders items code works fine (I looked at database and meta_key is exactly _wc_pao_addon_name). But code for cart doesnt work (Isnt it because order is not created yet?). Thanks you
            – Gore
            Nov 11 at 18:33






          • 1




            You are genius as everytime .) Right meta seems to be addon_parent_id. Thanks you very much!
            – Gore
            Nov 12 at 10:19
















          Thanks you! For mail / orders items code works fine (I looked at database and meta_key is exactly _wc_pao_addon_name). But code for cart doesnt work (Isnt it because order is not created yet?). Thanks you
          – Gore
          Nov 11 at 18:33




          Thanks you! For mail / orders items code works fine (I looked at database and meta_key is exactly _wc_pao_addon_name). But code for cart doesnt work (Isnt it because order is not created yet?). Thanks you
          – Gore
          Nov 11 at 18:33




          1




          1




          You are genius as everytime .) Right meta seems to be addon_parent_id. Thanks you very much!
          – Gore
          Nov 12 at 10:19




          You are genius as everytime .) Right meta seems to be addon_parent_id. Thanks you very much!
          – Gore
          Nov 12 at 10:19


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251235%2fadd-conditionally-a-class-to-cart-items-and-order-items-in-woocommerce%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          What is this shape that looks like a rectangle with rounded ends called?