繁体   English   中英

从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(或其名称为“最后一刻的礼物”)。

如何为每个购物车项目添加此检查?

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

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

经过测试和工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM