我试图将最低订单金额设置为200美元,除了一堆产品(大约5个)可以单独添加,而不管购物车中的金额。
到目前为止,我发现了这段代码,如果没有达到最小值,它似乎可以很好地阻止checkout:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
global $woocommerce;
$minimum = 200;
if ( $woocommerce->cart->get_cart_total() < $minimum ) {
$woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) );
}
}
我现在试图找出如何排除某些product_id的甚至某些category_id的
这里有类似的问题,但不完全是我的,我不确定如何修改代码以适应我的需求
woocommerce set category minimum cart
谢谢亲切的问候
现在经过一些改变,我有几个问题/问题的代码解释在我的回答下面。我使用的代码如下:
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, $product;
$i=0;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) :
// Set minimum cart total
$minimum_cart_total = 200;
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->subtotal;
// See if any product is from the STOCK category or not
if ( has_term( '481', 'product_cat', $product['product_id'] ) ) :
//Get price of that product
$regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale
//echo $regular_price."<br>";
$total = $regular_price * $product['quantity'];
//echo $total."<br>";
$subtotal_cat += $total; //get total of
//echo $subtotal_cat;
//$category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );
endif;
endforeach;
foreach ( $woocommerce->cart->cart_contents as $product ) :
if ( has_term( '481', 'product_cat', $product['product_id'] ) ) :
// 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( $subtotal_cat <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required from stock category before checking out.</strong>'
.'<br />Current cart''s total: %s %s excl. TAX',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
endif;
endforeach;
}
}
好的,我设法使它工作,除了类别的东西。让我解释一下。
现在它正在检查购物车中的产品是否属于一个类别,如果是,则检查它是否超过200美元。如果购物车的总金额不超过200美元,并且购物车中有指定类别的产品,则显示错误消息。我需要添加多个类别到这个函数,但我真的不知道如何做到这一点。我尝试了下面的代码,即使它没有给出任何错误,它也不能识别类别。如果我只使用一个类别ID,它可以正常工作:
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, $product;
$i=0;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) :
// Set minimum cart total
$minimum_cart_total = 200;
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->total;
// See if any product is from the STOCK category or not
if ( has_term( '481,482', 'product_cat', $product['product_id'] ) ) :
//Get price of that product
$regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale
//echo $regular_price."<br>";
$total = $regular_price * $product['quantity'];
//echo $total."<br>";
$subtotal_cat += $total; //get total of
//echo $subtotal_cat;
//$category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );
endif;
endforeach;
if ( has_term( '481,482', 'product_cat', $product['product_id'] ) ) :
// 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 200 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 from stock category before checking out.</strong>'
.'<br />Current cart''s total: %s %s excl. TAX',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
endif;
}
}
谢谢
我不得不做很多改变,但现在它是完美的工作。下面是我使用的代码:
add_action( 'woocommerce_check_cart_items', 'cart_set_min_total' );
function set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce, $product;
$i=0;
// Minimum order checking
$minimumCheck = false;
// Set minimum cart total
$minimum_cart_total = 200;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) {
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->total;
// See if any product is from the STOCK category or not
if ( has_term( '481', 'product_cat', $product['product_id'] ) || has_term( '482', 'product_cat', $product['product_id'] ) ) {
$minimumCheck = true;
//Get price of that product
$regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale
//echo $regular_price."<br>";
$total = $regular_price * $product['quantity'];
//echo $total."<br>";
$subtotal_cat += $total; //get total of
//echo $subtotal_cat;
//$category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );
}
if (has_term( '503', 'product_cat', $product['product_id']) || has_term( '495', 'product_cat', $product['product_id'] ) ) {
//Get price of that product
$regular_price = get_post_meta($product['product_id'], '_regular_price', true); //change to _sale_price if it is in sale
//echo $regular_price."<br>";
$total = $regular_price * $product['quantity'];
//echo $total."<br>";
$subtotal_cat += $total; //get total of
//echo $subtotal_cat;
//$category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );
}
}
if ( $minimumCheck && $subtotal_cat <= $minimum_cart_total) {
// 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 200 USD is required before checking out. (Cont. below)
// Current cart total: 6 USD
wc_add_notice( sprintf( '<strong>A Minimum of %s %s excl. TAX is required category before checking out.</strong>'
.'<br />Current cart''s total: %s %s excl. TAX',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$subtotal_cat,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}