从 Woocommerce 中的自定义动态定价中排除产品类别

Exclude product categories from custom dynamic pricing in Woocommerce

我正在我的 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(或其名称为 "last minute gifts")。

如何为每个购物车商品添加此支票?

已更新…这是从您的自定义动态定价中排除产品类别的方法,只需对您的第一个挂钩函数进行小的更改。

它使用has_term() WordPress条件函数来排除产品类别,这样:

add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 10, 2 );
function add_cart_item_data( $cart_item_data, $product_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)){

        // Excluded product categories in this array (Can be IDs, slugs or names)
        $excl_cats = array( 978 );  

        $product = wc_get_product( $product_id );
        // Excluding product categories

        if( ! has_term( $excl_cats, 'product_cat', $product_id ) ){
            $cart_item_data['ref_price'] = $product->get_price() * 0.85;

            // Every add to cart action is set as a unique line item
            $cart_item_data['unique_key'] = md5( microtime().rand() );
        }
    }
    return $cart_item_data;
}


add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals', 10, 1 );
function before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        if( isset( $cart_item['ref_price'] ) {
            $cart_item['data']->set_price( floatval($cart_item['ref_price']) );
        }
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。

已测试并有效