5

Currently I'm working on a new shipping method. I want this shipping method (pick-up at store) to be enabled for all categories, except for the category 'sale'. By category I mean the categories you can create in the backend of Magento, under the tab 'Products'. I did some research, but can't find a working code.

1

First of all,you need to track how a cart item is cart from Sales Category as magento system does not save the category details at checkout relate tables.

As per as magento system collectRates function of shipping method is responsible for a shipping will shown or not on Checkout.

There collectRates method you can get cart item details on by

if ($request->getAllItems()) {
                foreach ($request->getAllItems() as $item) {
            $product=$item->getProduct();
        }
    }

Solution: need to save category id at sales_flat_quote_item table during cart then on basic of this field disable shipment method.

1

You need to rewrite or copy to the local code pool the following class Mage_Shipping_Model_Shipping

To rewrite:

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

    /**
     * @param string $carrierCode
     * @return bool
     */
    protected function _isAvailable($carrierCode)
    {
        if ($carrierCode !== 'your carrier code') {
            return true;
        }

        $quoteItemList = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
        foreach ($quoteItemList as $quoteItem) {
            // Retrieve category ids
            $categoryIdList = $quoteItem->getProduct()->getCategoryIds();
            foreach ($categoryIdList as $categoryId) {
                if ($categoryId == 'your category id'){
                    return false;
                }
            }
        }
        return true;
    }
}

If you want to copy the following class to the local code pool, find the method collectCarrierRates and add the following lines before the core code.

if (!$this->_isAvailable($carrierCode)) {
    return $this;
}

Add new method _isAvailable, you can use the same method as indicated above for the rewriting case.

You can move the hardcoded category ids dependency to config and make it user friendly.

  • This gives an error: Fatal error: Class 'Mage_Shipping_Model_Shipping' not found in code/local/Mage/Shipping/Model/Shipping.php. And where do I add the new method _isAvailable? – Ellen-Rose van Dijk Jun 1 '15 at 15:37
  • I guess you want to use second solution copy to the local code pool So please copy app\code\core\Mage\Shipping\Model\Shipping.php to app\code\local\Mage\Shipping\Model\Shipping.php and make changes in this new file. Remember! Never edit Magento core files – Neklo.com Jun 2 '15 at 8:39
  • @Neklo.com Thanks man ...its working fine you solved my problem....Thanks – Deepak Rai Jul 20 '18 at 5:24
0

Please go through this link first:

In function _checkCarrierAvailability(), you need to change the logic in following manner.

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item)
{
$categoryIds = $item->getProduct()->getCategoryIds(); // returns category ids from product
  foreach($categoryIds as $categoryId)
   {
     if($categoryId == ***SALE CATEGORY ID***){
         if($carrierCode == ***pick-up at store METHOD CODE***){ #Hide Flat Rate for non logged in customers
            return false;
        }
      }
   }
}

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.