我正在WordPress子主题function.php下使用下面的脚本覆盖价格,但是下面的代码会影响所有产品。
如果产品属于某些特定的“衬衫”,“游戏”类别,您能帮我“不要运行”以下代码吗?
function calculate_cart_total( $cart_object ) {
foreach ( WC()->cart->get_cart() as $key => $value ) {
if(is_array($valueArray)) {
foreach ( $valueArray as $k => $value ) {
if($value['wccpf_cost']) {
$additionalPrice = $value['wccpf_cost']['user_val'];
}
}
}
}
$additionalPrice = $value['wccpf_cost']['user_val'];
foreach ( WC()->cart->get_cart() as $key => $value ) {
if( method_exists( $value['data'], "set_price" ) ) {
$value['data']->set_price( $additionalPrice );
} else {
$value['data']->price = ($additionalPrice );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_cart_total', 99 );
以上代码在所有产品上均能正常运行,但我不想在所有产品上都运行此代码,因为需要此代码
我尝试了诸如is_product_category( array( 'shirts', 'games' ) )
之类的条件标签,但无法正常工作。
或者WordPress中有什么特定的方法代替functions.php
我只能在需要的地方对特定产品或类别运行以上代码?
我也做了一些Google搜索,但是找不到完美的解决方案。
答案 0 :(得分:2)
您的代码有一些错误和错误……由于$valueArray
没有在您的代码中定义,因此第一个foreach循环没有任何作用,并且代码在其后变为活动状态。要定位购物车商品的产品类别,您可以使用WordPress条件函数has_term()
。
请尝试以下操作,而不是(适用于Woocommerce 3 +):
// Updating cart item prices conditionally
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 100, 1 );
function custom_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Your product categories to avoid
$categories = array("shirts", "games");
foreach ( $cart->get_cart() as $cart_item ) {
if( isset($cart_item['wccpf_cost']['user_val']) && ! has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$cart_item['data']->set_price($cart_item['wccpf_cost']['user_val']);
}
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。
但是,如果要定位父产品类别,则需要在代码中使用has_product_categories()
自定义条件函数:
// Custom conditional function that checks for parent product categories
function has_product_categories( $product_id, $categories ) {
// Initializing
$parent_term_ids = $categories_ids = array();
$taxonomy = 'product_cat';
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
if( is_numeric( $category ) ) {
$categories_ids[] = (int) $category;
} elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
$categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
// Updating cart item prices conditionally
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 100, 1 );
function custom_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Your product categories to avoid
$categories = array("shirts", "games");
foreach ( $cart->get_cart() as $cart_item ) {
if( isset($cart_item['wccpf_cost']['user_val']) && ! has_product_categories( $cart_item['product_id'], $categories ) ) {
$cart_item['data']->set_price($cart_item['wccpf_cost']['user_val']);
}
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。
相关线程: