1

I am currently developing a banner manager module that allows the admin to upload banners and select which store views those banners will appear on. I have everything working fine when a single store view is selected using a multi select attribute, however when you select multiple store views Magento seems to look for the selected store ID's as a string rather than individually.

For example for a banner I have 3 store views selected. Magento returns '1,3,4' as the value. Rather than looking for each number individually Magento looks for the string'1,3,4'. See the function I am using below:

   function getHomePageBanners() {

    $storeId = Mage::app()->getStore()->getStoreId();
    $customerId = Mage::getSingleton('customer/session')->getCustomerGroupId();

    $collection = Mage::getModel("homepagebanner/homepagebanner")->getCollection()
            ->addFieldToFilter('status', array('eq' => 0))
            ->addFieldToFilter('store_view_id', array('in' => array(0,$storeId)))
            ->addFieldToFilter('customer_group_id', array('in' => array($customerId)))
            ->setOrder('position', 'ASC')
            ->toArray();

    return $collection['items'];
   }

The same applies when I am filtering via customer group as well.

Would anyone know how I can get Magento to check each value individually in the string rather than as a whole?

  • Try with ->addFieldToFilter('store_view_id', array('finset' => array(0,$storeId))) instead of ->addFieldToFilter('store_view_id', array('in' => array(0,$storeId))) but I'm not sure finset works with arrays. It should work for a single value. As a recomandation you should use a separate table to store the relation between banners and stores. It's cleaner and you can add constraints to it, so when you delete a store the relations will be automatically deleted. – Marius Sep 17 '14 at 12:00
  • Thanks for your reply Marius. With your guidance and magento.stackexchange.com/questions/14590/… I managed to get this to work. Noted regarding the additional table and will also apply this based on your advice. Thanks again! :-) – Ryan Sep 17 '14 at 13:05
  • Please post your solution as an answer and mark it as accepted. It may serve other people. – Marius Sep 17 '14 at 13:06
2

I managed to get this to work using Marius advice and using the answer from the post below:

Collection with FIND_IN_SET on multiselect attributes, but some has only one value

My code now reads

    function getHomePageBanners() {

    $storeId = Mage::app()->getStore()->getStoreId();
    $customerId = Mage::getSingleton('customer/session')->getCustomerGroupId();

    $collection = Mage::getModel("homepagebanner/homepagebanner")->getCollection()
            ->addFieldToFilter('status', array('eq' => 0))
            ->addFieldToFilter('store_view_id',
                array(
                    array('finset'=> array('0')),
                    array('finset'=> array($storeId)),
                )
            )
            ->addFieldToFilter('customer_group_id',array('finset'=>array($customerId)))
            ->setOrder('position', 'ASC')
            ->toArray();

    return $collection['items'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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