Get product attributes values from cart items in Woocommerce
up vote
1
down vote
favorite
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):
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
add a comment |
up vote
1
down vote
favorite
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):
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
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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):
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
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):
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
php wordpress woocommerce hook-woocommerce custom-taxonomy
edited Nov 24 at 17:23
LoicTheAztec
82.3k125993
82.3k125993
asked Nov 21 at 17:39
Jérémie Czk
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 24 at 17:21
LoicTheAztec
82.3k125993
82.3k125993
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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