设置Woocommerce中特定产品和类别的最低订单金额

时间:2016-05-17 04:47:36

标签: php wordpress woocommerce cart taxonomy-terms

我已经进行了广泛的搜索,看看其他人是否有这种情况并得到了一个没有运气的答案。

基本上,我有两个客户可以添加到购物车的商品。我想这样做,如果他们的小计不是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' );
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

此未经测试的代码段由来自wooThemes的thisthis以及this too组成:

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

function wc_minimum_order_amount() {

    // minimum order value
    $minimum = 10;

    if ( WC()->cart->total < $minimum && sizeof( WC()->cart->get_cart() ) > 0 ) {
        $products_min = false;

        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product = $cart_item['data'];
            $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
            $terms = get_the_terms( $product_id, 'product_cat' );
            foreach ($terms as $term) {
                $category_term_id = $term->term_id;
            }
            // your products categories
            if ( in_array( $category_term_id, array( 17, 18 ) ) ) { 
                $products_min = true;

            // your product ID'S
            } elseif ( in_array( $product_id, array( 64, 67, 78 ) ) ) { 
                $products_min = true;
            }
        }
        if( ( is_cart() || is_checkout() ) && $products_min ) {
            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 ), 
                wc_price( WC()->cart->total )
            ), 'error' );
        }
    }
}

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

- - U p d a t e - -

这个钩子正在为消息工作。但是为了避免用户点击你需要添加Javascript / jQuery,也可能使用ajax(客户端检测)。

相关问题