Question

We are using the default Magento CE v 1.9.1.0 USPS shipping functionality. We have our api credentials allowed to create a shipping label.

However when we create a USPS shipping label inside the usps.com website their is an option to add a reference # to the shipping label which we use to add the order number onto the label.

However now that we are using Magento to create the shipping labels we do not see the order # on the shipping label that Magento developers and would like to now how to alter the code to send the order # as a ference # to USPS when we have Magento generate the shipping label.

Any suggestions on the file to edit?

Was it helpful?

Solution

To add information to the label, you must modify the xml sent to USPS. Magento collects your shipment information, converts it to xml, then sends it to the USPS api, which returns an image file. Magento then takes that image and creates the pdf you get when you click on "Print Shipping Label".

There is a tag in the api called "CustomerRefNo" which seems right for your purposes. In case you want to try something else, the usps reference is here

In order to accomplish this, I have created a module called Yourcompany_UspsLabel. I will include all of the contents for clarity.

app/code/local/Yourcompany/UspsLabel/etc/config.xml

Basically lets Magento know about what you are rewriting.

<?xml version="1.0"?>
<config>
    <modules>
        <Yourcompany_UspsLabel>
            <version>0.1.0</version>
        </Yourcompany_UspsLabel>
    </modules>
    <global>
        <models>
            <yourcompany_uspslabel>
                <class>Yourcompany_UspsLabel_Model</class>
                <resourceModel>yourcompany_uspslabel_resource</resourceModel>
            </yourcompany_uspslabel>
            <yourcompany_uspslabel_resource>
                <class>Yourcompany_UspsLabel_Model_Resource</class>
            </yourcompany_uspslabel_resource>
            <shipping>
                <rewrite>
                    <shipping>Yourcompany_UspsLabel_Model_Shipping_Shipping</shipping>
                </rewrite>
            </shipping>
            <usa>
                <rewrite>
                    <shipping_carrier_usps>Yourcompany_UspsLabel_Model_Usa_Shipping_Carrier_Usps</shipping_carrier_usps>
                </rewrite>
            </usa>
        </models>
        <helpers>
            <yourcompany_uspslabel>
                <class>Yourcompany_UspsLabel_Helper</class>
            </yourcompany_uspslabel>
        </helpers>
    </global>
</config>

app/code/local/Yourcompany/UspsLabel/Model/Shipping/Shipment/Request.php

Note the @methods at the end - getOrderId and setOrderId to make these available when this object is passed.

