0

我想知道是否可以创建一个 if 语句,如果客户没有在他们的订单中添加足够的特定产品类别(数量 - 不是价格),则会显示一条消息,说他们需要添加更多以避免附加费。我正在考虑此处记录的最低订单金额片段:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    function wc_minimum_order_amount() {
        global $woocommerce;
        $minimum = 50;
    if ( $woocommerce->cart->total() < $minimum ) {
        $woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) );
    }
}

任何帮助将不胜感激。

4

2 回答 2

2

这可以通过使用woocommerce_checkout_processwoocommerce_before_cart Woocommerce Hooks来完成。

functions.php因此,在您的主题文件中添加此代码(更改Name Your category字符串):

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

function wc_minimum_order_amount() {

    $minimum = 50; //Qty product

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

        $draught_links = array();

        foreach(WC()->cart->get_cart() as $cart_item_key => $values ) {

            $_product = $values['data'];

            $terms = get_the_terms( $_product->id, 'product_cat' );

            foreach ($terms as $term) {
                $draught_links[] = $term->name;
            }   

        }

        if (in_array("Name Your category", $draught_links)){
            $on_draught = true;
        }else{
            $on_draught = false;
        }

        if( is_cart() ) {

            if($on_draught){

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

            }

        } else {

            if($on_draught){

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

            }

        }
    }

}
于 2014-09-24T21:25:05.953 回答
0

来自WooCommerce 文档

public float $cart_contents_count - 购物车项目的总数。

因此,这应该可以满足您的需求:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
    global $woocommerce;
    $minimum = 50;
    if ( $woocommerce->cart->cart_contents_count < $minimum ) {
        $woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) );
    }
}
于 2013-10-09T03:13:11.737 回答