• Resolved Ibby

    (@ibby)


    Hi,
    I am trying to edit a specific category of products to make the archive page clearer to our customers. I want the pricing loop to only be amended for a specific category and only when certain variations are present. The category is ‘steaks’ and the variations are ‘per-steak’ and ‘per-kg’.

    For every other category/product type combination, I want the standard WC code to apply and I don’t want to have to write exceptions in my function for every different situation. My code currently looks like this:

    function iconic_variable_price_format( $price, $product ) {
        $prefix = '£';
    	
        $min_price_regular = $product->get_variation_regular_price( 'min', true );
        $min_price_sale    = $product->get_variation_sale_price( 'min', true );
        $max_price = $product->get_variation_price( 'max', true );
        $min_price = $product->get_variation_price( 'min', true );
    	$upload_dir = wp_get_upload_dir();
        $price = ( $min_price_sale == $min_price_regular ) ?
            wc_price( $min_price_regular ) :
            '<del>' . wc_price( $min_price_regular ) . '</del>' . '<ins>' . wc_price( $min_price_sale ) . '</ins>';
    	    
    	if ( is_product_category('steaks') && $product->is_type( 'variable' ) ) {
    		foreach ( $product->get_available_variations() as $key => $variation_data ) {
    			$weight = $variation_data['weight']; // Get weight from variation
    
    			return ( $min_price == $max_price ) ?
    					$price :
    					sprintf('<div class="per-piece"><img src="%s/2022/06/steak-var-icon.svg"> %s%s <span class="weight">/ %s</span></div><div class="per-kg"><img src="%s/2022/06/kg-var-icon.svg"> %s%s <span class="weight">per kg</span></div>', $upload_dir['baseurl'], $prefix, $min_price, $variation_data['weight_html'], $upload_dir['baseurl'], $prefix, $max_price);
        	} 
    	} elseif ( !is_product_category('steaks') && $product->is_type( 'variable' ) ) {
    		foreach ( $product->get_available_variations() as $key => $variation_data ) {
    			$price = ( $min_price_sale == $min_price_regular ) ? 
                    wc_price( $min_price_regular ) : 
                        '' . wc_price( $min_price_regular ) . '' . '' . wc_price( $min_price_sale ) . ''; 
                        return ( $min_price == $max_price ) ? 
                            $price : 
                                sprintf('%s', $price);
        	} 
    	}
    }
    add_filter( 'woocommerce_variable_sale_price_html', 'iconic_variable_price_format', 10, 2 );
    add_filter( 'woocommerce_variable_price_html', 'iconic_variable_price_format', 10, 2 );
    add_filter( 'woocommerce_show_variation_price', '__return_true' );
    
    function bd_rrp_price_html( $price, $product ) {
    	if ( is_product_category(16) && $product->is_type( 'simple' ) ) {
    		$upload_dir = wp_get_upload_dir();
    		$return_string = '<div class="per-kg"><img src="' . $upload_dir['baseurl'] . '/2022/06/kg-var-icon.svg"> '. $price .' <span class="weight">per kg</span></div>';
    		return $return_string;
    	} else {
    		$return_string = $price;
    		return $return_string;
    	}
    }
    add_filter( 'woocommerce_get_price_html', 'bd_rrp_price_html', 100, 2 );

    This is working well for the specific situations, but it is breaking other situations, for instance when a sale is placed on a variation within the category or for variation products outside of the category.

    Please could I have some help with this? Thank you!

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

Viewing 1 replies (of 1 total)
  • Hi @ibby

    Thanks for reaching out!

    I understand that you want to edit a specific category of products to make the archive page clearer to your customers using the custom code you wrote above.

    These forums are meant for general support with the core functionality of WooCommerce itself. What you want to achieve is a bit complex and would require customization to do it. Since custom coding is outside our scope of support, I am leaving this thread open for a bit to see if anyone can chime in to help you out.

    For questions related to development and custom coding, your best bet is to ask on any of these channels for support. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, too.

    WooCommerce Developer Resources Portal
    WooCommerce Advanced Facebook group
    WooCommerce Community Forum
    WooCommerce Developer Slack Channel.
    – Hire a WooCommerce Expert

Viewing 1 replies (of 1 total)

The topic ‘Editing variable pricing to add styling’ is closed to new replies.