WooCommerce 中不包括类别及其子项的折扣购物车商品价格
Discount cart item price excluding a category and its children in WooCommerce
事实是客户要求“本地取货”有 50% 的折扣 - 我已经这样做了(我在 Whosebug 上找到了一个对所有商品都适用折扣的代码),但还有一个条件 - 此折扣不适用于两类商品。
Sorry for Russian image;)
我试着遍历了订单列表中的每个产品,通过各种运算符检查它是否属于某个类别(is_category,in_category,has_term ), 但它没有用。我究竟做错了什么?
(id = 37 是 Premium rolls 类别的 id)
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 10, 1 );
function custom_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$category_id = 37; // premium-rolls id
$cats = hml_get_category_gender_line( $category_id );
foreach ( $cart->get_cart() as $cart_item ){
if (!has_term($cats, 'product_cat', $value['product_id'] )){
$price = $cart_item['data']->get_price(); // Get the product price
$new_price = $price-$price*50/100; // the calculation
$cart_item['data']->set_price( $new_price ); // Set the new price
}
}
}
function hml_get_category_gender_line( $cat_parent ){
// get_term_children() accepts integer ID only
$line = get_term_children( (int) $cat_parent, 'product_cat');
$line[] = $cat_parent;
return $line;
}
已更新
您的代码中有一些错误。下面的代码将对来自特定产品类别 (及其子项):
的购物车商品应用折扣价
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 10, 1 );
function custom_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$parent_id = 37; // premium-rolls id
$taxonomy = 'product_cat';
foreach ( $cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
$terms_ids = get_term_children( $parent_id, $taxonomy, $product_id ); // get children terms ids array
array_unshift( $terms_ids, $parent_id ); // insert parent term id to children terms ids array
if ( ! has_term( $terms_ids, $taxonomy, $product_id ) ){
$new_price = $cart_item['data']->get_price() / 2; // Get 50% of the initial product price
$cart_item['data']->set_price( $new_price ); // Set the new price
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。
事实是客户要求“本地取货”有 50% 的折扣 - 我已经这样做了(我在 Whosebug 上找到了一个对所有商品都适用折扣的代码),但还有一个条件 - 此折扣不适用于两类商品。 Sorry for Russian image;)
我试着遍历了订单列表中的每个产品,通过各种运算符检查它是否属于某个类别(is_category,in_category,has_term ), 但它没有用。我究竟做错了什么? (id = 37 是 Premium rolls 类别的 id)
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 10, 1 );
function custom_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$category_id = 37; // premium-rolls id
$cats = hml_get_category_gender_line( $category_id );
foreach ( $cart->get_cart() as $cart_item ){
if (!has_term($cats, 'product_cat', $value['product_id'] )){
$price = $cart_item['data']->get_price(); // Get the product price
$new_price = $price-$price*50/100; // the calculation
$cart_item['data']->set_price( $new_price ); // Set the new price
}
}
}
function hml_get_category_gender_line( $cat_parent ){
// get_term_children() accepts integer ID only
$line = get_term_children( (int) $cat_parent, 'product_cat');
$line[] = $cat_parent;
return $line;
}
已更新
您的代码中有一些错误。下面的代码将对来自特定产品类别 (及其子项):
的购物车商品应用折扣价add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 10, 1 );
function custom_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$parent_id = 37; // premium-rolls id
$taxonomy = 'product_cat';
foreach ( $cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
$terms_ids = get_term_children( $parent_id, $taxonomy, $product_id ); // get children terms ids array
array_unshift( $terms_ids, $parent_id ); // insert parent term id to children terms ids array
if ( ! has_term( $terms_ids, $taxonomy, $product_id ) ){
$new_price = $cart_item['data']->get_price() / 2; // Get 50% of the initial product price
$cart_item['data']->set_price( $new_price ); // Set the new price
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。