避免 Woocommerce 中特定产品类别的购物车商品价格更新
Avoid cart items price update for specific product categories in Woocommerce
我在我的 WordPress 子主题 functions.php 下使用以下脚本来覆盖价格,但以下代码影响所有产品。
如果产品属于某些特定的 "shirts"、"games" 类别,您能帮我 "not to run" 下面的代码吗?
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 搜索,但无法找到完美的解决方案。
您的代码有一些错误和失误...由于 $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 文件。它应该有效。
相关话题:
我在我的 WordPress 子主题 functions.php 下使用以下脚本来覆盖价格,但以下代码影响所有产品。
如果产品属于某些特定的 "shirts"、"games" 类别,您能帮我 "not to run" 下面的代码吗?
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 搜索,但无法找到完美的解决方案。
您的代码有一些错误和失误...由于 $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 文件。它应该有效。
相关话题: