最低购物车金额,产品类别除外
Minimum cart amount except and exclusively for a product category
我正在尝试设置购物车的最低金额以允许客户结帐。但是我想对一个产品类别做个例外,即使它不符合最低要求的 200,我也会允许客户继续。
我的代码可以正常工作 几乎 除了:
- 通知中显示的购物车计算金额(错误)。
- 此特殊类别的独占条件
501
(除此特殊类别外,当其他类别的产品也在购物车中时,无法按预期工作) .
这是我的代码:
add_action( 'woocommerce_check_cart_items', 'oxynergy_set_min_total' );
function oxynergy_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'] ) || has_term( '495', '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( '501', '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' );
}
}
}
我做错了什么?
我怎样才能使此代码工作以专门为该特殊产品类别设置例外?
谢谢。
如果我明白了,您需要为产品类别禁用定义的 "Minimal required total cart amount" 且排他性 ,但如果有来自其他产品类别的其他购物车项目则不需要用它。
所以你只需要在下面的代码中设置这个产品类别ID,你不需要任何购物车的计算。
我也没有使用 has_term()
函数来避免 parent/child 产品类别的一种错误。相反,我使用 wp_get_post_terms()
和一个 foreach 循环…
我一直在更改您的代码:
add_action( 'woocommerce_check_cart_items', 'oxynergy_set_min_total' );
function oxynergy_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// Set HERE your minimum cart total
$minimum_cart_total = 200;
// Set HERE your special product category ID
$special_category = 501;
//Iterating through each cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Get the product categories for the current item
$item_categories = wp_get_post_terms( $cart_item['product_id'], 'product_cat' );
// Iterating through each product categories defined for the item
foreach($item_categories as $item_category){
if( $item_category->term_id == $special_category ) {
$minimumCheck = false;
break;
}
else {
$minimumCheck = true;
}
}
// The cart total without taxes:
$cart_total_excl_taxes = WC()->cart->subtotal_ex_tax;
}
if ( $minimumCheck == 'true' && $cart_total_excl_taxes <= $minimum_cart_total) {
// Displays the message notice
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'),
$cart_total_excl_taxes,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}
这段代码已经过测试,这次可以正常工作……
代码进入您的活动子主题(或主题)的任何 php 文件或任何插件 php 文件。
我正在尝试设置购物车的最低金额以允许客户结帐。但是我想对一个产品类别做个例外,即使它不符合最低要求的 200,我也会允许客户继续。
我的代码可以正常工作 几乎 除了:
- 通知中显示的购物车计算金额(错误)。
- 此特殊类别的独占条件
501
(除此特殊类别外,当其他类别的产品也在购物车中时,无法按预期工作) .
这是我的代码:
add_action( 'woocommerce_check_cart_items', 'oxynergy_set_min_total' );
function oxynergy_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'] ) || has_term( '495', '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( '501', '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' );
}
}
}
我做错了什么?
我怎样才能使此代码工作以专门为该特殊产品类别设置例外?
谢谢。
如果我明白了,您需要为产品类别禁用定义的 "Minimal required total cart amount" 且排他性 ,但如果有来自其他产品类别的其他购物车项目则不需要用它。
所以你只需要在下面的代码中设置这个产品类别ID,你不需要任何购物车的计算。
我也没有使用 has_term()
函数来避免 parent/child 产品类别的一种错误。相反,我使用 wp_get_post_terms()
和一个 foreach 循环…
我一直在更改您的代码:
add_action( 'woocommerce_check_cart_items', 'oxynergy_set_min_total' );
function oxynergy_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// Set HERE your minimum cart total
$minimum_cart_total = 200;
// Set HERE your special product category ID
$special_category = 501;
//Iterating through each cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Get the product categories for the current item
$item_categories = wp_get_post_terms( $cart_item['product_id'], 'product_cat' );
// Iterating through each product categories defined for the item
foreach($item_categories as $item_category){
if( $item_category->term_id == $special_category ) {
$minimumCheck = false;
break;
}
else {
$minimumCheck = true;
}
}
// The cart total without taxes:
$cart_total_excl_taxes = WC()->cart->subtotal_ex_tax;
}
if ( $minimumCheck == 'true' && $cart_total_excl_taxes <= $minimum_cart_total) {
// Displays the message notice
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'),
$cart_total_excl_taxes,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}
这段代码已经过测试,这次可以正常工作……
代码进入您的活动子主题(或主题)的任何 php 文件或任何插件 php 文件。