[英]Set Max Purchase Totals in WooCommerce
I am trying to set a max purchase total in my WooCommerce cart.我正在尝试在我的 WooCommerce 购物车中设置最大购买总额。 (Background: I want to prohibit people, who use 100% coupons in this case, to add more products to their cart.)
(背景:我想禁止在这种情况下使用 100% 优惠券的人将更多产品添加到他们的购物车。)
This is the code I adapted from here :这是我从这里改编的代码:
// Weitere Einkäufe verhindern
add_action( 'woocommerce_check_cart_items', 'cldws_set_max_total');
function cldws_set_max_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set maximum cart total
$max_cart_total = 1;
// A maximum of 1 € is required before checking out.
$total = WC()->cart->totals;
// Compare values and add an error is Cart's total
if( $total >= $max_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>Bitte lege nicht mehr Stränge in den Warenkorb als du beim Crowdfunding bestellt hast.</strong> Die genaue Anzahl findest du in der E-Mail mit dem Gutscheincode. '),
'error' );
}
}
}
Here is what it should do:这是它应该做的:
After checking that this only runs on the cart and checkout pages and setting the max.检查这仅在购物车和结帐页面上运行并设置最大值后。 totals value to 1 €, I query the cart's totals and compare them to this max value.
总计值为 1 欧元,我查询购物车的总计并将它们与此最大值进行比较。 If the total is bigger than the max.
如果总数大于最大值。 allowed total, it displays the error message.
允许的总数,它会显示错误消息。
I suspect that I am querying the wrong variable with "totals", however that one should be the variable that is the total cart value after discounts.我怀疑我用“totals”查询了错误的变量,但是那个变量应该是折扣后购物车总价值的变量。 I've also tried "get_cart_total()", but that didn't help.
我也尝试过“get_cart_total()”,但这没有帮助。 I've also looked at this, but don't get why replacing WC() for $woocommerce should help.
我也看过这个,但不明白为什么用 $woocommerce 替换 WC() 会有所帮助。
Any help is greatly appreciated, thanks!非常感谢任何帮助,谢谢!
Turns out replacing结果替换
WC()->cart->get_cart_total();
with和
$woocommerce->cart->total;
actually does the trick.实际上可以解决问题。 But I don't understand why.
但我不明白为什么。 :-D
:-D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.