Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean cache for categories on stock change for variants #281

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions app/code/community/Lesti/Fpc/Model/Observer/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function catalogProductSaveAfter($observer)
if ($this->_getFpc()->isActive()) {
$product = $observer->getEvent()->getProduct();
if ($product->getId()) {
$this->_getFpc()->clean(sha1('product_' . $product->getId()));

$tags = $this->_getAllProductTags($product);
$this->_getFpc()->clean($tags);
$origData = $product->getOrigData();
if (empty($origData)
|| (!empty($origData) && $product->dataHasChangedFor('status'))
Expand Down Expand Up @@ -156,4 +156,55 @@ protected function _catalogProductSaveAfterMassAction(array $productIds)
$this->_getFpc()->clean($tags);
}
}

/**
* @param Mage_Catalog_Model_Product $product
*/
protected function _getAllProductTags($product)
{
$tags = array();
if (!is_object($product) || !$product->getId()) {
return $tags;
}

$tags[] = sha1('product_' . $product->getId());
$stockItem = $product->getStockItem();
$parentIds = Mage::getModel('catalog/product_type_configurable')
->getParentIdsByChild($product->getId());
// Clean tag for partner products, if any
foreach ($parentIds as $pid) {
$tags[] = sha1('product_' . $pid);
}

if ($parentIds) {
// Clean cache for categories on parent products if stock has changed for simple products
if ($stockItem && $stockItem->dataHasChangedFor('is_in_stock')) {
foreach ($this->_getCategories($parentIds) as $catId) {
$tags[] = sha1('category_' . $catId);
}
}
}

return $tags;
}

/**
* @param array $producIds
*/
protected function _getCategories($productIds)
{
if (!is_array($productIds)) {
$productIds = array($productIds);
}

$catRes = Mage::getResourceModel('catalog/category');
$table = $catRes->getTable('catalog/category_product');
$conn = $catRes->getReadConnection();

$query = $conn->select()->from($table, array('category_id'))->where('product_id IN (?)', $productIds);
$results = $conn->fetchCol($query);

return $results;
}

}