Get product attributes values from cart items in Woocommerce





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I need to change the weight of product after adding it to cart. The weight depends on the product attribute "quantite-avec-fenetre".



Based on Change cart items weight to update the shipping costs in Woocommerce, I am trying to use the following function in my theme's functions.php file:



add_action( 'woocommerce_before_calculate_totals', 'add_custom_weight', 10, 1);
function add_custom_weight( $cart_object ) {

foreach ( $cart_object->get_cart() as $cart_item ) {
//very simplified example - every item in cart will be 100 kg
$attr = $cart_item['data']->get_attributes();
if ($cart_item['data']->get_SKU() == "PSP229AFSF") {
$qtySF = $attr["pa_quantite-sans-fenetre"];
$qtyAF = $attr["pa_quantite-avec-fenetre"];
var_dump($attr);
//echo $qtyAF;
//echo $qtySF;

//echo "oll";
}
//echo $attr["pa_impressiont"];
//$cart_item['data']->set_weight( 0.001 );
}
// Testing: cart weight output
}


The echo and things that look strange are just for test.
So, this show with var_dump all attribute of each product of my cart (that have SKU of PSP229AFSF).



The problem is I set the attribute, I add to cart, in the cart I the attribute ith good value, but he var_dump return me this :




array(7) { 
["pa_impression"]=> string(15) "sans-impression"
["pa_impressionrecto"]=> string(11) "1couleurpms"
["pa_impressionverso"]=> string(12) "sansimpverso"
["pa_fichierimpr"]=> string(12) "designertool" ["pa_bat"]=> string(3)
"non" ["pa_quantite-avec-fenetre"]=> string(0) ""
["pa_quantite-sans-fenetre"]=> string(0) "" }



As you can see, the attribute "pa_quantite-avec-fenetre" and "pa_quantite-sans-fenetre" are empty.



This is what my cart show (screenshot of cart):



Image of cart



So the attribute is well set in the cart, but it doesn't seem to be available in $cart_item.



I tried without plugins that can affect attribute, same effect, any idea where this comes from?










