Search code examples
phpmagentomagento-1.9

Set shipping cost on the product, fix configurable products in Magento


I extend the flatrate carrier model of Magento 1.9 as suggested here because I want to set shipping cost on the single product.

When I add configurable products in the cart, shipping cost is double. Magento consider inside the cart a configurable product and simple product variant. So when I add a configurable product Magento sum shipping cost of both configurable and single products.

I add a condition to exclude configurable products. Is it ok? Is there a way to extract only single products from the cart?

Thanks.

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 ){
        // items are configurable and single products
        $cur_fproduct = Mage::getModel('catalog/product')->load($items->getProduct_id());
        if($cur_fproduct->getTypeId()=='simple') {
            $custom_ship +=($items->getQty())*($_helper->productAttribute($cur_fproduct, $cur_fproduct->getShippingWorld(), 'shipping_world'));
        }
    }

    return $custom_ship ;

Solution

  • Instead on this,

    $cart_items = $session->getQuote()->getAllItems();
    

    use,

     $cart_items = $session->getQuote()->getAllVisibleItems(); 
    

    Or you can check

    foreach( $cart_items as $items ){
            // items are configurable and single products
            if ($item->getParentItemId) // this is set for simple products of configurable
           { 
           }
        }
    

    Hope this helps!