更改 Woocommerce 3 中特定产品标签的所有产品价格
我使用了一个函数来自动标记所有产品,而无需使用 SQL 或手动标记所有产品,因为它们会非常频繁地重新上传和更新.
I used a function to be able to markup all product automatically without having to use SQL or manually do it as they get re-uploaded and updated very frequently.
除了 has_term if 语句之外,该函数完美运行.它们在 Wordpress 后端工作并在我添加标签后立即应用定价规则,但是当我尝试将产品添加到购物车时,它会恢复到购物车和结帐页面中的原始价格.如果我删除那个 if 语句,它就没有问题.我需要一种方法让该功能仅适用于标记为ama"的产品.
The function works perfectly other than the has_term if statements. They work in the Wordpress backend and apply the pricing rules as soon as I add the tag, but when I try to add the product to cart it reverts back to the original price in the cart and checkout pages. If I remove that if statement it works with no issues. I need a way to have the function only apply to products tagged 'ama'.
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );
// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 90, 2 );
// Variable product price range
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 90, 3 );
function custom_price( $price, $product ) {
if ( has_term( 'ama', 'product_tag' ) ) {
if ($price > 0.01 && $price < 4.99) {
$price *= 2.5;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 5 && $price < 9.99) {
$price *= 2;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 10 && $price < 19.99) {
$price *= 1.75;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 20 && $price < 39.99) {
$price *= 1.5;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 40 && $price < 59.99) {
$price *= 1.35;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 60 && $price < 79.99) {
$price *= 1.25;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 80 && $price < 999.99) {
$price *= 1.20;
$price = ceil($price + 0.01) - 0.01;
}
}
return $price;
}
function custom_variation_price( $price, $variation, $product ) {
if ( has_term( 'ama', 'product_tag' ) ) {
if ($price > 0.01 && $price < 4.99) {
$price *= 2.5;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 5 && $price < 9.99) {
$price *= 2;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 10 && $price < 19.99) {
$price *= 1.75;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 20 && $price < 39.99) {
$price *= 1.5;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 40 && $price < 59.99) {
$price *= 1.35;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 60 && $price < 79.99) {
$price *= 1.25;
$price = ceil($price + 0.01) - 0.01;
}
elseif ($price > 80 && $price < 999.99) {
$price *= 1.20;
$price = ceil($price + 0.01) - 0.01;
}
}
return $price;
}
我已经重新访问了您的代码……我已将定价更新合并到一个单独的实用程序函数中……
I have revisited your code… I have merged the pricing updates in a separated utility function…
为了避免您的问题需要定义产品 ID (之前检查产品类型).
然后我们在 WordPress has_term() 条件函数中设置这个正确定义的产品 ID.
To avoid your problems is necessary to define the product ID (checking the product type before).
Then we set this correct defined product ID in WordPress has_term() conditional function.
现在您的价格也适用于购物车和结帐页面......
Now your prices will work on cart and checkout pages too…
您重新访问的代码:
// Utility pricing function
function filtering_product_prices( $price, $product ) {
// Get the product ID
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Only for Woocomerce Product Tag "ama"
if ( ! has_term( 'ama', 'product_tag', $product_id ) ) return $price; // Exit
if ( $price < 5 ) {
$price *= 2.5;
} elseif ( $price >= 5 && $price < 10 ) {
$price *= 2;
} elseif ( $price >= 10 && $price < 20 ) {
$price *= 1.75;
} elseif ( $price >= 20 && $price < 40 ) {
$price *= 1.5;
} elseif ( $price >= 40 && $price < 60 ) {
$price *= 1.35;
} elseif ( $price >= 60 && $price < 80 ) {
$price *= 1.25;
} elseif ( $price >= 80 && $price < 1000 ) {
$price *= 1.20;
}
return ceil($price + 0.01) - 0.01;
}
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );
// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 90, 2 );
function custom_variation_price( $price, $variation, $product ) {
return filtering_product_prices( $price, $product );
}
// Variable product price range
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 90, 3 );
function custom_price( $price, $product ) {
return filtering_product_prices( $price, $product );
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中.经过测试和工作.