Viewing 15 replies - 1 through 15 (of 18 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hi There,
    Unfortunately there is no reliable way to do this. The price in the order is not tied to the product price (which is actually a good thing), and woocommerce does not save the regular price – this is different for discounts.
    You could check the price against the product price to see if was a sales price, but like I said, it’s not very reliable. It also depends on your tax settings whether this price actually makes sense (or if it even works at all!). That said, you can try this code in your themes functions.php:

    add_filter( 'wpo_wcpdf_order_item_data', 'wpo_wcpdf_order_regular_price', 10, 2 );
    function wpo_wcpdf_order_regular_price ( $item_data, $order) {
    	$sale_price = $item_data['product']->sale_price;
    	$current_price = $item_data['item']['line_subtotal'];
    	if ( !empty($sale_price) && $sale_price == $current_price) {
    		$item_data['order_price'] = sprintf('<s>%s</s><br/>', wc_price($item_data['product']->regular_price)).$item_data['order_price'];
    	}
    	return $item_data;
    }

    the strikethrough looks a bit funky on the simple template…

    Thread Starter developervkm

    (@developervkm)

    I am not that experienced with Woocommerce.Will adding this bit of code to functions.php give me the desired result or I will have to tweak invoice.php too?

    Plugin Contributor Ewout

    (@pomegranate)

    No this code is specifically for use in functions.php, it prepends the sale price to the price.

    Thread Starter developervkm

    (@developervkm)

    So basically you mean that I cannot have both the Sale Price and Regular Price exclusively?

    Plugin Contributor Ewout

    (@pomegranate)

    Yes you can, but with all the precautions from my first response :o)
    The code would output something like this:

    <strike>$15</strike> $10

    Thread Starter developervkm

    (@developervkm)

    I managed to do it using $item['product']->regular_price;
    Thanks for the lead. ??

    Plugin Contributor Ewout

    (@pomegranate)

    You’re welcome! I’d appreciate it if you can leave me a 5 star review :o)

    Thanks!

    I tried to add this code in the theme function file but nothing change I get. I put this code in functions.php in child theme

    Plugin Contributor Ewout

    (@pomegranate)

    Hi! Which code did you put in functions.php exactly? The code from the first answer was tested and should still work.

    I tried the code that you written above.
    But the output <strike>$15</strike> $10 if I order only one quantity of a product but when i order a product quantity two or more it don’t give me the <strike>regular price</strike> sale price

    Plugin Contributor Ewout

    (@pomegranate)

    Hi! That makes sense. The code assumes a single price, which is not always the case. Can you try this instead?

    add_filter( 'wpo_wcpdf_order_item_data', 'wpo_wcpdf_order_regular_price', 10, 2 );
    function wpo_wcpdf_order_regular_price ( $item_data, $order) {
    	$sale_price_single = $item_data['product']->sale_price;
    	if ( !empty($sale_price_single) ) {
    		$sale_price = $sale_price_single * $item_data['quantity'];
    		$current_price = $item_data['item']['line_subtotal'];
    		if ( $sale_price == $current_price) {
    			$item_data['order_price'] = sprintf('<s>%s</s><br/>', wc_price($item_data['product']->regular_price)).$item_data['order_price'];
    		}
    	}
    	return $item_data;
    }

    Thanks for the code it works great.

    Can we make the code to show the regular prices subtotal instead of single product price?

    Plugin Contributor Ewout

    (@pomegranate)

    Sure – I completely forgot that!

    Al you need to do is multiply $item_data[‘product’]->regular_price with the quantity. I’ve split it out so that it’s clearer:

    add_filter( 'wpo_wcpdf_order_item_data', 'wpo_wcpdf_order_regular_price', 10, 2 );
    function wpo_wcpdf_order_regular_price ( $item_data, $order) {
        $sale_price_single = $item_data['product']->sale_price;
        if ( !empty($sale_price_single) ) {
            $sale_price = $sale_price_single * $item_data['quantity'];
            $current_price = $item_data['item']['line_subtotal'];
            $regular_price = $item_data['product']->regular_price * $item_data['quantity'];
            if ( $sale_price == $current_price) {
                $item_data['order_price'] = sprintf('<s>%s</s><br/>', wc_price($regular_price)).$item_data['order_price'];
            }
        }
        return $item_data;
    }

    Have a fantastic weekend!

    Ewout

    Hi, I have added the code into template\pdf\simple\template-functions.php

    Do I have to edit invoice.php also? I would like to have discount deducted directly into single_price and price

    Plugin Contributor Ewout

    (@pomegranate)

    Hi! There’s a difference between discounted price and sale price: the sale price is always printed per item, whereas discounts are displayed in the totals. line_total and single_line_total hold the discounted price.

    Ewout

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Add Regular Price to the invoice’ is closed to new replies.