Magento Forum

   
Page 1 of 3
Flat Rate Shipping per item as override
 
jasso
Jr. Member
 
Total Posts:  8
Joined:  2008-03-31
 

Hi all,

I need a way to override our default shipping options and make it possible for us to specify a flat rate shipping fee per item.

For most products, we want to calculate shipping for USPS and UPS by weight—that’s great. But for some things we need to be able to specify a certain shipping price (for a variety of reasons).

I’ve been looking through the forums quite a lot and can’t find a simple hack, fix, or community extension that would do this, although I see a lot of people talking about the need for this.

I’m not sure if this is something on Varien’s roadmap, but since there seems to be so much community demand, I’m wondering if anyone can advise me about how we might go about this…

Or perhaps someone has a commercial and supported module available that can do this?

Or any other ideas?

Thank you!

 
Magento Community Magento Community
Magento Community
Magento Community
 
johnh123
Member
 
Total Posts:  64
Joined:  2008-06-12
 

I need the same thing- anybody have any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
jiggliemon
Jr. Member
 
Total Posts:  18
Joined:  2008-08-27
 

This is a big issue within the magento community.

What I’m doing temporarily (As all my product have a flat rate applied) is creating a “Custom Option” within each product, and I apply the shipping rate to that item.  For instance:

// Start Example
*Shipping Option:
• Delivery Continental U.S.  +$200.00
• Warehouse Pickup [Ca] +$20.00
// End example

This adjusts the price to the shipping cost. 
However, It does not show up under “Shipping” in the total at checkout.

This is what’s working until i get a Flat Rate Shipping override module created.

-Chase

 
Magento Community Magento Community
Magento Community
Magento Community
 
Baseballtips
Sr. Member
 
Total Posts:  83
Joined:  2008-08-01
 

I’ve created a Fixed Shipping module that I’ve got working. It will show my per item shipping in the total. It’ll show the per item shipping cost in the Shopping cart table, before subtotal. And it will show up in the total at checkout and on invoice. The hitch I’ve run into is described in this thread, http://www.magentocommerce.com/boards/viewthread/29253/. If you could help me solve this we will have a fully functional, per item shipping method based on a price set in the products properties. I can send you the folder with my shipping method in it if you want to give it a try.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jiggliemon
Jr. Member
 
Total Posts:  18
Joined:  2008-08-27
 

Baseball I sent you a PM.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

I have written a Per Item Shipping Adjustment module that will provide what you need.  It’s actually a supplemental module to the shipping modules, as in it adjusts the shipping price for you.  You configure the module by telling it what price attribute to use as the source for the adjustment for each product.  You can also set a default adjustment in case it cannot find an adjustment for a specific product. 

The updated shipping cost is transparent to the customer.  They never see anything beyond the normal shipping selection box, but every shipping method will be adjusted to reflect the per item shipping costs.

This module was written for a client who decided they didn’t want to pay for it, so it’s up for grabs to the first person who wants to buy it.  After that you can do what you like with the module, give it away or sell it yourself.

For a demo and/or questions, PM me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jiggliemon
Jr. Member
 
Total Posts:  18
Joined:  2008-08-27
 

Lee’s no joke.  He’s the Johnny 5 of Magento.

I got his shipping module and it works like a charm if you set your shipping method to “Flat Rate”.
If anyone is looking for a “Per Item” shipping charge, send Lee money and you’ll save yourself some headaches.

Thanks Lee.
-Obama

you can contact me:  chase at agroism d o t com to vouch for him
OR you can contact lee for the module at:  lee.saferite at lokeycoding d o t com

 
Magento Community Magento Community
Magento Community
Magento Community
 
ModifyMage
Sr. Member
 
Avatar
Total Posts:  84
Joined:  2008-10-15
 

Okay, I have come up with a very simple hack that uses flat rate shipping as it already exists in the backend and provides customized per item shipping prices. It is very limited, but it does check for a shipping price assigned to a product, and if none exists it defaults to the normal flat rate shipping price. By overloading the flat rate model, it also allows for a handling fee to be applied to the overall order and limits to be set by country, etc. Basically, it has all the normal functionality of flat rate shipping with the added benefit of being able to define a custom shipping cost for individual products if you need to.

