1 回答

TA貢獻1827條經驗 獲得超9個贊
你不需要你的功能check_user_role()
,因為你可以用同樣的方式使用 WordPress?current_user_can()
。
自 WooCommerce 3 以來,該掛鉤也woocommerce_get_price
已棄用。
要獲得自定義批發(fā)商價格,您只需使用元鍵,wholesaler_price
例如:
1) 使用對象get_meta()
上的方法(自 WooCommerce 3 起):WC_Product
$price?=?(float)?$product->get_meta(?'wholesaler_price'?);
get_post_meta()
2) 或者在功能中使用產品 ID?(WordPress 舊方法):
$price?=?(float)?get_post_meta(?$product->get_id()??'wholesaler_price',?true?);
現在在你相關的鉤子代碼函數中:
/* Custom prices by user role */
add_filter('woocommerce_product_get_price', 'custom_price_assign', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price_assign', 10, 2); // For product variations (optional)
function custom_price_assign( $price, $product ) {
? ? // Check if the user has a role of wholesaler
? ? if ( current_user_can('wholesaler') && $wholesaler_price = $product->get_meta('wholesaler_price') ){
? ? ? ? return $wholesaler_price;
? ? }
? ? return $price;
}
代碼進入您的活動子主題(或活動主題)的 functions.php 文件。測試和工作。
- 1 回答
- 0 關注
- 136 瀏覽
添加回答
舉報