2

I am trying to update my product prices against each store. When I do that, it shows me correct price on product view page and incorrect price on category view page. And some times it shows prices in regular price and special price form on category view page but it always shows correct price on product view page. How to remove incorrect prices? Here is my code. Thank You

$prod = Mage::getModel('catalog/product')
                ->setStoreId($storeId)
                ->load($prodId); 
$prod->setPrice($newPrice)->getResource()->saveAttribute($prod, 'price');
$prod->setSpecialPrice('')->getResource()->saveAttribute($prod, 'special_price');
$prod->setSpecialFromDate('')->getResource()->saveAttribute($prod, 'special_from_date');
$prod->setSpecialToDate('')->getResource()->saveAttribute($prod, 'special_to_date');

Screen shots: correct price on product view page incorrect price on category view page

6
  • 1
    Hi, after changing prices you have to run price index. Category view takes the price from index, product view directly from product. Commented Nov 18, 2014 at 7:20
  • Hi Stefan, thank you for answer. I am new in Magento so If you can please tell me how to run price index, it would be a great help for me.
    – umair ayub
    Commented Nov 18, 2014 at 7:24
  • Hi, depends on magento version. Tell me yours. Commented Nov 18, 2014 at 7:37
  • I am trying to reindex product price but it showing me an error. There was a problem with reindexing process.
    – umair ayub
    Commented Nov 18, 2014 at 7:43
  • Where have you put this code? Why are you calling ->load(), when this kills your performance? You have several ways to set promotion prices, why are you not using one of them (temporary price on the product and catalog price rules)?
    – user4351
    Commented Nov 18, 2014 at 8:02

2 Answers 2

7

It is not possible to have store view pricing in Magento.
You can switch price scope to website in the system configuration at Catalog > Price > Catalog Price Scope.

Customizing Magento to use store scope pricing is possible but it is a lot of work, breaks compatibility with many extensions, and makes the system quite brittle and hard to update, so it is not advisable.
A better aproach is to create more websites with one store view each to separate the pricing between the,.

Also, regarding the differences in the display between the product listing pages and the product detail page, in the listing pages the collection joins in the price index table to fetch all pricing information, which is then set on the product model as the final_price property.

On product detail pages the final price is calculated on the fly, based on the EAV price attributes (price, special_price, special_from_date, special_to_date, group_price) and the tax settings and customer group and product tax class ids.

My advice again is to rethink your approach.

My preferred way of doing catalog price adjustments is via observer. Two event need to be observed to make it work.

The first event is catalog_product_load_after. In this event observer method you can make price adjustments for the product detail page, for example.

public function catalogProductLoadAfter(Varien_Event_Observer $event)
{
    $product = $event->getData('product');
    $product->setPrice(19.99); // example values...
    $product->setSpecialPrice(17.99);
}

The second event that needs to be observed is for product listing pages and for the cart items: catalog_product_collection_load_after.

public function catalogProductCollectionLoadAfter(Varien_Event_Observer $event)
{
    $products = $event->getData('collection');
    foreach ($products as $product) {
        $product->setPrice(19.99);

        // set the final_price for the value used on product listing pages
        $product->setFinalPrice(17.99);

        // set the regular price data for the cart items because the final_price
        // will be recalculated on the fly
        $product->setSpecialPrice(17.99);
    }
}

Please note that the above code is just written here, its not tested and then copied over, so it might contain typos. However the approach works.

One thing to be aware of using this approach of changing the product prices on the fly is that it does not work with the layered navigation price filter.
The price segments displayed in the layered navigation are precalculated, and adjusting them requires an indexer rewrite, which is beyond the scope of this answer.

4
  • Hi vinai, thanks for your advice. But that is not the issue because I have already changed the price scope to website and I also have separate website for each store. Second thing you told me about the difference between prices, I did not get it. can you please elaborate how to remove that difference? Thank You
    – umair ayub
    Commented Nov 18, 2014 at 10:53
  • No, I can't elaborate, because teh answer always is "it depends". You don't specify how you are hooking your code into Magento. Are you using rewrites? Observers? Template hacks? Include path overrides? Core hacks? Something else? I'll expand my answer a little to let you know how to do it using observers.
    – Vinai
    Commented Nov 18, 2014 at 11:00
  • I am doing this in backend.
    – umair ayub
    Commented Nov 18, 2014 at 11:37
  • You did not answer my question with your comment.
    – Vinai
    Commented Nov 18, 2014 at 13:47
2

Reindexing is required of product prices.

2
  • 1
    Why did you accept your own answer when Vinai's made a lot more sense? It's a tad disrespectful since he took the time to give you a well thought out answer. Commented Jan 30, 2015 at 15:31
  • 1
    @umair just reindexing required.. is true sometime i face the same problem. Almost many thing in magento just solved by reindexing and caching if dont know why it is doing so. Commented Jul 16, 2015 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.