I am not a PhP programmer (yet), so I really want to thank Baseballtips for providing the code that allowed me to pull the shipping price from the product attribute table. I really, really needed to to be able to set a shipping price for each item in the store I’m working on. And, now that it works, I want to share it with others who might also find it useful.

Flat rate shipping is set up in the backend under “System > Configuration > Shipping Methods,” and this is where you will control what countries to apply flat rate shipping to, whether to apply a handling fee, etc.

We will be overloading this model : app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php

The reason we are overloading the core module is so you can upgrade without having your code get overwritten. If you don’t care about upgrades and prefer a simpler solution, you can just replace the function collectRates() in Flatrate.php with the code from that same function in my version of Flatrate.php in the next post once you have configured the “ship_price” attribute in xml.

First, go to “Catalog > Attributes > Manage Attributes” and add a new atribute with an Atribute Code of “ship_price”. Then go to “Catalog > Attributes > Manage Attribute Sets” and assign this new attribute to a Group. It is now ready to use, and you can go to “Catalog > Manage Products,” click on a product, select the Group from the left navigation, and enter a shipping cost for this item. Don’t forget to go to “System > Configuration > Shipping Methods” and enable Flat Rate Shipping and set the Type to “Per Item.”

Now, for the coding. First, go to “app/etc/modules” and add a new file called “MyNamespace_All.xml” (Anywhere in this post that you see “MyNamespace” - including in the code itself - sustitute your own made up namespace. For example, I use “SFA” for my namespace, so my file is named “SFA_All.xml") This file will include the following code (be sure to change “MyNamespace” to your actual namespace):

<?xml version="1.0"?>
<config>
  <
modules>
    <
MyNamespace_ItemShipping>
      <
active>true</active>
      <
codePool>local</codePool>
    </
MyNamespace_ItemShipping>
  </
modules>
  <
frontend>
    <
product>
      <
collection>
        <
attributes>
          <
ship_price/>
        </
attributes>
      </
collection>
    </
product>
  </
frontend>
</
config>

Now go to “app/code/local” and add a folder called “MyNamespace” (substitute your actual namespace). Inside this folder, add a folder called “ItemShipping”, then open that folder and add two folders - “Model” and “etc”.

Open “etc” and add a file named “config.xml” with the following code (be sure to replace “MyNamespace” with your actual namespace):

<?xml version="1.0" encoding="utf-8"?>
<config>
  <
modules>
    <
MyNamespace_ItemShipping>
      <
version>0.1.0</version>
    </
MyNamespace_ItemShipping>
  </
modules>
  <global>
    <
models>
      <
shipping>
        <
rewrite>
          <
carrier_flatrate>MyNamespace_ItemShipping_Model_Flatrate</carrier_flatrate>
        </
rewrite>
      </
shipping>
    </
models>
  </global>
</
config>

[continued in next post]

 
Magento Community Magento Community
Magento Community
Magento Community
 
ModifyMage
Sr. Member
 
Avatar
Total Posts:  84
Joined:  2008-10-15
 

Now for the actual code change. I just edited the orginal Flatrate.php file referenced above. Open the new “Model” folder and place a file in it called “Flatrate.php” which contains the following code (be sure to replace “MyNamespace” in the class name with your actual namespace):

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   Mage
 * @package    Mage_Shipping
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */


/**
 * Flat rate shipping model
 *
 * @category   Mage
 * @package    Mage_Shipping
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class MyNamespace_ItemShipping_Model_Flatrate
    
extends Mage_Shipping_Model_Carrier_Abstract
    
implements Mage_Shipping_Model_Carrier_Interface
{

    
protected $_code 'flatrate';

    
/**
     * 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->getFreeShipping() && !$item->getProduct()->getTypeInstance()->isVirtual()) {
                    $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 '0.00';
            foreach (
$request->getAllItems() as $item{
                
if (!$item->getFreeShipping()) {
                    $productId 
$item->getProduct()->getId();
                    
$productList Mage::getModel('catalog/product')->getCollection()
                        ->
addAttributeToSelect('ship_price')
                        ->
addIdFilter($productId);
                    foreach(
$productList as $product{
                        $quantity 
$item->getQty();
                        if (
$product->getShipPrice()) {
                            $shippingPrice
+=$product->getShipPrice() * $quantity;
                        
else {
                            $shippingPrice
+=$this->getConfigData('price') * $quantity;

                        
}
                    }
                }

            }
        } 
else {
            $shippingPrice 
false;
        
}

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

        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'));
    
}

}

I’m sure there are better ways to do this, and that this code can be vastly improved upon, but since so many people seem to need this and I found something that worked for me, I thought I’d post it “as is”. I hope it works for you.

I have tried to make this simple enough to follow that anyone who has not overloaded the core before can follow. If not, please let me know. Good luck, all.

EDIT: By the way, the first time I ran this, I crashed my store with some duplication errors. Go to “var/cache” and delete all of the folders in there. It should work then if everything is coded correctly.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dniesen
Member
 
Total Posts:  74
Joined:  2008-04-05
 

This code actually works quite well.  I thought this would be the answer to my woes with Google Checkout’s poor shipping calculation.  However Google Checkout only ends up using the default number.  Is there somewhere else in the Google Checkout API I need to edit to make this module quote the right price?

 
Magento Community Magento Community
Magento Community
Magento Community
 
ModifyMage
Sr. Member
 
Avatar
Total Posts:  84
Joined:  2008-10-15
 

Sorry about the extremely late reply. For some reason I didn’t receive an email when you posted. I’m afraid I can’t help you though, as I am completely unfamiliar with Magento’s Google Checkout API. If this doesn’t work, it sounds like it goes around the normal shipping models to acquire its shipping data.

 
Magento Community Magento Community
Magento Community
Magento Community
 
joey santiago
Sr. Member
 
Total Posts:  76
Joined:  2009-02-25
 

hei, great idea…

for some reason i can’t get it working without overwriting core’s Flatrate.php… does anyone have any idea?

thanks a lot! smile

I’m sorry… i re-checked everything and now works… can the problem be i didn’t use capital letters for my namespace? anyhow, thanks a lot!

 
Magento Community Magento Community
Magento Community
Magento Community
 
OwlBoy
Jr. Member
 
Total Posts:  8
Joined:  2009-04-07
 

I just implemented this on my site, and so far so good. I wanted to thank you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

I guess I could point out to all of you wanting to modify the shipping prices, I released part of my solution to the community already.  The module actually ties into the shipping rate request process adding a few new events that can be used to modify your shiping cost calculations. 

http://www.magentocommerce.com/extension/1059

Anyone can use this module as the basis for their own shipping adjustment modules.

Also, I do sell two modules that use this core code.  Per-Item and a Per-Group adjustment modules.  They are actually NOT modified shipping methods, they adjust the shipping rate request and the returned rate response.  Meaning, they work out of the box with any shipping provider.  The only issue currently is that it doesn’t work properly with Google Checkout requests, but I’m looking into that and hope to have a solution soon.

If anyone wants a demo of the code, don’t hesitate to contact me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
WebShopApps
Guru
 
Avatar
Total Posts:  460
Joined:  2008-05-30
London, UK
 

This extension does exactly what you need, documentation is attached. It has been deployed on a number of sites successfully and comes with full support and free upgrades to ensure compatibility with later Magento versions.

Contact me at enquiries@auctionmaid.com for a personalised demo - I have a range of shipping extensions that will fit almost any scenario.

Rgds

Karen

jasso - 03 November 2008 12:30 PM

Hi all,

I need a way to override our default shipping options and make it possible for us to specify a flat rate shipping fee per item.

For most products, we want to calculate shipping for USPS and UPS by weight—that’s great. But for some things we need to be able to specify a certain shipping price (for a variety of reasons).
...

File Attachments
Shipping Override Matrix.pdf  (File Size: 297KB - Downloads: 1102)
 
Magento Community Magento Community
Magento Community
Magento Community
 
Atjuh
Jr. Member
 
Total Posts:  2
Joined:  2009-02-10
Amsterdam
 

@ Spheric :

Thanks allot it is just what i needed plain and simple but Perfect for the job !!!!!

Thanks again!

Regards,

Aaron

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 3