I suggest,
Create new two date attribute product attribute.W
available_from_date
->the start date from where the product will be show on product listing
- available_to_date ->the end date from where the product w
will be stop on product listing
This two attribute manage the product wise available time .Just like h product new from date and product new to date.
Now you need to set product those two field value from admin
Then using below events filter the product collection by :
catalog_block_product_list_collection
(used by review module, after
collection is loaded its not editable anymore)
catalog_product_collection_load_before
catalog_product_collection_load_after
See catalog_product_collection_load_before and getLoadedProductCollection
Then make event area fronted
thus event will fire on frontend area
Example using event:
<frontend>
<events>
<catalog_product_collection_load_after> <!- event -->
<observers>
<pset_product_avaliable>
<class>YourModuleNameSpace_ModuleName_Model_Observer</class>
<method>collectiondatefilter</method>
</pset_product_avaliable>>
</observers>
</catalog_product_collection_load_after>
</events>
On event filter the collection by below code:
$todayStartOfDayDate = Mage::app()->getLocale()->date()
->setTime('00:00:00')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$todayEndOfDayDate = Mage::app()->getLocale()->date()
->setTime('23:59:59')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection->addAttributeToFilter('available_from_date', array('or'=> array(
0 => array('date' => true, 'to' => $todayEndOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter('available_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayStartOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter(
array(
array('attribute' => 'available_from_date', 'is'=>new Zend_Db_Expr('not null')),
array('attribute' => 'available_to_date, 'is'=>new Zend_Db_Expr('not null'))
)
);
Observer look like:
public function collectiondatefilter($observer)
{
$collection = $observer->getEvent()->getCollection();
$todayStartOfDayDate = Mage::app()->getLocale()->date()
->setTime('00:00:00')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$todayEndOfDayDate = Mage::app()->getLocale()->date()
->setTime('23:59:59')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection->addAttributeToFilter('available_from_date', array('or'=> array(
0 => array('date' => true, 'to' => $todayEndOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter('available_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayStartOfDayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter(
array(
array('attribute' => 'available_from_date', 'is'=>new Zend_Db_Expr('not null')),
array('attribute' => 'available_to_date, 'is'=>new Zend_Db_Expr('not null'))
)
);
return $this;
}
When a product is purchased, an Order Item is created. The way to go imho is to get the Order-Item-Collection, filter by product_id
and sort by created_at
. This should work (untested, though):
// only if product is not yet available
$product = Mage::registry('current_product');
// get newest order item of this product
$collection = Mage::getModel('sales/order_item')->getCollection()->addFieldToFilter('product_id', $product->getId())->addFieldToSort('created_at', 'DESC')->addPageSize(1)->addCurPage(1);
if($collection->count()) {
$item = $collection->getFirstItem();
echo $item->getCreatedAt();
}
You should not use this kind of code in a template file, though. Make an own extension with an own Block-class and include this block in your layout.
Best Answer
Here's a method I've used for something quite similar (in this case, using the "Special Price" date range for the product).
I've extended from
Mage_Catalog_Block_Product_List