【问题标题】:Exclude product categories from custom dynamic pricing in Woocommerce从 Woocommerce 中的自定义动态定价中排除产品类别
【发布时间】: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


    【解决方案1】:

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

    它使用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 文件中。

    经过测试并且可以工作

    【讨论】:

    • 除此之外,我在一个单独的文件中工作(通过包含在 function.php 中附加),这一切都得到了排序!完美运行
    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 2013-08-11
    • 2019-04-25
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    相关资源
    最近更新 更多