我正在尝试更改一位已登录客户的一种产品的价格。使用此代码时,价格确实会发生变化,但仅限于产品页面。我需要在任何地方更改价格(档案、产品页面、购物车等)
这是我正在尝试运行的代码。
add_filter( 'woocommerce_product_get_price', 'special_discount_for_product_cat_for_logged_in_customers', 10, 2 );
function special_discount_for_product_cat_for_logged_in_customers( $price, $product ) {
$current_user_id = get_current_user_id();
if( $current_user_id == 433 && is_user_logged_in() ) {
if (is_product( '11323', $product->get_id())) {
$price = 9.95;
}
}
return $price;
}
如有任何帮助,我们将不胜感激。
请尝试以下操作,这将改变特定产品和特定用户 ID 的产品价格:
add_filter( 'woocommerce_product_get_price', 'special_discount_for_product_cat_for_logged_in_customers', 10, 2 );
function special_discount_for_product_cat_for_logged_in_customers( $price, $product ) {
// YOUR SETTINGS
$targeted_user_id = 433;
$targeted_product_id = 11323;
if( get_current_user_id() == $targeted_user_id && $product->get_id() == $targeted_product_id ) {
$price = 9.95;
}
return $price;
}
代码位于活动子主题(或活动主题)的functions.php 文件中。已测试并可以使用。