GordonLesti / Lesti_Fpc

Simple Magento Fullpagecache
https://gordonlesti.com/lesti-fpc-documentationversion-1-4-5/
Other
358 stars 159 forks source link

auto-update product stocks #19

Closed kek0 closed 11 years ago

kek0 commented 11 years ago

Thanks for share your work!

I am showing the real stock in numbers in the frontpage.

I have a custom develpment extension that show the stock of all the combinations in a configurable product. In concret are t-shirts with differents color/sizes, and when you click in any color, appear the disponibility quanty of each size of this color.

I was tried to modify the stock in one simple product assigned to a configurable product, but still showing the old stock because is cached too.

Mabye you can add an option for auto-refresh the information in the cached files regarding about the stocks when was changed in any product.

thanks!

ndenos commented 11 years ago

I've extended LestiFpc code to refresh the cache when the stock is updated. this code with configurable products too. In app/code/community/Lesti/Fpc/etc/config.xml add a new observer hook in <events> block :

`

fpc/observer singleton catalogInventoryStockItemSaveAfter
        </cataloginventory_stock_item_save_after>`

In app/code/community/Lesti/Fpc/Model/Observer.php add

public function catalogInventoryStockItemSaveAfter($observer)
{
    $fpc = $this->_getFpc();

    if ($fpc->isActive()) {
        $item = $observer->getItem();

        if($item->getStockStatusChangedAuto() || ($item->getOriginalInventoryQty() <= 0 && $item->getQty() > 0 && $item->getQtyCorrection() > 0)) //If the stock status changed
        {
           // load and decache parent of configurable products
           $parentIdsConfigurable = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($item->getProductId());
           $parentIdsGrouped = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($item->getProductId());
           $parentIds = array_merge($parentIdsConfigurable,$parentIdsGrouped);
           if($parentIds)
           {
              foreach ($parentIds as $id)
              {
                 $product = Mage::getModel('catalog/product')->load($id);
                 $fpc->clean(sha1('product_' . $product->getId()));
              }
           }
           $product = Mage::getModel('catalog/product')->load($item->getProductId());
           $fpc->clean(sha1('product_' . $product->getId()));
        }
    }
 }

I'm not a pro magento developer so I don't know how to write a beautiful custom module overriding Lesti's one, but I hope it will help you

GordonLesti commented 11 years ago

@ndenos thank you very much, I did use your code to improve the behavior of fpc. I will close this issue.