Add conditionally a class to cart items and order items in Woocommerce
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
add a comment |
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
add a comment |
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
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
php wordpress woocommerce order cart
edited Nov 11 at 18:19
LoicTheAztec
84.2k136095
84.2k136095
asked Nov 11 at 17:19
Gore
598
598
add a comment |
add a comment |
1 Answer
1
active
oldest
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).
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
add a comment |
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
});
}
});
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%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
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).
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
add a comment |
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).
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
add a comment |
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).
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).
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
add a comment |
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
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%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
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