WooCommerce Add Fee If Coupon Applied











up vote
-1
down vote

favorite












I'd like to be able to add a fixed $20 fee if a given coupon code (100% off) is applied to the cart. The fee should also not have taxes applied. I'm offering a try at home service for $20, so a certain coupon would discount the cart and all products by 100% but add a $20 fixed fee. Any help is much appreciated!



JC










share|improve this question
























  • For your information fee lines in Woocommerce are always added by default below shipping lines and there is no other way when adding a fee to Woocommerce. You can change that behavior only if you customize the related templates (which is another question).
    – LoicTheAztec
    Nov 12 at 1:25












  • That would be great but the code your provided added $20 to the Shipping line not a Fee line.
    – jcooksey80
    Nov 12 at 1:25










  • Sorry but I am using WC_Cart add_fee() method, which adds a FEE and NOT a SHIPPING METHOD… There is no other ways or methods to add a fee. The display problem that you have is due to your theme, a customization that you have made or a third party plugin. But it's not due to my code.
    – LoicTheAztec
    Nov 12 at 1:28

















up vote
-1
down vote

favorite












I'd like to be able to add a fixed $20 fee if a given coupon code (100% off) is applied to the cart. The fee should also not have taxes applied. I'm offering a try at home service for $20, so a certain coupon would discount the cart and all products by 100% but add a $20 fixed fee. Any help is much appreciated!



JC










share|improve this question
























  • For your information fee lines in Woocommerce are always added by default below shipping lines and there is no other way when adding a fee to Woocommerce. You can change that behavior only if you customize the related templates (which is another question).
    – LoicTheAztec
    Nov 12 at 1:25












  • That would be great but the code your provided added $20 to the Shipping line not a Fee line.
    – jcooksey80
    Nov 12 at 1:25










  • Sorry but I am using WC_Cart add_fee() method, which adds a FEE and NOT a SHIPPING METHOD… There is no other ways or methods to add a fee. The display problem that you have is due to your theme, a customization that you have made or a third party plugin. But it's not due to my code.
    – LoicTheAztec
    Nov 12 at 1:28















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'd like to be able to add a fixed $20 fee if a given coupon code (100% off) is applied to the cart. The fee should also not have taxes applied. I'm offering a try at home service for $20, so a certain coupon would discount the cart and all products by 100% but add a $20 fixed fee. Any help is much appreciated!



JC










share|improve this question















I'd like to be able to add a fixed $20 fee if a given coupon code (100% off) is applied to the cart. The fee should also not have taxes applied. I'm offering a try at home service for $20, so a certain coupon would discount the cart and all products by 100% but add a $20 fixed fee. Any help is much appreciated!



JC







woocommerce coupon fee






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 1:19









LoicTheAztec

82.3k125993




82.3k125993










asked Nov 11 at 19:36









jcooksey80

11




11












  • For your information fee lines in Woocommerce are always added by default below shipping lines and there is no other way when adding a fee to Woocommerce. You can change that behavior only if you customize the related templates (which is another question).
    – LoicTheAztec
    Nov 12 at 1:25












  • That would be great but the code your provided added $20 to the Shipping line not a Fee line.
    – jcooksey80
    Nov 12 at 1:25










  • Sorry but I am using WC_Cart add_fee() method, which adds a FEE and NOT a SHIPPING METHOD… There is no other ways or methods to add a fee. The display problem that you have is due to your theme, a customization that you have made or a third party plugin. But it's not due to my code.
    – LoicTheAztec
    Nov 12 at 1:28




















  • For your information fee lines in Woocommerce are always added by default below shipping lines and there is no other way when adding a fee to Woocommerce. You can change that behavior only if you customize the related templates (which is another question).
    – LoicTheAztec
    Nov 12 at 1:25












  • That would be great but the code your provided added $20 to the Shipping line not a Fee line.
    – jcooksey80
    Nov 12 at 1:25










  • Sorry but I am using WC_Cart add_fee() method, which adds a FEE and NOT a SHIPPING METHOD… There is no other ways or methods to add a fee. The display problem that you have is due to your theme, a customization that you have made or a third party plugin. But it's not due to my code.
    – LoicTheAztec
    Nov 12 at 1:28


















For your information fee lines in Woocommerce are always added by default below shipping lines and there is no other way when adding a fee to Woocommerce. You can change that behavior only if you customize the related templates (which is another question).
– LoicTheAztec
Nov 12 at 1:25






For your information fee lines in Woocommerce are always added by default below shipping lines and there is no other way when adding a fee to Woocommerce. You can change that behavior only if you customize the related templates (which is another question).
– LoicTheAztec
Nov 12 at 1:25














That would be great but the code your provided added $20 to the Shipping line not a Fee line.
– jcooksey80
Nov 12 at 1:25




