Skip to content

Instantly share code, notes, and snippets.

@jreinke
Created May 5, 2014 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jreinke/d9c14b04549f7b1121c0 to your computer and use it in GitHub Desktop.
Save jreinke/d9c14b04549f7b1121c0 to your computer and use it in GitHub Desktop.
Bubble Dynamic Category Sample
<?php
/**
* @category Bubble
* @package Bubble_DynamicCategory
* @version 1.0.1
* @copyright Copyright (c) 2014 BubbleShop (http://www.bubbleshop.net)
*/
class Bubble_DynamicCategory_Helper_Data extends Mage_Core_Helper_Abstract
{
public function getDynamicProductIds(Varien_Object $category)
{
$ids = array();
$conds = $category->getDynamicProductsConds();
if (!empty($conds)) {
if (is_string($conds)) {
$conds = unserialize($conds);
}
/** @var $rule Mage_CatalogRule_Model_Rule */
$rule = Mage::getModel('catalogrule/rule');
$rule->setWebsiteIds(array_keys(Mage::app()->getWebsites(true)));
$rule->loadPost(array('conditions' => $conds));
$productIds = $rule->getMatchingProductIds();
foreach ($productIds as $productId => $validationByWebsite) {
if (false !== array_search(1, $validationByWebsite, true)) {
$ids[] = $productId;
}
}
}
return $ids;
}
public function getDynamicProductCollection(Varien_Object $category)
{
$productIds = $this->getDynamicProductIds($category);
$collection = Mage::getResourceModel('catalog/product_collection');
if (!empty($productIds)) {
$collection->addIdFilter($productIds);
} else {
$collection->addIdFilter(array(0)); // Workaround for empty collection
}
return $collection;
}
public function getCategoryProductCount(Varien_Object $category)
{
$resource = Mage::getResourceModel('catalog/category_indexer_product');
$conn = $resource->getReadConnection();
$select = $conn->select()
->distinct(true)
->from($resource->getMainTable(), array('product_id'))
->where('category_id = :category_id');
$productIds = $conn->fetchCol($select, array('category_id' => $category->getId()));
$productIds = array_unique(array_merge($productIds, $this->getDynamicProductIds($category)));
return count($productIds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment