我需要为我商店中所有登录的客户设置全球5%的折扣(或0.95的利润率)。我使用过滤器来改变所有简单产品和产品变体的价格。为了让它正常工作,我需要为登录用户删除每个产品的产品瞬态。
这很好,但加载时间太长。这对性能的影响似乎很大。
代码如下:
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 );
有没有办法更有效地做到这一点?
非常感谢。