0

I am new to magento. The Magento version that i am working on is 1.9.2.2. I created a custom shipping method using this tutorial. I have made some changes to it in order to suit my needs. The problem is i get this error on the last step of checkout page

Fatal error: Call to a member function getShp_carrier() on a non-object in ../app/code/local/Excellence/Pickup/Model/Sales/Order.php on line 12

I have googled a lot on this problem and found two links 1 & 2 relevant to my problem but that didn't help. Here is my config.xml file

<?xml version="1.0"?>
<config>
<modules>
    <Excellence_Pickup>
        <version>0.1.0</version>
    </Excellence_Pickup>
</modules>
<frontend>
    <routers>
        <pickup>
            <use>standard</use>
            <args>
                <module>Excellence_Pickup</module>
                <frontName>pickup</frontName>
            </args>
        </pickup>
    </routers>
    <layout>
        <updates>
            <pickup>
                <file>pickup.xml</file>
            </pickup>
        </updates>
    </layout>
</frontend>
<global>
    <events>
        <checkout_controller_onepage_save_shipping_method>
            <observers>
                <checkout_controller_onepage_save_shipping_method>
                    <type>model</type>
                    <class>pickup/observer</class>
                    <method>saveShippingMethod</method>
                </checkout_controller_onepage_save_shipping_method>
            </observers>
        </checkout_controller_onepage_save_shipping_method>
        <sales_model_service_quote_submit_after>
            <observers>
                <checkout_controller_onepage_save_shipping_method>
                    <type>model</type>
                    <class>pickup/observer</class>
                    <method>saveOrderAfter</method>
                </checkout_controller_onepage_save_shipping_method>
            </observers>
        </sales_model_service_quote_submit_after>
        <sales_order_load_after>
            <observers>
                <sales_order_load_after>
                    <type>model</type>
                    <class>pickup/observer</class>
                    <method>loadOrderAfter</method>
                </sales_order_load_after>
            </observers>
        </sales_order_load_after>
        <sales_quote_load_after>
            <observers>
                <sales_quote_load_after>
                    <type>model</type>
                    <class>pickup/observer</class>
                    <method>loadQuoteAfter</method>
                </sales_quote_load_after>
            </observers>
        </sales_quote_load_after>
    </events>
    <models>
        <sales>
            <rewrite>
                <order>Excellence_Pickup_Model_Sales_Order</order>
            </rewrite>
        </sales>
        <pickup>
            <class>Excellence_Pickup_Model</class>
            <resourceModel>pickup_mysql4</resourceModel>
        </pickup>
        <pickup_mysql4>
            <class>Excellence_Pickup_Model_Mysql4</class>
            <entities>
                <pickup>
                    <table>order_shipping_pickup</table>
                </pickup>
            </entities>
        </pickup_mysql4>
    </models>
    <resources>
        <pickup_setup>
            <setup>
                <module>Excellence_Pickup</module>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </pickup_setup>
        <pickup_write>
            <connection>
                <use>core_write</use>
            </connection>
        </pickup_write>
        <pickup_read>
            <connection>
                <use>core_read</use>
            </connection>
        </pickup_read>
    </resources>
    <blocks>
        <pickup>
            <class>Excellence_Pickup_Block</class>
        </pickup>
    </blocks>
    <helpers>
        <pickup>
            <class>Excellence_Pickup_Helper</class>
        </pickup>
    </helpers>
</global>
<default>
    <carriers>
        <pickup>
           <active>1</active>
            <model>Excellence_Pickup_Model_Carrier_Pickup</model>
           <title>Self Pickup</title>
           <name>Store Pickup</name>
           <price>0</price>
           <specificerrmsg>This shipping method is currently unavailable. If you would like to ship using this shipping method, please contact us.</specificerrmsg>
        </pickup>
     </carriers>
   </default>

app\code\local\Excellence\Pickup\Model\Carrier\Pickup.php file

<?php

class Excellence_Pickup_Model_Carrier_Pickup extends 
Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface {

protected $_code = 'pickup';

public function getFormBlock(){
    return 'pickup/pickup';
}

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
    if (!Mage::getStoreConfig('carriers/'.$this->_code.'/active')) {
        return false;
    }

    $handling = Mage::getStoreConfig('carriers/'.$this->_code.'/handling');
    $result = Mage::getModel('shipping/rate_result');
    $show = true;
    if($show){ // This if condition is just to demonstrate how to return success and error in shipping methods

        $method = Mage::getModel('shipping/rate_result_method');
        $method->setCarrier($this->_code);
        $method->setMethod($this->_code);
        $method->setCarrierTitle($this->getConfigData('title'));
        $method->setMethodTitle($this->getConfigData('name'));
        $method->setPrice($this->getConfigData('price'));
        $method->setCost($this->getConfigData('price'));
        $result->append($method);

    }else{
        $error = Mage::getModel('shipping/rate_result_error');
        $error->setCarrier($this->_code);
        $error->setCarrierTitle($this->getConfigData('name'));
        $error->setErrorMessage($this->getConfigData('specificerrmsg'));
        $result->append($error);
    }
    return $result;
}
public function getAllowedMethods()
{
    return array('excellence'=>$this->getConfigData('name'));
}
}

