我在我的WordPress子主题functions.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
我可以仅在需要时将上述代码运行到特定产品或类别?
我也做了一些谷歌搜索,但找不到完美的解决方案。
您的代码有一些错误和错误...由于
$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 handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// 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 文件中。应该有效。
相关主题: