Skip to content
This repository was archived by the owner on Feb 3, 2022. It is now read-only.

add a new option mode in NewCategory #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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');
}
}
16 changes: 13 additions & 3 deletions src/app/code/community/Meanbee/NewCategory/Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/app/code/community/Meanbee/NewCategory/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<meanbee_autocategories>
<new_category>
<enabled>0</enabled>
<use_created_at>1</use_created_at>
<days>14</days>
</new_category>
</meanbee_autocategories>
Expand Down
10 changes: 10 additions & 0 deletions src/app/code/community/Meanbee/NewCategory/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</category>
<use_created_at translate="label">
<label>Use created at instead of date news_from_date / news_to_date?</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</use_created_at>
<days translate="label comment">
<label>Days New</label>
<comment><![CDATA[The number of days a product will stay in the "New" category after it has been added.]]></comment>
Expand All @@ -37,6 +46,7 @@
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<depends><use_created_at>1</use_created_at></depends>
</days>
</fields>
</new_category>
Expand Down