app\code\local\Excellence\Pickup\Model\Sales\Order.php file

<?php


class Excellence_Pickup_Model_Sales_Order extends Mage_Sales_Model_Order{

public function getShippingDescription(){
    $desc = parent::getShippingDescription();
   $pickupObject = $this->getPickupObject();

    if(strpos($desc, 'Customer Shipping Account') !== false){

        $desc.= " | Carrier:".$pickupObject->getShp_carrier()." Via ".$pickupObject->getShp_method()."  | Account No:".$pickupObject->getAccountno();

         }
        return $desc;

     }



}

app\code\local\Excellence\Pickup\Model\Observer.php file

<?php
class Excellence_Pickup_Model_Observer extends Varien_Object
{
public function saveShippingMethod($evt){
    $request = $evt->getRequest();
    $quote = $evt->getQuote();
    $pickup = $request->getParam('shipping_pickup',false);
    $quote_id = $quote->getId();
    $data = array($quote_id => $pickup);
    if($pickup){
        Mage::getSingleton('checkout/session')->setPickup($data);
    }
    //print_r($data);die;
}
public function saveOrderAfter($evt){
    $order = $evt->getOrder();
    $quote = $evt->getQuote();
    $quote_id = $quote->getId();
    $pickup = Mage::getSingleton('checkout/session')->getPickup();
    if(isset($pickup[$quote_id])){
        $data = $pickup[$quote_id];
        $data['order_id'] = $order->getId();
        $pickupModel = Mage::getModel('pickup/pickup');
        $pickupModel->setData($data);
        $pickupModel->save();
    }
}
public function loadOrderAfter($evt){
    $order = $evt->getOrder();
    if($order->getId()){
        $order_id = $order->getId();
        $pickupCollection = Mage::getModel('pickup/pickup')->getCollection();
        $pickupCollection->addFieldToFilter('order_id',$order_id);
        $pickup = $pickupCollection->getFirstItem();
        $order->setPickupObject($pickup);
    }
}   
public function loadQuoteAfter($evt)
{
    $quote = $evt->getQuote();
    if($quote->getId()){
        $quote_id = $quote->getId();
        $pickup = Mage::getSingleton('checkout/session')->getPickup();
        if(isset($pickup[$quote_id])){
            $data = $pickup[$quote_id];
            $quote->setPickupData($data);
        }
    }
}
}
  • What it getPickupObject function in your code? I think you are using a function which is not defined in your code. – Jaimin Sutariya Jul 19 '17 at 10:39
  • actually i have followed this tutorial and took the code as it is. May be that's why i am unable to fix this bug. – umair.ashfr Jul 19 '17 at 10:47
  • If you have created all the files then there should be no error in your code. The object is set in loadOrderAfter function, did you create this function in your code? – Jaimin Sutariya Jul 19 '17 at 11:07
  • Yes i have, I am updating the question so that you can see that file also – umair.ashfr Jul 19 '17 at 11:10
  • Did you add shp_carrier, shp_method and accountno fields in your order_shipping_pickup table? – Jaimin Sutariya Jul 19 '17 at 11:23
1

As you can see here, pickObject is the object of the collection which will get data from your table.

Please add shp_carrier, shp_method and accountno fields in your order_shipping_pickup table.

Once you add the fields after that you will be able to get the data.

You can see below code where the module is trying to get pickup data using order id,

$pickupCollection = Mage::getModel('pickup/pickup')->getCollection();
$pickupCollection->addFieldToFilter('order_id',$order_id);
$pickup = $pickupCollection->getFirstItem();
$order->setPickupObject($pickup);

Please confirm with your database if you have a record in your table with current order id.

Also, you need to check if the object is available before using it in your getShippingDescription function.

if(strpos($desc, 'Customer Shipping Account') !== false && $pickupObject){
    $desc.= " | Carrier:".$pickupObject->getShpCarrier()." Via ".$pickupObject->getShpMethod()."  | Account No:".$pickupObject->getAccountno();
 }
  • 1
    Just added this line to my code and every thing runs smoothly.. if(strpos($desc, 'Customer Shipping Account') !== false && $pickupObject) – umair.ashfr Jul 19 '17 at 11:53

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.