首页 > 解决方案 > 在 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() 会有所帮助。

非常感谢任何帮助,谢谢!

标签: phpwoocommercecart

解决方案


结果替换

WC()->cart->get_cart_total();

$woocommerce->cart->total;

实际上可以解决问题。但我不明白为什么。:-D


推荐阅读