1

I just need to find the categories of item in cart. For that I've used:

class MageCustom_ShippingFilter_Model_Shipping extends Mage_Shipping_Model_Shipping
{
    public function collectCarrierRates($carrierCode, $request)
    {
        if (!$this->_checkCarrierAvailability($carrierCode, $request)) {
            return $this;
        }
        return parent::collectCarrierRates($carrierCode, $request);
    }

    protected function _checkCarrierAvailability($carrierCode, $request = null)
    {
        Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
        $i=0;
        $productName = array();
        foreach ($cart as $item) {
            $productName[$i] = $item->getProduct()->getCategoryIds()[0];
            $i++;
        }

        $cat = array(94,97);
        $cartCat = productName;
        $result=array_diff($cat,$cartCat);
        if(count($result)==0){
            //echo count($result);
            if($carrierCode == "southeastern"){
                return true;
            }
        }

        if(count($result)>=1){
            //echo count($result); 
            if($carrierCode == "fedex"){ 
                return true;
        }
    }

}

But it's iterating as per cart item. Like if I add product in cart with same category 10 item, then it's iterating 10 times.

enter image description here

Here you can see many arrays coming. I'm getting category name, but I do not know why is coming several times.

  • Where did you add this code? Also paste the whole code. – Sukumar Gorai Jul 25 '18 at 7:55
  • <p>Did you used any new extension or observer using in your project because generally it does not happen.can you tell more?,it is happening from other code that is interrupting during getting quote data.</p> – Charul Tyagi Jul 25 '18 at 9:42
  • @SukumarGorai Code Updated – Deepak Rai Jul 25 '18 at 9:56
  • Check from where you are calling your function. Seems like your function is calling several times or you can check how many times the collectCarrierRates function gets called. – Mandar Parbate Jul 26 '18 at 6:55

Your Answer

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

Browse other questions tagged or ask your own question.