在 Woocommerce 购物车中设置一个最小数量,一个类别除外
Set a minimum amount in Woocommerce cart except for one category
我正在尝试将最低订单金额设置为 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 的
这里有类似的问题,但与我的不完全一样,我不确定如何修改代码以适应我的需要
谢谢
亲切的问候
现在,经过一些更改后,我有一些 questions/problems 代码,如我在下面的回答中所解释的那样。我使用的代码如下:
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 美元,并且购物车中有指定类别的产品,则会显示错误消息。我需要为此功能添加多个类别,但我真的不知道该怎么做。我尝试了下面的代码,即使它没有给出任何错误,它也无法识别 categories.If 我只使用一个类别 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' );
}
}
}
我正在尝试将最低订单金额设置为 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 的
这里有类似的问题,但与我的不完全一样,我不确定如何修改代码以适应我的需要
谢谢 亲切的问候
现在,经过一些更改后,我有一些 questions/problems 代码,如我在下面的回答中所解释的那样。我使用的代码如下:
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 美元,并且购物车中有指定类别的产品,则会显示错误消息。我需要为此功能添加多个类别,但我真的不知道该怎么做。我尝试了下面的代码,即使它没有给出任何错误,它也无法识别 categories.If 我只使用一个类别 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' );
}
}
}