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);
}

1 Answer 1

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 :)

16
  • 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 Feb 1, 2016 at 7:27
  • Wow such a nice data, i'm pretty sure that i've made something like that so i'll look where... Feb 1, 2016 at 7:43
  • @nikhil I've just updated the answer, take a look Feb 1, 2016 at 8: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" Feb 1, 2016 at 9:54
  • Basically I'm extending Mage_Catalog_Block_Product_List and overriding the function _getProductCollection Feb 1, 2016 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.