php - 在 WooCommerce 中设置最低订单金额

标签 php wordpress woocommerce cart checkout

我想在我的 WooCommerce 商店中设置最低订单量。如果未达到金额但仍然可以结帐,则以下代码完美显示通知。未达到最低金额时如何禁用结帐按钮?

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );

function wc_minimum_order_amount() {
    // Set this variable to specify a minimum order value
    $minimum = 50;

    if ( WC()->cart->total < $minimum ) {

        if( is_cart() ) {

            wc_print_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        } else {

            wc_add_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        }
    }
}

最佳答案

要设置最低订购量,您可以使用 woocommerce_check_cart_items这样的 Action 钩子(Hook):

add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {

    // HERE Set minimum cart total amount
    $minimum_amount = 250;

    // Total (before taxes and shipping charges)
    $cart_subtotal = WC()->cart->subtotal;

    // Add an error notice is cart total is less than the minimum required
    if( $cart_subtotal < $minimum_amount  ) {
        // Display an error message
        wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($minimum_amount) ) . '<strong>', 'error' );
    }
}
代码位于您的事件子主题(或事件主题)的 function.php 文件中。测试和工作。

If customer update the cart changing quantities or removing items, The behavior will be updated too.



enter image description here

enter image description here

相关答案:Woocommerce set minimum order for a specific user role

关于php - 在 WooCommerce 中设置最低订单金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55038477/

相关文章:

css - WordPress 不应用样式,即使它们已加载

php - 获取最后一个/in url之后的字符

PHP 7 接口(interface),返回类型提示和 self

wordpress - 如何在woocommerce上添加第二个添加到购物车按钮

WordPress:高级自定义字段:将字段导出和导入到新的 WordPress 安装

php - 所有 WooCommerce 结帐字段的自定义占位符

php - 同一张表上的 SQL JOIN 返回重复结果

php - 打印一个图案以显示最多 5 行和 5 列的数字,例如 5 4 3 2 1 并在下一行 4 3 2 1 5 到第 5 行

php - WooCommerce - 如何将列添加到管理订单页面中的产品列表

php - WooCommerce - 添加到购物车时忽略未选择的变体