I have added a custom attribute for the product called is_master. The aim is to create a master record for the product. And sub products based on the master record (We have multiple sellers). While searching for a product i want show only the master records details... details such as description image etc... But while clicking the 'Add to cart' button i want to add specific product_id.
I have extended the class Mage_Catalog_Block_Product_List overridden the function _getProductCollection to show only records where is_master is 1. Now i want to get the product with lowest group price or selling price.
The code i was using to get the master record is
$this->_productCollection->addAttributeToFilter('is_master_record',1);
Since $this->_productCollection already have the product with lowest price i tried to save the value to a variable before applying the filter as follows.
$all_pdts=$this->_productCollection->addAttributeToFilter;
$collection =$this->_productCollection->addAttributeToFilter('is_master_record',1);
But the problem is that after applying the filter the $all_pdts and $collection have same results. I would like save the results of $this->_productCollection in a variable then do some processing later. I tried creating a new class and extending Mage_Catalog_Block_Product_List and save the result from $this->_productCollection in that class. But still when i apply the filter in the main class its also get applied to variable in second class.
Could some one help me to resolve the issue. My aim is to find the lowest group price or selling price and also show the master record.
EDIT
The code i'm currently using is :
MyModule/CustomCatalog/Block/Product/List.php
class MyModule_CustomCatalog_Block_Product_List extends Mage_Catalog_Block_Product_List{
public $matched_records = array();
protected function _getProductCollection($showMasterProduct=true){
if (is_null($this->_productCollection)) {
$layer = $this->getLayer();
/* @var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
$this->addModelTags($category);
}
}
$this->_productCollection = $layer->getProductCollection();
$this->matched_records = $layer->getProductCollection();
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
}
$allPdts = Mage::getResourceModel('mymodule_customcatalog/catalog_product_collection');
$allPdts->cloneFromCollection($this->_productCollection);
$this->matched_records = $allPdts->cloneFromCollection;
return $this->_productCollection->addAttributeToFilter('is_master_record',1);
}
Mage_Catalog_Model_Resource_Product_Collectionand implement cloneFromCollection() there, don't forget that part. – MauroNigrele Feb 01 '16 at 09:57"... on a not object"means that your model isn't loaded properly. maybe you just forgot to define model and resource model in your moduleconfig.xml– MauroNigrele Feb 01 '16 at 10:01foreach($collection as $product) { // do something }– MauroNigrele Feb 01 '16 at 17:42foreach($allPdts as product)– MauroNigrele Feb 01 '16 at 20:57