• Resolved pandarbalette

    (@pandarbalette)


    Hello, I currently manually price products depending on their weight (ie. 7,90 euros for 1 gram, 250 euros for 5 grams, 450 euros for 100 grams, etc). There is an economy of scale that I would like to emphasize. Therefore I would like to display the cheapest price per gram (in this case, “from 4,5 euros per gram”) on the archive page (only!), as I′d prefer not to edit the products features (prices must remain displayed upon the manually chosen prices). Is there any way to do that? Thank you.

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • @pandarbalette

    You can create variations in this case: https://woocommerce.com/document/variable-product/

    Thread Starter pandarbalette

    (@pandarbalette)

    The product already have variations, i would like to know if it’s possible to personalize the price displayed in “woocommerce product price”. but without changing the real price of the product.

    Mirko P.

    (@rainfallnixfig)

    Hello @pandarbalette,

    I understand that you’d like to remove the price range in the variable product page and display something like “From $ xxx per gram”, correct?

    You may want to use a code snippet like this one, as found in this Stack Overflow thread.

    add_filter( 'woocommerce_get_price_html', 'change_variable_products_price_display', 10, 2 );
    function change_variable_products_price_display( $price, $product ) {
    
        // Only for variable products type
        if( ! $product->is_type('variable') ) return $price;
    
        $prices = $product->get_variation_prices( true );
    
        if ( empty( $prices['price'] ) )
            return apply_filters( 'woocommerce_variable_empty_price_html', '', $product );
    
        $min_price = current( $prices['price'] );
        $max_price = end( $prices['price'] );
        $prefix_html = '<span class="price-prefix">' . __('From: ') . '</span>';
    
        $prefix = $min_price !== $max_price ? $prefix_html : ''; // HERE the prefix
    
        return apply_filters( 'woocommerce_variable_price_html', $prefix . wc_price( $min_price ) . $product->get_price_suffix(), $product );
    }

    As a side note, I would recommend using a plugin like Code Snippets to add custom PHP code into your site without directly accessing the functions.php file.

    I hope this helps!

    Mirko P.

    (@rainfallnixfig)

    Hi there,

    We haven’t heard from you in a while, so I’m going to mark this as resolved. Feel free to start a new thread if you have any more questions.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom price in product archive’ is closed to new replies.