php - 在 WooCommerce 中设置最大购买总数
问题描述
我正在尝试在我的 WooCommerce 购物车中设置最大购买总额。(背景:我想禁止在这种情况下使用 100% 优惠券的人将更多产品添加到他们的购物车。)
这是我从这里改编的代码:
// 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' );
}
}
}
这是它应该做的:
检查这仅在购物车和结帐页面上运行并设置最大值后。总计值为 1 欧元,我查询购物车的总计并将它们与此最大值进行比较。如果总数大于最大值。允许的总数,它会显示错误消息。
我怀疑我用“totals”查询了错误的变量,但是那个变量应该是折扣后购物车总价值的变量。我也尝试过“get_cart_total()”,但这没有帮助。我也看过这个,但不明白为什么用 $woocommerce 替换 WC() 会有所帮助。
非常感谢任何帮助,谢谢!
解决方案
结果替换
WC()->cart->get_cart_total();
和
$woocommerce->cart->total;
实际上可以解决问题。但我不明白为什么。:-D
推荐阅读
- mongodb - 一段时间后,docker-compose MongoDB 副本集数据丢失
- pandas - 如何根据其他行的条件创建列 - Pandas Dataframe?
- vue.js - 如何从 vuex 实时 Vue
- maven - 无法测试 Maven 插件
- php - TYPO3 10.4.12 安装在第二步被阻止
- javascript - 一个接一个地响应执行动作调用不起作用
- apache-spark - Spark 3.0 UTC 到 AKST 转换失败,出现 ZoneRulesException:未知时区 ID
- c# - Sql server 我想让存储过程在运行时获取表名并返回此表中的最后一行
- javascript - Vue firebase查询:如何使用无限加载显示下一个结果?
- java - 在具有特定条件的 ArrayList 中查找值