That would be great but the code your provided added $20 to the Shipping line not a Fee line.
– jcooksey80
Nov 12 at 1:25












Sorry but I am using WC_Cart add_fee() method, which adds a FEE and NOT a SHIPPING METHOD… There is no other ways or methods to add a fee. The display problem that you have is due to your theme, a customization that you have made or a third party plugin. But it's not due to my code.
– LoicTheAztec
Nov 12 at 1:28






Sorry but I am using WC_Cart add_fee() method, which adds a FEE and NOT a SHIPPING METHOD… There is no other ways or methods to add a fee. The display problem that you have is due to your theme, a customization that you have made or a third party plugin. But it's not due to my code.
– LoicTheAztec
Nov 12 at 1:28














1 Answer
1






active

oldest

votes

















up vote
0
down vote













The following code will apply a fee of $20 if a specific coupon code has been applied to cart:



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

// HERE set your targeted coupon code (100 % off)
$coupon_code = 'thatsforfree';

// Check if our targeted coupon is applied
if( in_array( wc_format_coupon_code( $coupon_code ), $cart->get_applied_coupons() ) ){
$title = __('Fee', 'woocommerce'); // The fee title
$cost = 20; // The fee amount

// Adding the fee (not taxable)
$cart->add_fee( $title, $cost, false );
}
}


Code goes in function.php file of your active child theme (active theme). Tested and works.



The display in cart and checkout pages (on Woocommerce storefront theme):



enter image description here




This code uses The Woocommerce FEE API using WC_Cart method add_fee() with the dedicated action hook woocommerce_cart_calculate_fees.







share|improve this answer























  • Thanks so much for the quick response! The code is adding a $20 fee, however, it is adding it on the Shipping line as a Flat Rate. Is there a way to make this show as a separate line with the fee title? Thanks!
    – jcooksey80
    Nov 11 at 22:45










  • So there is no way to create a new line? This will give customers the impression they are paying for shipping, when shipping is to be free. A bit confusing. Any workarounds for a new line? Thanks!
    – jcooksey80
    Nov 11 at 22:53










  • I would accept the answer, but this code did not result in what I wanted. It added the fee in the shipping line.
    – jcooksey80
    Nov 12 at 1:13











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',
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%2f53252446%2fwoocommerce-add-fee-if-coupon-applied%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








up vote
0
down vote













The following code will apply a fee of $20 if a specific coupon code has been applied to cart:



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

// HERE set your targeted coupon code (100 % off)
$coupon_code = 'thatsforfree';

// Check if our targeted coupon is applied
if( in_array( wc_format_coupon_code( $coupon_code ), $cart->get_applied_coupons() ) ){
$title = __('Fee', 'woocommerce'); // The fee title
$cost = 20; // The fee amount

// Adding the fee (not taxable)
$cart->add_fee( $title, $cost, false );
}
}


Code goes in function.php file of your active child theme (active theme). Tested and works.



The display in cart and checkout pages (on Woocommerce storefront theme):



enter image description here




This code uses The Woocommerce FEE API using WC_Cart method add_fee() with the dedicated action hook woocommerce_cart_calculate_fees.







share|improve this answer























  • Thanks so much for the quick response! The code is adding a $20 fee, however, it is adding it on the Shipping line as a Flat Rate. Is there a way to make this show as a separate line with the fee title? Thanks!
    – jcooksey80
    Nov 11 at 22:45










  • So there is no way to create a new line? This will give customers the impression they are paying for shipping, when shipping is to be free. A bit confusing. Any workarounds for a new line? Thanks!
    – jcooksey80
    Nov 11 at 22:53










  • I would accept the answer, but this code did not result in what I wanted. It added the fee in the shipping line.
    – jcooksey80
    Nov 12 at 1:13















up vote
0
down vote













The following code will apply a fee of $20 if a specific coupon code has been applied to cart:



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

// HERE set your targeted coupon code (100 % off)
$coupon_code = 'thatsforfree';

// Check if our targeted coupon is applied
if( in_array( wc_format_coupon_code( $coupon_code ), $cart->get_applied_coupons() ) ){
$title = __('Fee', 'woocommerce'); // The fee title
$cost = 20; // The fee amount

// Adding the fee (not taxable)
$cart->add_fee( $title, $cost, false );
}
}


Code goes in function.php file of your active child theme (active theme). Tested and works.



The display in cart and checkout pages (on Woocommerce storefront theme):



enter image description here




This code uses The Woocommerce FEE API using WC_Cart method add_fee() with the dedicated action hook woocommerce_cart_calculate_fees.







