0

I'm trying to filter these collections without any change to the template, I had no problems with the up-sell (not tested), but the other two doesn't have an event.
Note: The dom structure is unknown
Up-sell
Event: catalog_product_upsell

public function checkUpsell(Varien_Event_Observer $observer){
    if(!self::isModuleEnabled()){
        return;
    }

    $upsell =$observer->getEvent()->getCollection();
    $ids =array();
    foreach($upsell as $item){
        $ids[]=$item->getId();
    }
    $collection = Mage::getModel('country/product') ->getCollection()
                                                    ->addAttributeToFilter('active', 1)
                                                    ->addAttributeToFilter('product_id', array('in' => $ids));
    if($collection->length >0){
        $country = Mage::getSingleton('core/session')->getRazorphynCountry;
        foreach($collection as $res){
            if(($res->allowed==0 && strpos($countries, $country) !== false) || ($res->allowed==1 && strpos($countries, $country) === false)){
                $upsell->removeItemByKey($res->product_id);
            }
        }
    }
 }

Configurable options
Note: Horrible, can cause eye-bleeding
Event: core_block_abstract_to_html_after

...
if( Mage::registry('current_product')->getTypeId()=='configurable'){ //to be checked
                //Retrieve all options ids
                $childIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($productId);

                //Retrieve rules to remove  for all options
                $collection = Mage::getModel('country/product')->getCollection()
                                                               ->addFieldToFilter('active', 1)
                                                               ->addFieldToFilter('product_id', $childIds)
                                                               ->limit(count($childIds));

                if($collection->length >0){
                    //Create an array with the items that will be removed
                    $removable=array();
                    foreach($collection as $res){
                        if(($res->allowed==0 && strpos($countries, $visitorCountry) !== false) || ($res->allowed==1 && strpos($countries, $visitorCountry) === false)){
                            $removable[$res->product_id]=true;
                        }
                    }

                    //Retrieve json with the product options (will be printed in the frontend
                    $json = Mage::createBlock('Mage_Catalog_Block_Product_View')->setProduct($productId)->getJsonConfig();
                    $arr=json_decode($json);
                    //I would like to print all the removed options
                    //each line is combination of removed options for the same product id
                    //label1: option - label2: option - label3:option
                    $removedOpt= array();
                    //Loop through options
                    foreach($arr['attribute'] as $element){
                        foreach($element['options'] as &$option){
                            foreach($option['products'] as &$productId){
                                if(in_array($productId,$removable)){
                                    $removedOpt[$productId][]=$element['label'].': '.$option['label'];
                                    unset($productId);
                                }
                            }
                            //Remove options if there is no associated product
                            if(count($option) == 0)
                                unset($option);
                        }
                    }

                    //Create final string with removed options
                    foreach($removedOpt as $o){
                        $o=implode(' - ',$o);
                    }
                    $removedOpt=implode("\n",$removedOpt);

                    $results  = $xpath->query('//script');

                    foreach($results as $res){
                        //Ugh...ehm...replace the already printed json with the new one...
                        preg_replace('/new\s*Product\.Config\((.*)\)/',$json,$html);
                    }
                    Mage::getSingleton('core/session')->addNotice(Mage::helper('country')->__("The following options are not available in your country:")."\n".$removedOpt);

                }
            }
...

So my question are:

  • is there a smart way to limit the configurable options?
  • is there a way to achieve the same results for cross-sells and related product?

config.xml:

...
<frontend>
        <events>

            <!-- Page Rendering event -->
            <core_block_abstract_to_html_after>
                <observers>
                    <razorphyn_country_core_block_abstract_to_html_after>
                        <type>singleton</type>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>checkMerchantable</method>
                    </razorphyn_country_core_block_abstract_to_html_after>
                </observers>
            </core_block_abstract_to_html_after>

            <!-- Remove up-sell items -->
            <catalog_product_upsell>
                <observers>
                    <razorphyn_country_catalog_product_upsell>
                        <type>singleton</type>
                        <class>Razorphyn_Country_Model_Observer</class>
                        <method>checkUpsell</method>
                    </razorphyn_country_catalog_product_upsell>
                </observers>
            </catalog_product_upsell>

        </events>
...
2
  • are you want this for frontend? and where which pages and which location
    – Amit Bera
    Commented Jun 16, 2015 at 11:29
  • Yes, all the events are associated to the frontend, in particular the category and product view page
    – Razorphyn
    Commented Jun 16, 2015 at 11:43

1 Answer 1

1

Using type Wrong event:

As per my concept,

It really bad Idea to use core_block_abstract_to_html_after for collection filter.That is too horrible .

Wrong collection filter process:

It bad to use a loop and make a collection again and that it too hurt for site speed.

Solution: Better idea to use magento joining with you custom module model table Mage::getModel('country/product')

[Edit:]

For case of upsell you can do like this:

   public function checkUpsell(Varien_Event_Observer $observer){


    $upsell =$observer->getEvent()->getCollection();


        /* $table=Mage::getSingleton('core/resource')->getTableName('country/country');*/
        /* $Filter=Mage::getSingleton('core/resource')->getConnection('core_read')->select()
            ->from(array('d'=>$table), array('d.product_id'))  ->where('d.active = ?',1 );
            */
            //echo $upsell->getSelect()->where('`e`.`entity_id` IN (?)',new Zend_Db_Expr($Filter));
            $table=Mage::getSingleton('core/resource')->getTableName('country/product');
            echo $upsell->getSelect()
                  ->join(array('countryTable' => $table),
                'countryTable.product_id =e.entity_id And  countryTable.active=1',
                array('countryTable' => 'countryTable.product_id'));        


return $upsell;

    }
}

Note: as load() function before fire run observer then the joing query should not run

4
  • I haven't tested it, but there is another question that hook that event for collection filtering, also it is present in this list:event list. I'm trying to remove the ad ot cart button but keep the product and this is the only way to manipulate that dom element without editing the template. The configurable options I have seen that are insert with js with new product.Config(jons of options), I would have printed it at the bottom of the page, but the code that eleborate it is not encapsed in a loaded dom event
    – Razorphyn
    Commented Jun 16, 2015 at 12:10
  • there are my mistake catalog_product_upsell event exist.. i have edit it
    – Amit Bera
    Commented Jun 16, 2015 at 12:13
  • I will try to join the tables, what about the second question, is it possible to achieve this result witht the other two collections?
    – Razorphyn
    Commented Jun 16, 2015 at 12:17
  • I have just seen your edit, the function wasn't complete, however thanks for the input about the join
    – Razorphyn
    Commented Jun 16, 2015 at 15:58

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.