diff --git a/src/app/code/community/Meanbee/AutoCategories/Model/Auto/Category/Abstract.php b/src/app/code/community/Meanbee/AutoCategories/Model/Auto/Category/Abstract.php index e2f5e98..8d7556f 100644 --- a/src/app/code/community/Meanbee/AutoCategories/Model/Auto/Category/Abstract.php +++ b/src/app/code/community/Meanbee/AutoCategories/Model/Auto/Category/Abstract.php @@ -109,6 +109,7 @@ public function maintain($products = array()) { } $this->getConnection()->delete($this->getCategoryProductTable(), $where); + $this->getConnection()->delete($this->getCategoryProductIndexTable(), $where); // Add products if they match the filter if (!empty($products)) { @@ -126,6 +127,13 @@ public function maintain($products = array()) { $this->getConnection()->query($insert); + if(Mage::helper('catalog/category_flat')->isEnabled()){ + Mage::getSingleton('index/indexer')->getProcessByCode('catalog_category_product')->reindexEverything(); + } + // load category id $category_id then save + //$category = Mage::getModel('catalog/category')->load($category_id); + //$category->save(); + Mage::app()->setCurrentStore($originalStore); } @@ -155,4 +163,13 @@ protected function getConnection() { protected function getCategoryProductTable() { return Mage::getModel('core/resource')->getTableName('catalog/category_product'); } + + /** + * Return the name of the table used to store products in categories index. + * + * @return string + */ + protected function getCategoryProductIndexTable() { + return Mage::getModel('core/resource')->getTableName('catalog/category_product_index'); + } } diff --git a/src/app/code/community/Meanbee/NewCategory/Helper/Config.php b/src/app/code/community/Meanbee/NewCategory/Helper/Config.php index c82f589..1b38d45 100644 --- a/src/app/code/community/Meanbee/NewCategory/Helper/Config.php +++ b/src/app/code/community/Meanbee/NewCategory/Helper/Config.php @@ -2,9 +2,10 @@ class Meanbee_NewCategory_Helper_Config extends Mage_Core_Helper_Abstract { - const XML_PATH_IS_ENABLED = 'meanbee_autocategories/new_category/enabled'; - const XML_PATH_CATEGORY_ID = 'meanbee_autocategories/new_category/category'; - const XML_PATH_DAYS_NEW = 'meanbee_autocategories/new_category/days'; + const XML_PATH_IS_ENABLED = 'meanbee_autocategories/new_category/enabled'; + const XML_PATH_CATEGORY_ID = 'meanbee_autocategories/new_category/category'; + const XML_PATH_USE_CREATED_AT = 'meanbee_autocategories/new_category/use_created_at'; + const XML_PATH_DAYS_NEW = 'meanbee_autocategories/new_category/days'; /** * Check if NewCategory is enabled. @@ -15,6 +16,15 @@ public function isEnabled() { return (Mage::getStoreConfigFlag(self::XML_PATH_IS_ENABLED)) ? $this->checkDependencies() : false; } + /** + * Return use if is mode created_at enabled + * + * @return string + */ + public function getUseCreatedAt() { + return Mage::getStoreConfig(self::XML_PATH_USE_CREATED_AT); + } + /** * Return the id of the Magento category used for NewCategory. * diff --git a/src/app/code/community/Meanbee/NewCategory/Model/Auto/Category/New.php b/src/app/code/community/Meanbee/NewCategory/Model/Auto/Category/New.php index dc0ead5..20a4bbf 100644 --- a/src/app/code/community/Meanbee/NewCategory/Model/Auto/Category/New.php +++ b/src/app/code/community/Meanbee/NewCategory/Model/Auto/Category/New.php @@ -29,9 +29,32 @@ protected function getCategoryId() { * @return $this */ protected function applyFilter($collection) { - $cutoff_date = $this->getCutoffDate()->toString('YYYY-MM-dd HH:mm:ss'); + if($this->getConfig()->getUseCreatedAt()){ + $cutoff_date = $this->getCutoffDate()->toString('YYYY-MM-dd HH:mm:ss'); - $collection->addAttributeToFilter('created_at', array('gteq' => $cutoff_date)); + $collection->addAttributeToFilter('created_at', array('gteq' => $cutoff_date)); + + }else{ + $now = $this->getCurrentDate()->toString('YYYY-MM-dd HH:mm:ss'); + $collection->addAttributeToFilter("news_from_date", array( + array("notnull" => false), + array("lteq" => $now) + ), "left") + ->addAttributeToFilter("news_to_date", array( + array("null" => true), + array("gteq" => $now) + ), "left"); + + } + } + + /** + * Return the current date. + * + * @return Zend_Date + */ + protected function getCurrentDate() { + return Mage::app()->getLocale()->storeDate(Mage::app()->getStore(), null, true); } /** diff --git a/src/app/code/community/Meanbee/NewCategory/etc/config.xml b/src/app/code/community/Meanbee/NewCategory/etc/config.xml index 820a629..91cf429 100644 --- a/src/app/code/community/Meanbee/NewCategory/etc/config.xml +++ b/src/app/code/community/Meanbee/NewCategory/etc/config.xml @@ -37,6 +37,7 @@ 0 + 1 14 diff --git a/src/app/code/community/Meanbee/NewCategory/etc/system.xml b/src/app/code/community/Meanbee/NewCategory/etc/system.xml index f6ebd3e..0a82c62 100644 --- a/src/app/code/community/Meanbee/NewCategory/etc/system.xml +++ b/src/app/code/community/Meanbee/NewCategory/etc/system.xml @@ -29,6 +29,15 @@ 1 1 + + + select + adminhtml/system_config_source_yesno + 10 + 1 + 1 + 1 + @@ -37,6 +46,7 @@ 1 1 1 + 1