• Resolved shubh1976

    (@shubh1976)


    I want to Customize WooCommerce Product Prices (having Simple and Variable Products) appearance in Single Product Page, Category Page, Search Page,Shop Page and Other Pages (Excluding Checkout Page and Cart Page) in following Way:

    Simple Product a: No Sales Price Defined i) Product Detail Page (Single Product Page): Show Regular Price ii) Category, Search and other Pages (except Cart and Checkout) Show Regular Price (Same as above) b: Sales Price Defined i) Product Detail Page: Show Sale Price (Strike Off Regular Price) ii) Category, Search and other Pages (except Cart and Checkout) Show Sales Price
    Variable Product a: Default Variation Not defined Case 1: No Sales Price Defined i) Product Detail Page (Single Product Page): Show lowest Variation’s Regular Price ii) Category, Search and other Pages (except Cart and Checkout) Show lowest Variation’s Regular Price (Same as above) Case 2: Sales Price Defined i) Product Detail Page (Single Product Page): Show lowest Variation’s Price (From Combining Sales & Regular Prices) I) if Lowest Price is Regular Price show only Regular Price II) if Lowest Price is Sales Price show Sale Price (Strike Off Regular Price of same Variation ID) ii) Category, Search and other Pages (except Cart and Checkout) Show only lowest Variation’s Price (From Combining Sales & Regular Prices) b: Default Variation defined Case 1: No Sales Price Defined i) Product Detail Page (Single Product Page): Show Default Variation’s Regular Price ii) Category, Search and other Pages (except Cart and Checkout) Show Default Variation’s Regular Price (Same as above) Case 2: Sales Price Defined i) Product Detail Page (Single Product Page): Show Default Variation’s Sales Price (Strike Off Regular Price of same Variation ID) ii) Category, Search and other Pages (except Cart and Checkout) Show Default Variation’s Sales Price
    Its a Ecommerce Wite built using WooCommerce and site running on Plex.

    I have tried following code:

    
    add_filter( 'woocommerce_get_price_html', 'custom_price_html', 10, 2 );
    function custom_price_html(){
        global $product;
        //echo $product->get_id();
        $user = wp_get_current_user();
        $allowed_roles = array('editor', 'administrator', 'author');
        if( array_intersect($allowed_roles, $user->roles ) && !is_product() ) {
            if($product->is_type('simple')){
                $regular_price=$product->get_regular_price('min');
                $sale_price=$product->get_sale_price('min');
                if(!empty($sale_price)){
                    $formatted_price='<del><span class="woocommerce-Price-amount amount">'.number_format((float)$regular_price, 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></del> ';
                    $formatted_price.=' <ins><span class="woocommerce-Price-amount amount">'.number_format((float)$sale_price, 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></ins>';
                    return $formatted_price;
                }else{
                    $formatted_price = number_format((float)$regular_price, 2, '.', '') .get_woocommerce_currency_symbol();
                    return $formatted_price;
                }
            }
            if($product->is_type('variable')){
    
            }
        }
        if( $product->is_type('simple') && !array_intersect($allowed_roles, $user->roles )){
            $regular_price=$product->get_regular_price('min');
            $sale_price=$product->get_sale_price('min');
            if(is_product()){
                if(!empty($sale_price)){
                    $formatted_price='<del><span class="woocommerce-Price-amount amount">'.number_format((float)$regular_price, 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></del> ';
                    $formatted_price.=' <ins><span class="woocommerce-Price-amount amount">'.number_format((float)$sale_price, 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></ins>';
                    return $formatted_price;
                }else{
                    $formatted_price = number_format((float)$regular_price, 2, '.', '') .get_woocommerce_currency_symbol();
                return $formatted_price;
                }
            }
            if(is_product_category() || is_search()){
                if($sale_price < $regular_price){
                    return number_format((float)$sale_price, 2, '.', '') .get_woocommerce_currency_symbol();
                }else{
                    return number_format((float)$regular_price, 2, '.', '') .get_woocommerce_currency_symbol();
                }
            }
        }
        if( $product->is_type('variable') && !array_intersect($allowed_roles, $user->roles )){
            $default_attributes = $product->get_default_attributes($product);
            $default_variation_id = custom_find_matching_product_variation( $product, $default_attributes );
            if(empty($default_variation_id)){
                $child_prices     = array();
                foreach ( $product->get_children() as $child_id ) {
                    $sale_price=get_post_meta( $child_id, '_sale_price', true );
                    if(!empty($sale_price)){
                        $child_prices['sales'][$child_id] = get_post_meta( $child_id, '_sale_price', true );
                    }
                    $child_prices['regular'][$child_id] = get_post_meta( $child_id, '_regular_price', true );
                }
                if(!empty($child_prices['sales'])){
                    asort($child_prices['sales']);
                    $lowest_salse_variation_id=key($child_prices['sales']);
                    $lowest_salse_price=$child_prices['sales'][$lowest_salse_variation_id];
                }
    
                asort($child_prices['regular']);
                $lowest_regular_variation_id=key($child_prices['regular']);
                $lowest_regular_price=$child_prices['regular'][$lowest_regular_variation_id];           
            }
    
            if(is_product()){
                $children=$product->get_children();
                if(!in_array($product->get_id(),$children)){
                    if(!empty($default_variation_id)){
                        $sale_price = get_post_meta($default_variation_id, '_sale_price', true);
                        $regular_price = get_post_meta($default_variation_id, '_regular_price', true);
                        if(!empty($sale_price)){
                            $formatted_price='<del><span class="woocommerce-Price-amount amount">'.number_format((float)$regular_price, 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></del> ';
                            $formatted_price.=' <ins><span class="woocommerce-Price-amount amount">'.number_format((float)$sale_price, 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></ins>';
                            return $formatted_price;
                        }else{
                            return number_format((float)$regular_price, 2, '.', '') .get_woocommerce_currency_symbol();
                        }
                    }else{
                        if(!empty($lowest_salse_price) && ($lowest_salse_price < $lowest_regular_price)){
                                $formatted_price='<del><span class="woocommerce-Price-amount amount">'.number_format((float)$child_prices['regular'][$lowest_salse_variation_id], 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></del> ';
                                $formatted_price.=' <ins><span class="woocommerce-Price-amount amount">'.number_format((float)$lowest_salse_price, 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></ins>';
                                return $formatted_price;
                        }else{
                            if(!empty($child_prices['sales']) && array_key_exists($lowest_regular_variation_id,$child_prices['sales'])){
                                $formatted_price='<del><span class="woocommerce-Price-amount amount">'.number_format((float)$lowest_regular_price, 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></del> ';
                                $formatted_price.=' <ins><span class="woocommerce-Price-amount amount">'.number_format((float)$child_prices['sales'][$lowest_regular_variation_id], 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></ins>';
                                return $formatted_price;
                            }else{
                                return number_format((float)$lowest_regular_price, 2, '.', '') .get_woocommerce_currency_symbol();
                            }
                        }
                    }
                }
            }
    
            if(is_product_category() || is_search()){
                if(empty($default_variation_id)){
                    if(!empty($lowest_salse_price) && ($lowest_salse_price < $lowest_regular_price)){
                            return ' <ins><span class="woocommerce-Price-amount amount">'.number_format((float)$lowest_salse_price, 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></ins>';
                    }else{
                        if(!empty($child_prices['sales']) && array_key_exists($lowest_regular_variation_id,$child_prices['sales'])){
                            return ' <ins><span class="woocommerce-Price-amount amount">'.number_format((float)$child_prices['sales'][$lowest_regular_variation_id], 2, '.', '').'<span class="woocommerce-Price-currencySymbol">'.get_woocommerce_currency_symbol().'</span></span></ins>';
                        }else{
                            return number_format((float)$lowest_regular_price, 2, '.', '') .get_woocommerce_currency_symbol();
                        }
                    }
                }else{
                    $sale_price = get_post_meta($default_variation_id, '_sale_price', true);
                    $regular_price = get_post_meta($default_variation_id, '_regular_price', true);
                    if(!empty($sale_price)){
                        return number_format((float)$sale_price, 2, '.', '') .get_woocommerce_currency_symbol();
                    }else{
                        return number_format((float)$regular_price, 2, '.', '') .get_woocommerce_currency_symbol();
                    }
                }
            }
        }
    }
    
    function custom_find_matching_product_variation( $product, $attributes ) {
        foreach( $attributes as $key => $value ) {
            if( strpos( $key, 'attribute_' ) === 0 ) {
                continue;
            }
            unset( $attributes[ $key ] );
            $attributes[ sprintf( 'attribute_%s', $key ) ] = $value;
        }
        if( class_exists('WC_Data_Store') ) {
            $data_store = WC_Data_Store::load( 'product' );
            return $data_store->find_matching_product_variation( $product, $attributes );
        } else {
            return $product->get_matching_variation( $attributes );
        }
    }

    Everything goes fine except probably due to using “woocommerce_get_price_html” function, on Single Product Page for a variable product showing same Price for all variations which is actually customized for Price to be shone next to Product Title.

    E.g.: Case1: If the product have 4 Variations: Var1 reg price $50, Var2 reg price $80 sale price $40, var3 reg price $75 sale price $60 and var4 reg price $100. This product has no default variation set. The Price on Single Product should appear like ($80)striked off then $40. Which is appearing correct. But issue is in the dropdown when I select variation var1, the price shown below is ($80)striked off then $40 where as it should show $50. For all the variations when changed from var1 to var4, everytime it shows ($80)striked off then $40.

    Case2: If the product have 4 Variations: Var1 reg price $50, Var2 reg price $80 sale price $40, var3(set default) reg price $75 sale price $60 and var4 reg price $100. The Price on Single Product should appear like ($75)striked off then $60. Which is appearing correct. But issue is in the dropdown when I select variation var1, the price shown below is ($75)striked off then $60 where as it should show $50. For all the variations when changed from var1 to var4, everytime it shows ($75)striked off then $60.

    That is the main issue.

    If don’t use “woocommerce_get_price_html” and use “woocommerce_variable_price_html” and “woocommerce_dropdown_variation_attribute_options_html” for variable products and “woocommerce_single_price_html” for Simple products, Simple product price formatting remains unchanged in Category Page, Shop Page, Search result and other pages where products appears in loop.

    I am trying various examples posted in various forums including stackoverflow too, has no luck.

    I will be great full if someone can provide any solution to solve the issue.

    I am doing all this by creating a plugin for this purpose only.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Custom Pricing for Simple and Variable Products, in Single, Category and other’ is closed to new replies.