share|improve this answer























  • Thanks so much for the quick response! The code is adding a $20 fee, however, it is adding it on the Shipping line as a Flat Rate. Is there a way to make this show as a separate line with the fee title? Thanks!
    – jcooksey80
    Nov 11 at 22:45










  • So there is no way to create a new line? This will give customers the impression they are paying for shipping, when shipping is to be free. A bit confusing. Any workarounds for a new line? Thanks!
    – jcooksey80
    Nov 11 at 22:53










  • I would accept the answer, but this code did not result in what I wanted. It added the fee in the shipping line.
    – jcooksey80
    Nov 12 at 1:13













up vote
0
down vote










up vote
0
down vote









The following code will apply a fee of $20 if a specific coupon code has been applied to cart:



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

// HERE set your targeted coupon code (100 % off)
$coupon_code = 'thatsforfree';

// Check if our targeted coupon is applied
if( in_array( wc_format_coupon_code( $coupon_code ), $cart->get_applied_coupons() ) ){
$title = __('Fee', 'woocommerce'); // The fee title
$cost = 20; // The fee amount

// Adding the fee (not taxable)
$cart->add_fee( $title, $cost, false );
}
}


Code goes in function.php file of your active child theme (active theme). Tested and works.



The display in cart and checkout pages (on Woocommerce storefront theme):



enter image description here




This code uses The Woocommerce FEE API using WC_Cart method add_fee() with the dedicated action hook woocommerce_cart_calculate_fees.







share|improve this answer














The following code will apply a fee of $20 if a specific coupon code has been applied to cart:



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

// HERE set your targeted coupon code (100 % off)
$coupon_code = 'thatsforfree';

// Check if our targeted coupon is applied
if( in_array( wc_format_coupon_code( $coupon_code ), $cart->get_applied_coupons() ) ){
$title = __('Fee', 'woocommerce'); // The fee title
$cost = 20; // The fee amount

// Adding the fee (not taxable)
$cart->add_fee( $title, $cost, false );
}
}


Code goes in function.php file of your active child theme (active theme). Tested and works.



The display in cart and checkout pages (on Woocommerce storefront theme):



enter image description here




This code uses The Woocommerce FEE API using WC_Cart method add_fee() with the dedicated action hook woocommerce_cart_calculate_fees.








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 1:52

























answered Nov 11 at 21:21









LoicTheAztec

82.3k125993




82.3k125993












  • Thanks so much for the quick response! The code is adding a $20 fee, however, it is adding it on the Shipping line as a Flat Rate. Is there a way to make this show as a separate line with the fee title? Thanks!
    – jcooksey80
    Nov 11 at 22:45










  • So there is no way to create a new line? This will give customers the impression they are paying for shipping, when shipping is to be free. A bit confusing. Any workarounds for a new line? Thanks!
    – jcooksey80
    Nov 11 at 22:53










  • I would accept the answer, but this code did not result in what I wanted. It added the fee in the shipping line.
    – jcooksey80
    Nov 12 at 1:13


















  • Thanks so much for the quick response! The code is adding a $20 fee, however, it is adding it on the Shipping line as a Flat Rate. Is there a way to make this show as a separate line with the fee title? Thanks!
    – jcooksey80
    Nov 11 at 22:45










  • So there is no way to create a new line? This will give customers the impression they are paying for shipping, when shipping is to be free. A bit confusing. Any workarounds for a new line? Thanks!
    – jcooksey80
    Nov 11 at 22:53










  • I would accept the answer, but this code did not result in what I wanted. It added the fee in the shipping line.
    – jcooksey80
    Nov 12 at 1:13
















Thanks so much for the quick response! The code is adding a $20 fee, however, it is adding it on the Shipping line as a Flat Rate. Is there a way to make this show as a separate line with the fee title? Thanks!
– jcooksey80
Nov 11 at 22:45




Thanks so much for the quick response! The code is adding a $20 fee, however, it is adding it on the Shipping line as a Flat Rate. Is there a way to make this show as a separate line with the fee title? Thanks!
– jcooksey80
Nov 11 at 22:45












So there is no way to create a new line? This will give customers the impression they are paying for shipping, when shipping is to be free. A bit confusing. Any workarounds for a new line? Thanks!
– jcooksey80
Nov 11 at 22:53




So there is no way to create a new line? This will give customers the impression they are paying for shipping, when shipping is to be free. A bit confusing. Any workarounds for a new line? Thanks!
– jcooksey80
Nov 11 at 22:53












I would accept the answer, but this code did not result in what I wanted. It added the fee in the shipping line.
– jcooksey80
Nov 12 at 1:13




I would accept the answer, but this code did not result in what I wanted. It added the fee in the shipping line.
– jcooksey80
Nov 12 at 1:13


















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%2f53252446%2fwoocommerce-add-fee-if-coupon-applied%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

Bressuire

Vorschmack

Quarantine