skip to Main Content

I was just wondering if anyone has a solution for this problem I am currently having. I am actually trying to print a customise price on the shopping cart for my users who are accessing my online store. Below are the conditions which I plan to set.

user who are not logged in -> No Discount, not suppose to see the customise pricing display on my shopping cart page.

user who are logged in as "customer" user role -> No Discount, not suppose to see the customise pricing display on my shopping cart page.

user who are logged in as "paid_customer" user role -> Discount applied, suppose to see the customise pricing display on my shopping cart page.

Currently only users which are not logged in and "paid_customer" user role have their pricing display working correctly but not for "customer" role. Not sure if i am identifying the customer role correctly over here.

Below is the hook which I am using for this condition:

add_filter( 'woocommerce_cart_item_price', function ( $price, $values, $cart_item_key ){

        global $woocommerce;
        $items = $woocommerce->cart->get_cart();
        $user = wp_get_current_user();
    
    if (!is_user_logged_in() || (in_array( 'customer', (array) $user->roles ))){
        
        return $price;
        
    }else{
        
        foreach($items as $item => $values) {
            echo "Discounted Price : " . get_post_meta($values['product_id'] , ('_sale_price', true);       
            return $price;  
        }   
    }

}, 10, 3);

Edit:

My pricing discounts are already managed by Advanced dynamic pricing for WooCommerce plugin, based on my custom user roles, so I don’t need to worry about the price output anymore.

See my code answer below, based on @LoicTheAztec accepted answer.

3

Answers


  1. Chosen as BEST ANSWER

    This is what I did to accomplish what I wanted (thanks to @LoicTheAztec answer):

    add_filter( 'woocommerce_cart_item_price', 'filter_wc_cart_item_price', 10, 3 );
    
    function filter_wc_cart_item_price( $price, $values, $cart_item_key ){
    
        // For 'lifetime_member' user role when sale price is defined
        if ( current_user_can( 'paid_customer' )) {
        
            echo "Members Price (5% Off): ";
    
        } elseif (current_user_can('customer')){
            
            echo "Normal Price: ";
            
        } 
        
        return $price;
    }
    

    My pricing discounts are already managed by Advanced dynamic pricing for WooCommerce plugin, based on my custom user roles, so I don't need to worry about the price output anymore.


  2. Use this hook instead "woocommerce_before_calculate_totals"

    add_action("woocommerce_before_calculate_totals", "set_product_price");
    
    function set_product_price($cart_obj) {
        $user = wp_get_current_user();
        foreach ($cart_obj->get_cart() as $item) {
             if (!is_user_logged_in() || (in_array( 'customer', (array) $user->roles ))){
                   $item['data']->set_price(20);
             } else {
                   $item['data']->set_price(15);
             }
        }
    }
    

    In that way you no need to handle price during checkout and place order process.

    Login or Signup to reply.
  3. You should mention in your question which plugin you are using, as questions / answers on StackOverFlow are for the hole community and not just for you.

    Your code has some mistakes and can be simplified:

    add_filter( 'woocommerce_cart_item_price', 'filter_wc_cart_item_price', 10, 3 );
    function filter_wc_cart_item_price( $price, $values, $cart_item_key ){
    
        // For 'paid_customer' user role when sale price is defined
        if ( current_user_can( 'paid_customer' ) && $sale_price = get_post_meta( $values['product_id'],'_sale_price', true) ) {
        
            // The custom formatted sale price
            $price = sprintf( __("Discounted Price : %s"), wc_price( $sale_price ) );
        }
        return $price;
    }
    

    It should work.


    An edit to your answer code that is incorrect as the data in filter hooks needs always to be returned, but not echoed:

    add_filter( 'woocommerce_cart_item_price', 'filter_wc_cart_item_price', 10, 3 );
    function filter_wc_cart_item_price( $price, $values, $cart_item_key ){
    
        // For 'lifetime_member' user role when sale price is defined
        if ( current_user_can( 'paid_customer' ) ) 
        {
            $price = __("Members Price (5% Off): ") .  $price;
        } 
        elseif ( current_user_can('customer') ) 
        {
            $price = __("Normal Price: ") .  $price;
        } 
        return $price;
    }
    

    This is the correct and working way.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search