我想知道是否有可能创建一个if语句,如果客户没有在他们的订单中添加足够的(数量 - 而不是价格)特定产品类别,则会显示一条消息,说明他们需要添加更多是为了避免附加费。我正在考虑最小订单金额代码段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 ) );
}
}
非常感谢任何帮助。
答案 0 :(得分:2)
可以使用woocommerce_checkout_process
和woocommerce_before_cart
Woocommerce Hooks完成此操作。
所以在您的主题functions.php
file 添加此代码(更改命名您的类别字符串):
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'
);
}
}
}
}
答案 1 :(得分:0)
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 ) );
}
}