【发布时间】:2017-03-03 12:58:37
【问题描述】:
我正在尝试设置 200 美元的最低订单金额,除了可以单独添加的一堆产品(大约 5 个),而不管购物车中的金额是多少。
到目前为止,我发现了这段代码,如果未达到最小值,这似乎可以阻止结帐:
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
这里有类似的问题,但不完全是我的问题,我不确定如何修改代码以适应我的需要
Woocomerce 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;
}
}
谢谢
【问题讨论】:
-
嘿,我几乎可以解决,但仍有 3 个问题 1. 我不确定如何指定多个类别,我知道 PHP 代码中有一些方法可以添加像 ||确定 OR 2. 它显示错误消息两次 3. 我不计算购物车总数的金额,而是计算类别总数...所以我需要它用于整个购物车而不是指定的类别。你可以看到第一个问题的变化
-
好吧,我得到了它的总金额,但它仍然显示每个添加到购物车的产品一行错误,我想总共只显示一行
-
我知道它与 endforeach 有关;但我不知道如何修改那个位
标签: php wordpress function woocommerce checkout