5

I tried to add different shipping costs for different products using http://blog.aajit.com/product-level-shipping-in-magento/ but i am getting 0 rs as flat rate in checkout. I should get different flat rate for different products how can do it?

Flatrate.php

class Mage_Shipping_Model_Carrier_Flatrate
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{

protected $_code = 'flatrate';
protected $_isFixed = true;

/**
 * Enter description here...
 *
 * @param Mage_Shipping_Model_Rate_Request $data
 * @return Mage_Shipping_Model_Rate_Result
 */
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    $freeBoxes = 0;
    if ($request->getAllItems()) {
        foreach ($request->getAllItems() as $item) {

            if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
                continue;
            }

            if ($item->getHasChildren() && $item->isShipSeparately()) {
                foreach ($item->getChildren() as $child) {
                    if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
                        $freeBoxes += $item->getQty() * $child->getQty();
                    }
                }
            } elseif ($item->getFreeShipping()) {
                $freeBoxes += $item->getQty();
            }
        }
    }
    $this->setFreeBoxes($freeBoxes);

    $result = Mage::getModel('shipping/rate_result');
    if ($this->getConfigData('type') == 'O') { // per order
        $shippingPrice = $this->getConfigData('price');
    } elseif ($this->getConfigData('type') == 'I') { // per item
        $shippingPrice = ($request->getPackageQty() * $this->getConfigData('price')) - ($this->getFreeBoxes() * $this->getConfigData('price'));
    } else {
        $shippingPrice = false;
    }

    $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);
    $shippingPrice = $this->get_pro_ship();

    if ($shippingPrice !== false) {
        $method = Mage::getModel('shipping/rate_result_method');

        $method->setCarrier('flatrate');
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod('flatrate');
        $method->setMethodTitle($this->getConfigData('name'));

        if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
            $shippingPrice = '0.00';
        }


        $method->setPrice($shippingPrice);
        $method->setCost($shippingPrice);

        $result->append($method);
    }

    return $result;
}

public function getAllowedMethods()
{
    return array('flatrate'=>$this->getConfigData('name'));
}

public function get_pro_ship()
{
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');
$cart_items = $session->getQuote()->getAllItems();
$_helper = Mage::helper('catalog/output');
$custom_ship=0;
foreach( $cart_items as $items ){

         $cur_fproduct = Mage::getModel('catalog/product')->load($items->getProductId());
    //$cur_fproduct = Mage::getModel('catalog/product')->load($items->getProduct_id());
$custom_ship +=($items->getQty())*($_helper->productAttribute($cur_fproduct, $cur_fproduct->getShippingWorld(), 'shipping_world'));                        
}


return $custom_ship ;   

}// function end

}

  • 1
    try this Extension magentocommerce.com/magento-connect/… – Magento 2 Oct 26 '15 at 7:15
  • i want to try it without extension – Swetha Oct 26 '15 at 7:17
  • Use and extension for it! Every other way does not really make sense. Everything else would be an overwriting within the local files. That makes sense for small changes but not for a whole set of functions (what your requrement is). – Bernhard Prange Oct 26 '15 at 9:25
2

You need to create observer for it.
Step 1 :- put this code in your app/code/local/Shipping/Disable/etc/config.xml file.

<config>
  <frontend>
    <events>
      <sales_quote_collect_totals_before>
          <observers>
              <shipping_disable>
                  <type>singleton</type>
                  <class>disable/observer</class>
                  <method>salesQuoteCollectTotalsBefore</method>
              </shipping_disable>
          </observers>
      </sales_quote_collect_totals_before>
    </events>
  </frontend>
 </config>

Step-2 : Put this code in app/code/local/Shipping/Disable/Model/Observer.php

<?php
class Shipping_Disable_Model_Observer
{
 public function salesQuoteCollectTotalsBefore(Varien_Event_Observer $observer)
    {
        /** @var Mage_Sales_Model_Quote $quote 778.78*/
            $quote = $observer->getQuote();
            $subTotal = $quote->getSubtotal();
            $newSubTotal = 55;  //update your price here 
            $store    = Mage::app()->getStore($quote->getStoreId());
            $productAttr = array();
            foreach($quote->getAllItems() as $quote_item) {
                $productAttr[] = $quote_item->getYourProductCustomAttr();
        }
        if(is_array($productAttr) && count($productAttr > 0){
           $carriers = Mage::getStoreConfig('carriers',$store);
           foreach ($carriers as $carrierCode => $carrierConfig) {
            if($carrierCode == 'fedex'){                      
                    $store->setConfig("carriers/freightquote/handling_fee", $newSubTotal);

            }

            }

        }
    }
}
?>
  • Note that this updates a configuration value. As such, it won't be visible unless you clear configuration cache. That is not something you want to do in the frontend, even though I know several extensions who do just that to update their license information - cache management is a backend's responsibility. – Melvyn Oct 26 '15 at 9:31

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.