【发布时间】:2018-02-06 00:25:29
【问题描述】:
我正在我的 Wordpress 商店开发一个动态定价系统。我已将其设置为具有特定角色的用户(现在,订阅者或管理员 - 用于测试目的)获得 15% 的折扣(价格*0.85)。但是,我还需要从折扣规则中排除特定的产品类别。
现在我有:
function add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
//if admin || subscriber
$role = get_user_role(); /* I know that isn't the default get user role. I made a shortened version of it in functions.php because I used it a lot in other functions */
if (in_array("admin", $role) || in_array("subscriber", $role)){
$product = wc_get_product( $product_id );
// ==> Start: Needed product category check HERE
$price = $product->get_price();
$cart_item_data['RefPrice'] = $price * 0.85;
// ==> End of product category check
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 10, 2 );
function before_calculate_totals( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
// Iterate through each cart item
foreach( $cart_obj->get_cart() as $key=>$value ) {
if( isset( $value['RefPrice'] ) ) {
$price = $value['RefPrice'];
$value['data']->set_price( ( $price ) );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals', 10, 1 );
这正在通过 * 0.85 修改某些角色的价格,但我在试图从中排除产品类别时遇到了麻烦。产品类别 ID 为 978(或名称为“最后一分钟的礼物”)。
如何为每个购物车项目添加此检查?
【问题讨论】:
标签: php wordpress woocommerce cart price