Hi, we had a little problem trying to get discounts on Products using Rules and Views.

We have made a simple discount to a Product (say 50% of 10€) applying a simple price calculation rule using the SIMPLE DISCOUNT TUTORIAL explained in this page http://www.drupalcommerce.org/node/461

Our issue comes in two scenarios. To get a better picture of what we are trying to do here, please take a look at the attached screenshot below.

// SCENARIO 1

When viewing the Product in Full Node, the PRICE printed by our node.tpl.php in the Content Region is 5€ (good) but the PRICE printed by Views in the Sidebar region is 2.50€ so the price rule is executing twice. We had our Views Price field configured as Formatted amount/Display the calculated sell price for the current user. But the only way we solved this problem in by configuring the price as Formatted amount/Display the original price as loaded.

Shouldn't the first configuration of the Price Field be the correct?

// SCENARIO 2

When viewing another Product from the SAME CATEGORY, if we look at the 2 Views in the Content Bottom Region (check attached screenshot), our discounted Product is there in BOTH Views (because it's from the same Category AND is a New Product).

If we say to both Views to display the content TEASER, the PRICE for the Product in our first View is 5€ (good) but the PRICE of the same product on the second View is 2.50€ so the price rule is executing twice. We have printed the PRICE in our node.tpl.php in the same way that we did in the Full Node...
print render($content['product:commerce_price']);
...and in the Manage Display of that field in Teaser Mode we have set it to Formatted amount/Displaying a calculated price.

If we use Formatted amount/Displaying the original price both prices show as 10€ so no 50% discount is made.

The only way we had to solve this is by saying to both Views to display as fields, and add the (Product) Commerce Product: Price configured as Formatted amount/Display the calculated sell price for the current user. Then and only then both Products will have 5€ as price.

So we don't understand the logic behind all this:

- Why in our Sidebar we have to configure the View price as Display the original price as loaded but in our ContentBottom we have to configure the View price (using fields) as Display the calculated sell price for the current user?

- Why can't we use the TEASER CONTENT in our Content Bottom Views?

Thanx in advance, we are worried our solution is not good at all.

CommentFileSizeAuthor
ISSUE_Product_Discounts.jpg84.42 KBEndEd

Comments

EndEd’s picture

Our main problem here is that we want to use the TEASER CONTENT in some of our Views so they share the same phpcode/markup in our node.tpl.php.

rszrama’s picture

Status: Active » Fixed

The problem is that currently hook_commerce_price_field_formatter_prepare_view() can be invoked more than once if hook_field_formatter_prepare_view() is invoked multiple times on a pageload. The Product Pricing module implements our Commerce Price version of the hook to run the price through Rules to get a calculated price. In your case (which I duplicated by just adding a View of product nodes to the footer), it appears Views is executing that hook with the same price data a second time, cause the pricing rules to apply twice.

My simple fix for now was to check to ensure the same price isn't calculated twice in the Product Pricing module. It turns out I actually have a TODO item in the Price module that when addressed will be able to resolve this issue a little higher up. However, I didn't want to get into changing things there, especially since the TODO is for a slightly different use case (preventing alteration when showing a price field formatted with its components). I added documentation so developers know that implementations of this hook are currently responsible for their own duplicate prevention.

Commit: http://drupalcode.org/project/commerce.git/commitdiff/26020b7

EndEd’s picture

Thanks rszrama, this simple fix is all we need :) Working great

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.