share|improve this question































    1















    I need to change the weight of product after adding it to cart. The weight depends on the product attribute "quantite-avec-fenetre".



    Based on Change cart items weight to update the shipping costs in Woocommerce, I am trying to use the following function in my theme's functions.php file:



    add_action( 'woocommerce_before_calculate_totals', 'add_custom_weight', 10, 1);
    function add_custom_weight( $cart_object ) {

    foreach ( $cart_object->get_cart() as $cart_item ) {
    //very simplified example - every item in cart will be 100 kg
    $attr = $cart_item['data']->get_attributes();
    if ($cart_item['data']->get_SKU() == "PSP229AFSF") {
    $qtySF = $attr["pa_quantite-sans-fenetre"];
    $qtyAF = $attr["pa_quantite-avec-fenetre"];
    var_dump($attr);
    //echo $qtyAF;
    //echo $qtySF;

    //echo "oll";
    }
    //echo $attr["pa_impressiont"];
    //$cart_item['data']->set_weight( 0.001 );
    }
    // Testing: cart weight output
    }


    The echo and things that look strange are just for test.
    So, this show with var_dump all attribute of each product of my cart (that have SKU of PSP229AFSF).



    The problem is I set the attribute, I add to cart, in the cart I the attribute ith good value, but he var_dump return me this :




    array(7) { 
    ["pa_impression"]=> string(15) "sans-impression"
    ["pa_impressionrecto"]=> string(11) "1couleurpms"
    ["pa_impressionverso"]=> string(12) "sansimpverso"
    ["pa_fichierimpr"]=> string(12) "designertool" ["pa_bat"]=> string(3)
    "non" ["pa_quantite-avec-fenetre"]=> string(0) ""
    ["pa_quantite-sans-fenetre"]=> string(0) "" }



    As you can see, the attribute "pa_quantite-avec-fenetre" and "pa_quantite-sans-fenetre" are empty.



    This is what my cart show (screenshot of cart):



    Image of cart



    So the attribute is well set in the cart, but it doesn't seem to be available in $cart_item.



    I tried without plugins that can affect attribute, same effect, any idea where this comes from?










    share|improve this question



























      1












      1








      1








      I need to change the weight of product after adding it to cart. The weight depends on the product attribute "quantite-avec-fenetre".



      Based on Change cart items weight to update the shipping costs in Woocommerce, I am trying to use the following function in my theme's functions.php file:



      add_action( 'woocommerce_before_calculate_totals', 'add_custom_weight', 10, 1);
      function add_custom_weight( $cart_object ) {

      foreach ( $cart_object->get_cart() as $cart_item ) {
      //very simplified example - every item in cart will be 100 kg
      $attr = $cart_item['data']->get_attributes();
      if ($cart_item['data']->get_SKU() == "PSP229AFSF") {
      $qtySF = $attr["pa_quantite-sans-fenetre"];
      $qtyAF = $attr["pa_quantite-avec-fenetre"];
      var_dump($attr);
      //echo $qtyAF;
      //echo $qtySF;

      //echo "oll";
      }
      //echo $attr["pa_impressiont"];
      //$cart_item['data']->set_weight( 0.001 );
      }
      // Testing: cart weight output
      }


      The echo and things that look strange are just for test.
      So, this show with var_dump all attribute of each product of my cart (that have SKU of PSP229AFSF).



      The problem is I set the attribute, I add to cart, in the cart I the attribute ith good value, but he var_dump return me this :




      array(7) { 
      ["pa_impression"]=> string(15) "sans-impression"
      ["pa_impressionrecto"]=> string(11) "1couleurpms"
      ["pa_impressionverso"]=> string(12) "sansimpverso"
      ["pa_fichierimpr"]=> string(12) "designertool" ["pa_bat"]=> string(3)
      "non" ["pa_quantite-avec-fenetre"]=> string(0) ""
      ["pa_quantite-sans-fenetre"]=> string(0) "" }



      As you can see, the attribute "pa_quantite-avec-fenetre" and "pa_quantite-sans-fenetre" are empty.



      This is what my cart show (screenshot of cart):



      Image of cart



      So the attribute is well set in the cart, but it doesn't seem to be available in $cart_item.



      I tried without plugins that can affect attribute, same effect, any idea where this comes from?










      share|improve this question
















      I need to change the weight of product after adding it to cart. The weight depends on the product attribute "quantite-avec-fenetre".



      Based on Change cart items weight to update the shipping costs in Woocommerce, I am trying to use the following function in my theme's functions.php file:



      add_action( 'woocommerce_before_calculate_totals', 'add_custom_weight', 10, 1);
      function add_custom_weight( $cart_object ) {

      foreach ( $cart_object->get_cart() as $cart_item ) {
      //very simplified example - every item in cart will be 100 kg
      $attr = $cart_item['data']->get_attributes();
      if ($cart_item['data']->get_SKU() == "PSP229AFSF") {
      $qtySF = $attr["pa_quantite-sans-fenetre"];
      $qtyAF = $attr["pa_quantite-avec-fenetre"];
      var_dump($attr);
      //echo $qtyAF;
      //echo $qtySF;

      //echo "oll";
      }
      //echo $attr["pa_impressiont"];
      //$cart_item['data']->set_weight( 0.001 );
      }
      // Testing: cart weight output
      }


      The echo and things that look strange are just for test.
      So, this show with var_dump all attribute of each product of my cart (that have SKU of PSP229AFSF).



      The problem is I set the attribute, I add to cart, in the cart I the attribute ith good value, but he var_dump return me this :




      array(7) { 
      ["pa_impression"]=> string(15) "sans-impression"
      ["pa_impressionrecto"]=> string(11) "1couleurpms"
      ["pa_impressionverso"]=> string(12) "sansimpverso"
      ["pa_fichierimpr"]=> string(12) "designertool" ["pa_bat"]=> string(3)
      "non" ["pa_quantite-avec-fenetre"]=> string(0) ""
      ["pa_quantite-sans-fenetre"]=> string(0) "" }



      As you can see, the attribute "pa_quantite-avec-fenetre" and "pa_quantite-sans-fenetre" are empty.



      This is what my cart show (screenshot of cart):



      Image of cart



      So the attribute is well set in the cart, but it doesn't seem to be available in $cart_item.



      I tried without plugins that can affect attribute, same effect, any idea where this comes from?







      php wordpress woocommerce hook-woocommerce custom-taxonomy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 17:23









      LoicTheAztec

      98.4k1472114




      98.4k1472114










      asked Nov 21 '18 at 17:39









      Jérémie CzkJérémie Czk

      61




      61
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You need to get your variation product attribute data from the cart item (and not from the product itself).



          So try this:



          add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_weight', 10, 1);
          function custom_cart_item_weight( $cart ) {
          if ( is_admin() && ! defined( 'DOING_AJAX' ) )
          return;

          if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
          return;

          foreach ( $cart_object->get_cart() as $cart_item ) {
          if ( $cart_item['data']->get_sku() == "PSP229AFSF" ) {
          $attributes = $cart_item['variation'];
          $attr_key = 'attribute_pa_';

          if( isset($attributes[$attr_key.'impression']) )
          $attr_imp = $attributes[$attr_key.'impression'];

          if( isset($attributes[$attr_key.'impressionrecto']) )
          $attr_imp_rec = $attributes[$attr_key.'impressionrecto'];

          if( isset($attributes[$attr_key.'impressionverso']) )
          $attr_imp_ver = $attributes[$attr_key.'impressionverso'];

          if( isset($attributes[$attr_key.'fichierimpr']) )
          $attr_fich_imp = $attributes[$attr_key.'fichierimpr'];

          if( isset($attributes[$attr_key.'bat']) )
          $attr_bat = $attributes[$attr_key.'bat'];

          if( isset($attributes[$attr_key.'quantite-sans-fenetre']) )
          $attr_qte_sf = $attributes[$attr_key.'quantite-sans-fenetre'];

          if( isset($attributes[$attr_key.'quantite-avec-fenetre']) )
          $attr_qte_af = $attributes[$attr_key.'quantite-avec-fenetre'];

          ## Make your weight calculations ##

          $new_weight = 1; // <=== Make your calculated weight

          $cart_item['data']->set_weight( $new_weight ); // Set the calculated weight
          }
          }
          }


          Code goes in function.php file of your active child theme (or active theme). It should better works.






          share|improve this answer
























            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%2f53417765%2fget-product-attributes-values-from-cart-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









            0














            You need to get your variation product attribute data from the cart item (and not from the product itself).



            So try this:



            add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_weight', 10, 1);
            function custom_cart_item_weight( $cart ) {
            if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;

            if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;

            foreach ( $cart_object->get_cart() as $cart_item ) {
            if ( $cart_item['data']->get_sku() == "PSP229AFSF" ) {
            $attributes = $cart_item['variation'];
            $attr_key = 'attribute_pa_';

            if( isset($attributes[$attr_key.'impression']) )
            $attr_imp = $attributes[$attr_key.'impression'];

            if( isset($attributes[$attr_key.'impressionrecto']) )
            $attr_imp_rec = $attributes[$attr_key.'impressionrecto'];

            if( isset($attributes[$attr_key.'impressionverso']) )
            $attr_imp_ver = $attributes[$attr_key.'impressionverso'];

            if( isset($attributes[$attr_key.'fichierimpr']) )
            $attr_fich_imp = $attributes[$attr_key.'fichierimpr'];

            if( isset($attributes[$attr_key.'bat']) )
            $attr_bat = $attributes[$attr_key.'bat'];

            if( isset($attributes[$attr_key.'quantite-sans-fenetre']) )
            $attr_qte_sf = $attributes[$attr_key.'quantite-sans-fenetre'];

            if( isset($attributes[$attr_key.'quantite-avec-fenetre']) )
            $attr_qte_af = $attributes[$attr_key.'quantite-avec-fenetre'];

            ## Make your weight calculations ##

            $new_weight = 1; // <=== Make your calculated weight

            $cart_item['data']->set_weight( $new_weight ); // Set the calculated weight
            }
            }
            }


            Code goes in function.php file of your active child theme (or active theme). It should better works.






            share|improve this answer




























              0














              You need to get your variation product attribute data from the cart item (and not from the product itself).



              So try this:



              add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_weight', 10, 1);
              function custom_cart_item_weight( $cart ) {
              if ( is_admin() && ! defined( 'DOING_AJAX' ) )
              return;

              if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
              return;

              foreach ( $cart_object->get_cart() as $cart_item ) {
              if ( $cart_item['data']->get_sku() == "PSP229AFSF" ) {
              $attributes = $cart_item['variation'];
              $attr_key = 'attribute_pa_';

              if( isset($attributes[$attr_key.'impression']) )
              $attr_imp = $attributes[$attr_key.'impression'];

              if( isset($attributes[$attr_key.'impressionrecto']) )
              $attr_imp_rec = $attributes[$attr_key.'impressionrecto'];

              if( isset($attributes[$attr_key.'impressionverso']) )
              $attr_imp_ver = $attributes[$attr_key.'impressionverso'];

              if( isset($attributes[$attr_key.'fichierimpr']) )
              $attr_fich_imp = $attributes[$attr_key.'fichierimpr'];

              if( isset($attributes[$attr_key.'bat']) )
              $attr_bat = $attributes[$attr_key.'bat'];

              if( isset($attributes[$attr_key.'quantite-sans-fenetre']) )
              $attr_qte_sf = $attributes[$attr_key.'quantite-sans-fenetre'];

              if( isset($attributes[$attr_key.'quantite-avec-fenetre']) )
              $attr_qte_af = $attributes[$attr_key.'quantite-avec-fenetre'];

              ## Make your weight calculations ##

              $new_weight = 1; // <=== Make your calculated weight

              $cart_item['data']->set_weight( $new_weight ); // Set the calculated weight
              }
              }
              }


              Code goes in function.php file of your active child theme (or active theme). It should better works.






              share|improve this answer


























                0












                0








                0







                You need to get your variation product attribute data from the cart item (and not from the product itself).



                So try this:



                add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_weight', 10, 1);
                function custom_cart_item_weight( $cart ) {
                if ( is_admin() && ! defined( 'DOING_AJAX' ) )
                return;

                if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
                return;

                foreach ( $cart_object->get_cart() as $cart_item ) {
                if ( $cart_item['data']->get_sku() == "PSP229AFSF" ) {
                $attributes = $cart_item['variation'];
                $attr_key = 'attribute_pa_';

                if( isset($attributes[$attr_key.'impression']) )
                $attr_imp = $attributes[$attr_key.'impression'];

                if( isset($attributes[$attr_key.'impressionrecto']) )
                $attr_imp_rec = $attributes[$attr_key.'impressionrecto'];

                if( isset($attributes[$attr_key.'impressionverso']) )
                $attr_imp_ver = $attributes[$attr_key.'impressionverso'];

                if( isset($attributes[$attr_key.'fichierimpr']) )
                $attr_fich_imp = $attributes[$attr_key.'fichierimpr'];

                if( isset($attributes[$attr_key.'bat']) )
                $attr_bat = $attributes[$attr_key.'bat'];

                if( isset($attributes[$attr_key.'quantite-sans-fenetre']) )
                $attr_qte_sf = $attributes[$attr_key.'quantite-sans-fenetre'];

                if( isset($attributes[$attr_key.'quantite-avec-fenetre']) )
                $attr_qte_af = $attributes[$attr_key.'quantite-avec-fenetre'];

                ## Make your weight calculations ##

                $new_weight = 1; // <=== Make your calculated weight

                $cart_item['data']->set_weight( $new_weight ); // Set the calculated weight
                }
                }
                }


                Code goes in function.php file of your active child theme (or active theme). It should better works.






                share|improve this answer













                You need to get your variation product attribute data from the cart item (and not from the product itself).



                So try this:



                add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_weight', 10, 1);
                function custom_cart_item_weight( $cart ) {
                if ( is_admin() && ! defined( 'DOING_AJAX' ) )
                return;

                if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
                return;

                foreach ( $cart_object->get_cart() as $cart_item ) {
                if ( $cart_item['data']->get_sku() == "PSP229AFSF" ) {
                $attributes = $cart_item['variation'];
                $attr_key = 'attribute_pa_';

                if( isset($attributes[$attr_key.'impression']) )
                $attr_imp = $attributes[$attr_key.'impression'];

                if( isset($attributes[$attr_key.'impressionrecto']) )
                $attr_imp_rec = $attributes[$attr_key.'impressionrecto'];

                if( isset($attributes[$attr_key.'impressionverso']) )
                $attr_imp_ver = $attributes[$attr_key.'impressionverso'];

                if( isset($attributes[$attr_key.'fichierimpr']) )
                $attr_fich_imp = $attributes[$attr_key.'fichierimpr'];

                if( isset($attributes[$attr_key.'bat']) )
                $attr_bat = $attributes[$attr_key.'bat'];

                if( isset($attributes[$attr_key.'quantite-sans-fenetre']) )
                $attr_qte_sf = $attributes[$attr_key.'quantite-sans-fenetre'];

                if( isset($attributes[$attr_key.'quantite-avec-fenetre']) )
                $attr_qte_af = $attributes[$attr_key.'quantite-avec-fenetre'];

                ## Make your weight calculations ##

                $new_weight = 1; // <=== Make your calculated weight

                $cart_item['data']->set_weight( $new_weight ); // Set the calculated weight
                }
                }
                }


                Code goes in function.php file of your active child theme (or active theme). It should better works.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 17:21









                LoicTheAztecLoicTheAztec

                98.4k1472114




                98.4k1472114
































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417765%2fget-product-attributes-values-from-cart-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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini