【问题标题】:Set minimum Order amount for specific Products or Categories in WooCommerce在 WooCommerce 中为特定产品或类别设置最低订单金额
【发布时间】:2016-05-17 04:47:35
【问题描述】:

我已经广泛搜索,看看其他人是否有这种情况,但没有得到答案。

基本上,我有两件商品可以让客户添加到他们的购物车中。我想这样做,如果他们的小计不是 15 美元或更多,他们就不能用这些项目中的任何一个结帐。

能够将他们的 ID 放入代码中就可以了。或者,我可以将它们分配到同一类别并按类别设置此最小值。

到目前为止,我所拥有的只是设置通用最小值的基本 PHP。我只是需要一些帮助来弄清楚如何限制它以满足我的需求。

我是设计师而不是开发人员,因此非常感谢任何帮助。

    // Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set minimum cart total
        $minimum_cart_total = 10;

        // Total we are going to be using for the Math
        // This is before taxes and shipping charges
        $total = WC()->cart->subtotal;

        // Compare values and add an error is Cart's total
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of 10 USD is required before checking out. (Cont. below)
        // Current cart total: 6 USD 
        if( $total <= $minimum_cart_total  ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
                .'<br />Current cart\'s total: %s %s',
                $minimum_cart_total,
                get_option( 'woocommerce_currency'),
                $total,
                get_option( 'woocommerce_currency') ),
            'error' );
        }
    }
}

【问题讨论】:

    标签: php wordpress woocommerce cart taxonomy-terms


    【解决方案1】:

    2020 年更新

    仅为购物车和结帐页面设置购物车中某些产品类别或产品 ID 的最小值。

    这个未经测试的 sn-p 由来自 wooThemes 的 thisthisthis too 组成:

    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
     
    function wc_minimum_order_amount() {
        ##  SETTINGS  ##
        $minimum_amount = 50; // Define a minimum order amount
        $category_ids   = array( 17, 18 ); // Define your category ids in the array (or an empty array to disable)
        $product_ids    = array( 64, 67, 78 ); // Define your product ids in the array (or an empty array to disable)
    
        // Only on cart or checkout pages
        if( WC()->cart->is_empty() || ! ( is_cart() || is_checkout() ) ) 
            return; // Exit
    
        $total_amount = WC()->cart->subtotal; // Items subtotal including taxes
    
        if ( $total_amount < $minimum_amount ) {
            $needs_minimum_amount = false; // Initializing
    
            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $cart_item ) {
                $product_id   = $cart_item['product_id'];
                $variation_id = $cart_item['variation_id'];
                
                // 1. Check for matching product categories
                if( sizeof($category_ids) > 0 ) {
                    $taxonomy = 'product_cat';
    
                    if ( has_term( $category_ids, $taxonomy, $product_id ) ) { 
                        $needs_minimum_amount = true;
                        break; // Stop the loop
                    }
                }
    
                // 2. Check for matching product Ids
                if( sizeof($product_ids) > 0 ) {
                    if ( array_intersect( $product_ids, array($product_id, $variation_id) ) ) { 
                        $needs_minimum_amount = true;
                        break; // Stop the loop
                    }
                }
            }
    
            if( $needs_minimum_amount ) {
                wc_print_notice( sprintf( 
                    __("You must have an order with a minimum of %s to place your order. Your current order total is %s."), 
                    wc_price( $minimum_amount ), 
                    wc_price( $total_amount )
                ), 'error' );
            }
        }
    }
    

    代码在您的活动子主题(活动主题)的functions.php 文件中。经过测试并且可以工作。

    您可以设置类别 ID、产品 ID 和最低订单金额。


    ————……我不建议……不……——

    这个钩子适用于消息。但是为了避免用户点击,您需要添加 Javascript/jQuery,也许还需要添加 ajax(客户端检测)。

    【讨论】:

    • 您好——非常感谢您的回复。这是一个很好的开始,但不幸的是,它实际上并没有阻止用户单击 Checkout 并继续使用 PayPal。警告会显示但不会阻止它们。另外仅供参考,上面有一个小错字,产品ID所在的地方有一个额外的括号。
    • 好的,谢谢。我的意思是“供您参考”有一个错字。 :) 我会将此标记为正确,即使它还没有阻止用户结帐。我会尝试看看我是否可以自己找出 jQuery 来阻止它。谢谢。
    • 哦,如果不是太麻烦那就太好了!所以这里是the link to the cart page。这是the item that blocks a purchase under a certain minimum,如果您需要,可以将this product 添加到您的购物车以超过最低要求。谢谢!
    • 好的,不用担心。我很感激你的时间。虽然我有一个问题(同样,我真的不太了解 PHP),但我最初尝试过 this code 并尝试根据我的需要定制它,但没有运气。但是,当您按原样使用时,当您单击 Checkout 时,它会重定向到错误页面。我发现你的代码和他们的代码在语法上存在一些差异,“错误”在哪里......就像调整结尾一样简单吗?
    猜你喜欢
    • 2021-05-08
    • 2019-07-29
    • 2019-10-07
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多