admin管理员组文章数量:1316674
I need to set a global 5% discount (or 0.95 margin) for all logged in customers on my store. I've used filters to alter the price for all simple products and product variations. To get it working correctly I needed to delete product transients of each product for logged in users.
This works well but the loading time is way too long. The performance impact of this seems to be drastic.
Here's the code:
function change_price_for_logged_in_customers($price, $product, $clear_transients) {
if ( is_user_logged_in() && is_numeric($price) ) {
// Clear WC transients for variations
if($clear_transients) {
wc_delete_product_transients($product->get_id());
}
// Apply 5% discount, round up discounted price to 1 decimal
$logged_in_price = $price*0.95;
$logged_in_price = number_format((float)$logged_in_price, 1, '.', '');
return $logged_in_price;
}
return $price;
}
function logged_in_discount_filter($price, $product) {
return change_price_for_logged_in_customers($price, $product, 0);
}
add_filter('woocommerce_product_get_price', 'logged_in_discount_filter', 10, 2);
function custom_price( $price, $product ) {
return change_price_for_logged_in_customers($price, $product, 1);
}
add_filter('woocommerce_product_variation_get_price', 'custom_price' , 99, 2 );
function custom_variation_price( $price, $variation, $product ) {
return change_price_for_logged_in_customers($price, $product, 1);
}
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
Is there a way to do this more efficiently?
Thank you
I need to set a global 5% discount (or 0.95 margin) for all logged in customers on my store. I've used filters to alter the price for all simple products and product variations. To get it working correctly I needed to delete product transients of each product for logged in users.
This works well but the loading time is way too long. The performance impact of this seems to be drastic.
Here's the code:
function change_price_for_logged_in_customers($price, $product, $clear_transients) {
if ( is_user_logged_in() && is_numeric($price) ) {
// Clear WC transients for variations
if($clear_transients) {
wc_delete_product_transients($product->get_id());
}
// Apply 5% discount, round up discounted price to 1 decimal
$logged_in_price = $price*0.95;
$logged_in_price = number_format((float)$logged_in_price, 1, '.', '');
return $logged_in_price;
}
return $price;
}
function logged_in_discount_filter($price, $product) {
return change_price_for_logged_in_customers($price, $product, 0);
}
add_filter('woocommerce_product_get_price', 'logged_in_discount_filter', 10, 2);
function custom_price( $price, $product ) {
return change_price_for_logged_in_customers($price, $product, 1);
}
add_filter('woocommerce_product_variation_get_price', 'custom_price' , 99, 2 );
function custom_variation_price( $price, $variation, $product ) {
return change_price_for_logged_in_customers($price, $product, 1);
}
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
Is there a way to do this more efficiently?
Thank you
Share Improve this question asked Dec 9, 2020 at 7:38 Kristián FiloKristián Filo 4316 silver badges20 bronze badges1 Answer
Reset to default 0https://woocommerce.wp-a2z/oik_api/wc_delete_product_transients/
You need to clear only 1 transient:
delete_transient( 'wc_var_prices_'. $product->get_id() );
本文标签: Change WooCommerce product and variation prices programatically without affecting performance
版权声明:本文标题:Change WooCommerce product and variation prices programatically without affecting performance 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741930546a2405547.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论