【问题标题】:Woocommerce minimum order by category/quantityWoocommerce 按类别/数量的最小订单
【发布时间】:2013-10-16 04:06:10
【问题描述】:

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

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 ) );
    }
}

任何帮助将不胜感激。

【问题讨论】:

  • 您似乎已经在代码中找到了答案,那么问题是什么?
  • 上面的代码根据货币价值计算整个购物车的总数 - 我所追求的是特定类别的产品的最低数量,而不管价格如何。

标签: php wordpress woocommerce cart


【解决方案1】:

来自WooCommerce Docs

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 ) );
    }
}

【讨论】:

  • 即将推出,但我需要将其限制为特定类别中的产品数量。
【解决方案2】:

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

所以在您的主题的functions.php file 中添加此代码(更改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' 
                );

            }

        }
    }

}

【讨论】:

  • 请提供有关您的答案的更多信息并为下一个用户描述您的代码...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
  • 2015-07-16
  • 2021-07-24
  • 2020-12-02
  • 2012-05-04
  • 1970-01-01
  • 2018-10-25
相关资源
最近更新 更多