Geeks With Blogs
Biff Martin
Greetings, this is my first geeks with blogs posting. so forgive me if it looks so sloppy. 

Purpose: To calculate additional shipping fees for a shopping cart full of items based on product name in Magento 1.4.   We will assume the same shipping rate for all services here. Most items in our store are the same size and weight, except for a few items that are a little larger.  We will identify those large items by name and add some amount onto them. 

First we shall start off with a free open source Magento shipping module from webshopapps.com called MatrixRate. Thanks to the great Karen Baker for writing such excellent php code.

First you need a new SQL table added into your magento backend.  Design a new table, and fill it with fictional items and shipping costs.

--
-- Table structure for table `custom_product_matrix`
-- used in Magento ver. 1.4.0.1 with Magento Auctionmaid Shipping Module
-- Mike Martin 12/26/2011
-- * @category   Auctionmaid
-- * @package    Auctionmaid_Matrixrate
-- * @copyright  Copyright (c) 2008 Auction Maid (http://www.auctionmaid.com)
-- * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
--

DROP TABLE IF EXISTS `custom_product_matrix`;
CREATE TABLE `custom_product_matrix` (
  `pk` int(10) unsigned NOT NULL auto_increment,
  `condition_from_value` decimal(12,4) NOT NULL default '0.0000',
  `condition_to_value` decimal(12,4) NOT NULL default '0.0000',
  `price` decimal(12,4) NOT NULL default '0.0000',
  `cost` decimal(12,4) NOT NULL default '0.0000',
  `productname` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`pk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='martin custom shiping matrix';



INSERT INTO `custom_product_matrix` VALUES
(1,  1,  1,  1.00,  1.00, 'La Palma'),
(2,  2,  2,  2.00,  2.00, 'La Palma'),
(3,  3,  3,  3.00,  3.00, 'La Palma'),
(4,  4, 10,  4.00,  4.00, 'La Palma'),
(5,  1,  1,  1.00,  1.00, 'La Montage'),
(6,  2,  2,  2.00,  2.00, 'La Montage'),
(7,  3,  3,  3.00,  3.00, 'La Montage'),
(8,  4, 10,  4.00,  4.00, 'La Montage'),
(9,  1,  1,  1.00,  1.00, 'Le Riviera'),
(10, 2,  2,  2.00,  2.00, 'Le Riviera'),
(11, 3,  3,  3.00,  3.00, 'Le Riviera'),
(12, 4, 10,  4.00,  4.00, 'Le Riviera'),
(13, 1, 10,  1.00,  1.00, 'Le Menlo');

In your magento installation, cd to the webroot, which could be httpdocs or public_html.  you will find a directory called app. The full directory path to webshopapps/AuctionMaid matrix rate class files will be:  app/code/community/Auctionmaid/Matrixrate

Edit AuctionMaid Shipping Module:
"app/code/community/Auctionmaid/Matrixrate/Model/Carrier/Matrixrate.php"

Edit the collectRates class member function
Auctionmaid_Matrixrate_Model_Carrier_Matrixrate::collectRates( )

within collectRates, add a loop to summ up the additional shipping cost in the shopping cart.

Some interesting code within AuctionMaid MatrixRates, a long tortorous method of querying the database tables. We will not change this code. Just study it. Clearly, and tortured piece of php code, an example of terrible British coding.

"./Model/Mysql4/Carrier/Matrixrate.php"
Auctionmaid_Matrixrate_Model_Mysql4_Carrier_Matrixrate:getNewRate  query table


Calculate Rate for all items in cart
"./Model/Carrier/Matrixrate.php"


in magento, database functions are highly abstracted. Example of select a row in sql database

    $row = $read->fetchAll($select);
    if (!empty($row))
    {

Using these highly abstracted class methods that are inherited from Mage_Core_Model_Mysql4_Abstract

     $read = $this->_getReadAdapter();
     $write = $this->_getWriteAdapter();

....

     $row = $read->fetchAll($select);
     if (!empty($row))
     {
            // have found a result or found nothing and at end of list!
            foreach ($row as $data) {
                    $newdata[]=$data;
            }

Summary:
working dir:
./app/code/community/Auctionmaid/Matrixrate

Files:
./Model/Mysql4/Carrier/Matrixrate.php          Added new class member called getShippingPremium
./Model/Carrier/Matrixrate.php                 contains collectRates method, added a loop

Let's add some new php code to class that does all the dirty work for us...

in "./Model/Mysql4/Carrier/Matrixrate.php"
within class: Auctionmaid_Matrixrate_Model_Mysql4_Carrier_Matrixrate add a member function that returns the extra shipping cost, add it after custructor.

Add a class member function to Auctionmaid_Matrixrate_Model_Mysql4_Carrier_Matrixrate:

new member function is called:
Auctionmaid_Matrixrate_Model_Mysql4_Carrier_Matrixrate::getShippingPremium()

    //---------------------------------------------
    // MARTIN'S CUSTOM SHIP FUNCTION
    //---------------------------------------------
    public function getShippingPremium($diag, $pname, $qty)
    {
        $newdata=array();
        $tablename = "custom_product_matrix";
        $select = "select * from custom_product_matrix where productname like '$pname' and $qty >= condition_from_value and $qty <= condition_to_value order by condition_from_value ASC;";
        $price=0.00;
        $read = $this->_getReadAdapter();
        $write = $this->_getWriteAdapter();

        if ($diag > 0) {
           echo "name:".$pname." qty:".$qty."<br/>";
        }

        $row = $read->fetchAll($select);
        if (!empty($row))
        {
            // have found a result or found nothing and at end of list!
            foreach ($row as $data) {
                     $newdata[]=$data;
                     if ($diag > 0) {
                        echo "Shipping Cost: ".$data['price'];
                     }
                    $price = $data['price'];
            }
        }

        return $price;
    }


in "./Model/Carrier/Matrixrate.php"  within collectRates
line 115:

        /*----------------------------------------*/
        /* MARTIN - ADD SOME TO THE SHIPPING COST */
        /* FOR ALL ITEMS IN THE CART, LOOP, ADD   */
        /*----------------------------------------*/
        $summ=0.0;
        foreach ($request->getAllItems() as $item) {
            $sprem = Mage::getResourceModel('matrixrate_shipping/carrier_matrixrate')->getShippingPremium(0, $item->getName(),$item->getQty());
            $summ=$summ+$sprem;
        }
        //echo "Summ:".$summ."<br/>";


line 143:
       /* MARTIN - ADD THIS AMOUNT TO THE SHIPPING COST */
       $method->setPrice($shippingPrice + $summ);


In the second part of our effort we will customize The Flat Rate Shipping Methods in Magento, by inserting the new php code to the flat rate shipping module, thus making it a variable rate shipping module.

file:  ./app/code/core/Mage/Shipping/Model/Carrier/Flatrate.php
within class member function Mage_Shipping_Model_Carrier_Flatrate::collectRates

Add this block of code to collectRates, starting at line 71, after the foreach loop.

        /*----------------------------------------*/
        /* MARTIN - ADD SOME TO THE SHIPPING COST */
        /* FOR ALL ITEMS IN THE CART, LOOP, ADD   */
        /*----------------------------------------*/
        $summ=0.0;
        foreach ($request->getAllItems() as $item) {
            $sprem = Mage::getResourceModel('matrixrate_shipping/carrier_matrixrate')->getShippingPremium(0, $item->getName(),$item->getQty());
            $summ=$summ+$sprem;
        }
        /*----------------------------------------*/


at line 98, add this summ:

        $shippingPrice += $summ;

If you enjoyed that little excercise in customizing the Magento 1.4 backend, then leave a nice little comment for me to read. 

Thanks

Biff Martin
Bare Wire Networks Corp,
Redondo Beach, CA 90278

Posted on Monday, December 26, 2011 4:02 PM | Back to top


Comments on this post: Customizing the webshopapps.com AuctionMaid MatrixRate Shipping Module in Magento

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © nematoad | Powered by: GeeksWithBlogs.net