/**
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setOrderShipment(Mage_Sales_Model_Order_Shipment $orderShipment)
 * @method Mage_Sales_Model_Order_Shipment getOrderShipment()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperContactPersonName(string $value)
 * @method string getShipperContactPersonName()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperContactPersonFirstName(string $value)
 * @method string getShipperContactPersonFirstName()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperContactPersonLastName(string $value)
 * @method string getShipperContactPersonLastName()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperContactCompanyName(string $value)
 * @method string getShipperContactCompanyName()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperContactPhoneNumber(int $value)
 * @method int getShipperContactPhoneNumber()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperAddressStreet(string $value)
 * @method string getShipperAddressStreet()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperAddressStreet1(string $value)
 * @method string getShipperAddressStreet1()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperAddressStreet2(string $value)
 * @method string getShipperAddressStreet2()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperAddressCity(string $value)
 * @method string getShipperAddressCity()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperAddressStateOrProvinceCode(string $value)
 * @method string getShipperAddressStateOrProvinceCode()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperAddressPostalCode(int $value)
 * @method int getShipperAddressPostalCode()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShipperAddressCountryCode(string $value)
 * @method string getShipperAddressCountryCode()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientContactPersonName(string $value)
 * @method string getRecipientContactPersonName()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientContactPersonFirstName(string $value)
 * @method string getRecipientContactPersonFirstName()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientContactPersonLastName(string $value)
 * @method string getRecipientContactPersonLastName()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientContactCompanyName(string $value)
 * @method string getRecipientContactCompanyName()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientContactPhoneNumber(int $value)
 * @method int getRecipientContactPhoneNumber()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientAddressStreet(string $value)
 * @method string getRecipientAddressStreet()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientAddressStreet1(string $value)
 * @method string getRecipientAddressStreet1()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientAddressStreet2(string $value)
 * @method string getRecipientAddressStreet2()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientAddressCity(string $value)
 * @method string getRecipientAddressCity()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientAddressStateOrProvinceCode(string $value)
 * @method string getRecipientAddressStateOrProvinceCode()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientAddressPostalCode(int $value)
 * @method int getRecipientAddressPostalCode()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setRecipientAddressCountryCode(string $value)
 * @method string getRecipientAddressCountryCode()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setShippingMethod(string $value)
 * @method string getShippingMethod()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setPackageWeight(float $value)
 * @method float getPackageWeight()
 * @method Yourcompany_UspsLabel_Model_Shipping_Shipment_Request setOrderId(string $value)
 * @method string getOrderId()
 *
 * @category    Mage
 * @package     Mage_Shipping
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Yourcompany_UspsLabel_Model_Shipping_Shipment_Request extends Mage_Shipping_Model_Shipment_Request
{
}

app/code/local/Yourcompany/UspsLabel/Model/Shipping/Shipping.php

The only change here is adding in the order's Increment ID - $request->setOrderId($order->getIncrementId());, at the end of the requestToShipment function.

<?php

class Yourcompany_UspsLabel_Model_Shipping_Shipping extends Mage_Shipping_Model_Shipping {

    public function requestToShipment(Mage_Sales_Model_Order_Shipment $orderShipment)
    {
        $admin = Mage::getSingleton('admin/session')->getUser();
        $order = $orderShipment->getOrder();
        $address = $order->getShippingAddress();
        $shippingMethod = $order->getShippingMethod(true);
        $shipmentStoreId = $orderShipment->getStoreId();
        $shipmentCarrier = $order->getShippingCarrier();
        $baseCurrencyCode = Mage::app()->getStore($shipmentStoreId)->getBaseCurrencyCode();
        if (!$shipmentCarrier) {
            Mage::throwException('Invalid carrier: ' . $shippingMethod->getCarrierCode());
        }
        $shipperRegionCode = Mage::getStoreConfig(self::XML_PATH_STORE_REGION_ID, $shipmentStoreId);
        if (is_numeric($shipperRegionCode)) {
            $shipperRegionCode = Mage::getModel('directory/region')->load($shipperRegionCode)->getCode();
        }

        $recipientRegionCode = Mage::getModel('directory/region')->load($address->getRegionId())->getCode();

        $originStreet1 = Mage::getStoreConfig(self::XML_PATH_STORE_ADDRESS1, $shipmentStoreId);
        $originStreet2 = Mage::getStoreConfig(self::XML_PATH_STORE_ADDRESS2, $shipmentStoreId);
        $storeInfo = new Varien_Object(Mage::getStoreConfig('general/store_information', $shipmentStoreId));

        if (!$admin->getFirstname() || !$admin->getLastname() || !$storeInfo->getName() || !$storeInfo->getPhone()
            || !$originStreet1 || !Mage::getStoreConfig(self::XML_PATH_STORE_CITY, $shipmentStoreId)
            || !$shipperRegionCode || !Mage::getStoreConfig(self::XML_PATH_STORE_ZIP, $shipmentStoreId)
            || !Mage::getStoreConfig(self::XML_PATH_STORE_COUNTRY_ID, $shipmentStoreId)
        ) {
            Mage::throwException(
                Mage::helper('sales')->__('Insufficient information to create shipping label(s). Please verify your Store Information and Shipping Settings.')
            );
        }

        /** @var $request Mage_Shipping_Model_Shipment_Request */
        $request = Mage::getModel('shipping/shipment_request');
        $request->setOrderShipment($orderShipment);
        $request->setShipperContactPersonName($admin->getName());
        $request->setShipperContactPersonFirstName($admin->getFirstname());
        $request->setShipperContactPersonLastName($admin->getLastname());
        $request->setShipperContactCompanyName($storeInfo->getName());
        $request->setShipperContactPhoneNumber($storeInfo->getPhone());
        $request->setShipperEmail($admin->getEmail());
        $request->setShipperAddressStreet(trim($originStreet1 . ' ' . $originStreet2));
        $request->setShipperAddressStreet1($originStreet1);
        $request->setShipperAddressStreet2($originStreet2);
        $request->setShipperAddressCity(Mage::getStoreConfig(self::XML_PATH_STORE_CITY, $shipmentStoreId));
        $request->setShipperAddressStateOrProvinceCode($shipperRegionCode);
        $request->setShipperAddressPostalCode(Mage::getStoreConfig(self::XML_PATH_STORE_ZIP, $shipmentStoreId));
        $request->setShipperAddressCountryCode(Mage::getStoreConfig(self::XML_PATH_STORE_COUNTRY_ID, $shipmentStoreId));
        $request->setRecipientContactPersonName(trim($address->getFirstname() . ' ' . $address->getLastname()));
        $request->setRecipientContactPersonFirstName($address->getFirstname());
        $request->setRecipientContactPersonLastName($address->getLastname());
        $request->setRecipientContactCompanyName($address->getCompany());
        $request->setRecipientContactPhoneNumber($address->getTelephone());
        $request->setRecipientEmail($address->getEmail());
        $request->setRecipientAddressStreet(trim($address->getStreet1() . ' ' . $address->getStreet2()));
        $request->setRecipientAddressStreet1($address->getStreet1());
        $request->setRecipientAddressStreet2($address->getStreet2());
        $request->setRecipientAddressCity($address->getCity());
        $request->setRecipientAddressStateOrProvinceCode($address->getRegionCode());
        $request->setRecipientAddressRegionCode($recipientRegionCode);
        $request->setRecipientAddressPostalCode($address->getPostcode());
        $request->setRecipientAddressCountryCode($address->getCountryId());
        $request->setShippingMethod($shippingMethod->getMethod());
        $request->setPackageWeight($order->getWeight());
        $request->setPackages($orderShipment->getPackages());
        $request->setBaseCurrencyCode($baseCurrencyCode);
        $request->setStoreId($shipmentStoreId);
        $request->setOrderId($order->getIncrementId());

        return $shipmentCarrier->requestToShipment($request);
    }
}

app/code/local/Yourcompany/UspsLabel/Model/Usa/Shipping/Carrier/Usps.php

StackExchange is stopping me from pasting the next bit, as it goes over the character limit. I created a paste here. The changes in this file are simple - just add $xml->addChild('CustomerRefNo', $request->getOrderId()); to each of the xml creation functions - _formUsExpressShipmentRequest, _formUsSignatureConfirmationShipmentRequest, and _formIntlShipmentRequest. This should be added after the $xml->addChild('ImageType', 'PDF'); calls.

app/etc/modules/Yourcompany_UspsLabel.xml Because, well, you will need one.

<?xml version="1.0"?>
<config>
    <modules>
        <Yourcompany_UspsLabel>
            <active>true</active>
            <codePool>local</codePool>
        </Yourcompany_UspsLabel>
    </modules>
</config>

You can see the order number on the label - It's on the right, under weight - Ref:145000008.

label with order number

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top