0

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);
}
Schrödinger's Cat
  • 378
  • 1
  • 6
  • 18

1 Answers1

0

EDIT

Based on comment data, you actually can't clone directly a product collection and since $_select is a protected property my suggested approach will be:

  • Create a New Collection Model
  • Add a cloneFromCollection() method

{Vendor}/{Module}/Model/Resource/Catalog/Product/Collection.php

class {Vendor}/{Module}/Model/Resource/Catalog/Product/Collection.php
    extends Mage_Catalog_Model_Resource_Product_Collection 
{
    public function cloneFromCollection(Mage_Catalog_Model_Resource_Product_Collection $collection) {
       $this->select = clone $collection->getSelect();
       $this->_reset();
       $this->_totalRecords = null;
       return $this;            
    }
}

Then in your code create a new collection:

$allPdts = Mage::getResourceModel('vendor_module/catalog_product_collection');
$allPdts->cloneFromCollection($this->_productCollection);

This should be enough.

NOTE: This is just a theorical approach, because I can't try it right now, so just let me know if it's useful for you.


If i understand well you are trying to save in $all_pdts the original collection and then apply the filters to the new one.

So (if I'm right) the problem is that when you do:

$all_pdts=$this->_productCollection->` 

you are referencing the collection object to $all_pdts ( the object still be the same) so regardless the specific filter you need to perform you must clone the collection first.

$all_pdts = clone $this->_productCollection;

Since here $all_pdts and $this->_productColletion are different objects and you can do whatever you need with them.

Sorry if I misunderstood the question.

BTW I hate to sound like a "Standards Officer" but the standard using in magento 1 says that the variables name should be camelCase so $all_pdtsshould be $allPdts, sorry I just can't stop my internal COP :)

MauroNigrele
  • 3,005
  • 1
  • 18
  • 28
  • Thank you for reply.. Yes i'm trying to do the same. Actually i tried clone. But its still returning the reference instead of value.. when i searched about this i found that "Magento does not implement __clone on it's collection abstracts, and therefore does not support deep cloning as you want it to." Please see the link. Sorry i should have mentioned that earlier. Thanks again for the suggestion on the naming conventions – Schrödinger's Cat Feb 01 '16 at 07:27
  • Wow such a nice data, i'm pretty sure that i've made something like that so i'll look where... – MauroNigrele Feb 01 '16 at 07:43
  • @nikhil I've just updated the answer, take a look – MauroNigrele Feb 01 '16 at 08:03
  • I tried that... but i must be doing something wrong. I'm getting an error message "Fatal error: Call to a member function cloneFromCollection() on a non-object in /opt/lampp/htdocs/mymodule/marketplace/magento/app/code/local/Buildnext/CustomCatalog/Block/Product/List.php on line 52" – Schrödinger's Cat Feb 01 '16 at 09:54
  • Basically I'm extending Mage_Catalog_Block_Product_List and overriding the function _getProductCollection – Schrödinger's Cat Feb 01 '16 at 09:56
  • Yes but in my workaround you must extend Mage_Catalog_Model_Resource_Product_Collection and implement cloneFromCollection() there, don't forget that part. – MauroNigrele Feb 01 '16 at 09:57
  • Sorry if the error is "... on a not object" means that your model isn't loaded properly. maybe you just forgot to define model and resource model in your module config.xml – MauroNigrele Feb 01 '16 at 10:01
  • I missed to add the resource in the config file. I have added that and know the error seems to resolved. However since i'm new to magento i'm not sure how to get data from the returned value. I have stored the value returned from $allPdts->cloneFromCollection in $values. When i do $values->getSize() its returning 138. But not sure how to loop through the result. – Schrödinger's Cat Feb 01 '16 at 17:38
  • foreach($collection as $product) { // do something } – MauroNigrele Feb 01 '16 at 17:42
  • $collections = $allPdts->cloneFromCollection($this->_productCollection);
        foreach($collections as $each){
         echo $each->getMsrp();
        }  But its not printing anything :(
    
    – Schrödinger's Cat Feb 01 '16 at 18:26
  • No boy just: foreach($allPdts as product) – MauroNigrele Feb 01 '16 at 20:57
  • @nikhil hey some progress with that? – MauroNigrele Feb 03 '16 at 04:08
  • I tried the code foreach($allPdts as $product){echo $product->getPrice();} But its still not showing any result. I'm still working on that. – Schrödinger's Cat Feb 03 '16 at 05:40
  • Actually we are building a multi seller application. So we have created a master product. So all the products added by different sellers will have a unique master record. So while searching we only want to show the master record in the search result. Then we need to find the product with least price and show that price along with the master record. that what i'm trying to accomplish. – Schrödinger's Cat Feb 03 '16 at 05:52
  • I guess that my answer was more around the collection clone process than your actual needs. – MauroNigrele Feb 03 '16 at 06:24
  • Thats ok, our plan was to find the correct product from $this->_productColletion before applying filter 'is_master_record' thats why we wanted to create a copy. – Schrödinger's Cat Feb 03 '16 at 06:55