Skip to content

Instantly share code, notes, and snippets.

@amacgregor
Created August 9, 2011 15:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amacgregor/1134407 to your computer and use it in GitHub Desktop.
Save amacgregor/1134407 to your computer and use it in GitHub Desktop.
Observer.php
<?php
/**
* Demac Media
*
* NOTICE OF LICENSE
*
* This source file is subject to the EULA
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.demacmedia.com/LICENSE-Magento.txt
*
* @category Demac
* @author Allan MacGregor (allan@demacmedia.com)
* @package Demac_Crons
* @copyright Copyright (c) 2010-2011 Demac Media (http://www.demacmedia.com)
* @license http://www.demacmedia.com/LICENSE-Magento.txt
*/
//TODO: Add Configuration options for selecting the category
class Demac_Crons_Model_Observer
{
public function addNewProducts()
{
$datefrom = date('Y-m-d H:i:s', strtotime('-30 days'));
$dateto = date('Y-m-d H:i:s');
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addFieldToFilter('created_at', array('from' => $datefrom, 'to' => $dateto));
$this->addToCategory($products, 71);
return $this;
}
public function addSaleProducts()
{
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('special_price', array('neq' => ""));
$this->addToCategory($products, 58);
}
public function removeNewProducts()
{
$datefrom = date('Y-m-d H:i:s', strtotime('-60 days'));
$dateto = date('Y-m-d H:i:s', strtotime('-30 days'));
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addFieldToFilter('created_at', array('from' => $datefrom, 'to' => $dateto));
$this->removeFromCategory($products, 71);
}
public function removeSaleProducts()
{
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('category_ids',array('finset'=>'85'))->addAttributeToFilter('special_price', array('eq' => ""));
$this->removeFromCategory($products, 58);
}
public function addToCategory($products, $catId)
{
foreach($products as $product)
{
$categoryIds = $product->getCategoryIds();
if(!in_array($catId, $categoryIds)){
$categoryIds[] = $catId;
$product->setCategoryIds($categoryIds);
$product->save();
Mage::log('ADDED - Product: ' . $product['sku'], null, 'demac_crons.log');
}
}
}
public function removeFromCategory($products, $catId)
{
foreach($products as $product)
{
$categoryIds = $product->getCategoryIds();
foreach($categoryIds as $key => $value)
{
if ($value == $catId) unset($categoryIds[$key]);
}
$product->setCategoryIds($categoryIds);
$product->save();
Mage::log('REMOVED - Product: ' . $product['sku'], null, 'demac_crons.log');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment