Quantity Discount for 2nd Item Based on Product Category in Woocommerce
I got the initial question answered here.
I would like help on how to add a if statement for has_term
and that way, make this code applicable only for one or more categories.
Here is the code from LoicTheAztec:
add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // 20%
$discount = 0;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// When quantity is more than 1
if( $cart_item['quantity'] > 1 ){
// 20% of the product price as a discount
$discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}
php wordpress woocommerce cart custom-taxonomy
add a comment |
I got the initial question answered here.
I would like help on how to add a if statement for has_term
and that way, make this code applicable only for one or more categories.
Here is the code from LoicTheAztec:
add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // 20%
$discount = 0;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// When quantity is more than 1
if( $cart_item['quantity'] > 1 ){
// 20% of the product price as a discount
$discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}
php wordpress woocommerce cart custom-taxonomy
So what is the problem or error you got?
– Chris Chen
Jan 19 at 21:08
I tried adding an term statement like this:if ( has_term( array('cat1', 'cat2'), 'product_cat', $product->get_id() ) ) {
but it crashed the page.
– user10913018
Jan 19 at 21:16
add a comment |
I got the initial question answered here.
I would like help on how to add a if statement for has_term
and that way, make this code applicable only for one or more categories.
Here is the code from LoicTheAztec:
add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // 20%
$discount = 0;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// When quantity is more than 1
if( $cart_item['quantity'] > 1 ){
// 20% of the product price as a discount
$discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}
php wordpress woocommerce cart custom-taxonomy
I got the initial question answered here.
I would like help on how to add a if statement for has_term
and that way, make this code applicable only for one or more categories.
Here is the code from LoicTheAztec:
add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // 20%
$discount = 0;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// When quantity is more than 1
if( $cart_item['quantity'] > 1 ){
// 20% of the product price as a discount
$discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}
php wordpress woocommerce cart custom-taxonomy
php wordpress woocommerce cart custom-taxonomy
edited Jan 19 at 21:33
LoicTheAztec
87.6k1364101
87.6k1364101
asked Jan 19 at 21:02
user10913018
So what is the problem or error you got?
– Chris Chen
Jan 19 at 21:08
I tried adding an term statement like this:if ( has_term( array('cat1', 'cat2'), 'product_cat', $product->get_id() ) ) {
but it crashed the page.
– user10913018
Jan 19 at 21:16
add a comment |
So what is the problem or error you got?
– Chris Chen
Jan 19 at 21:08
I tried adding an term statement like this:if ( has_term( array('cat1', 'cat2'), 'product_cat', $product->get_id() ) ) {
but it crashed the page.
– user10913018
Jan 19 at 21:16
So what is the problem or error you got?
– Chris Chen
Jan 19 at 21:08
So what is the problem or error you got?
– Chris Chen
Jan 19 at 21:08
I tried adding an term statement like this:
if ( has_term( array('cat1', 'cat2'), 'product_cat', $product->get_id() ) ) {
but it crashed the page.– user10913018
Jan 19 at 21:16
I tried adding an term statement like this:
if ( has_term( array('cat1', 'cat2'), 'product_cat', $product->get_id() ) ) {
but it crashed the page.– user10913018
Jan 19 at 21:16
add a comment |
1 Answer
1
active
oldest
votes
The following will extend your code for defined product categories using WordPress has_tem()
conditional function:
add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // 20%
$categories = array('cat1', 'cat2'); // Defined product categories term slugs
$discount = 0;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// When quantity is more than 1 and for defined product categories
if( $cart_item['quantity'] > 1 && has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
// 20% of the product price as a discount
$discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
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%2f54271364%2fquantity-discount-for-2nd-item-based-on-product-category-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
The following will extend your code for defined product categories using WordPress has_tem()
conditional function:
add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // 20%
$categories = array('cat1', 'cat2'); // Defined product categories term slugs
$discount = 0;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// When quantity is more than 1 and for defined product categories
if( $cart_item['quantity'] > 1 && has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
// 20% of the product price as a discount
$discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
add a comment |
The following will extend your code for defined product categories using WordPress has_tem()
conditional function:
add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // 20%
$categories = array('cat1', 'cat2'); // Defined product categories term slugs
$discount = 0;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// When quantity is more than 1 and for defined product categories
if( $cart_item['quantity'] > 1 && has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
// 20% of the product price as a discount
$discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
add a comment |
The following will extend your code for defined product categories using WordPress has_tem()
conditional function:
add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // 20%
$categories = array('cat1', 'cat2'); // Defined product categories term slugs
$discount = 0;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// When quantity is more than 1 and for defined product categories
if( $cart_item['quantity'] > 1 && has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
// 20% of the product price as a discount
$discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
The following will extend your code for defined product categories using WordPress has_tem()
conditional function:
add_action( 'woocommerce_cart_calculate_fees', 'second_item_discount', 10, 1 );
function second_item_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 20; // 20%
$categories = array('cat1', 'cat2'); // Defined product categories term slugs
$discount = 0;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// When quantity is more than 1 and for defined product categories
if( $cart_item['quantity'] > 1 && has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
// 20% of the product price as a discount
$discount += wc_get_price_excluding_tax( $cart_item['data'] ) * $percentage / 100;
}
}
if( $discount > 0 )
$cart->add_fee( __( '2nd item discount', 'woocommerce' ) , -$discount );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
answered Jan 19 at 21:31
LoicTheAztecLoicTheAztec
87.6k1364101
87.6k1364101
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.
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%2f54271364%2fquantity-discount-for-2nd-item-based-on-product-category-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
So what is the problem or error you got?
– Chris Chen
Jan 19 at 21:08
I tried adding an term statement like this:
if ( has_term( array('cat1', 'cat2'), 'product_cat', $product->get_id() ) ) {
but it crashed the page.– user10913018
Jan 19 at 21:16