Pregunta

Estamos utilizando la funcionalidad de envío predeterminada de Magento CE v 1.9.1.0 USPS.Tenemos nuestras credenciales API permitidas para crear una etiqueta de envío.

Sin embargo, cuando creamos una etiqueta de envío de USPS dentro del sitio web usps.com, existe una opción para agregar un número de referencia a la etiqueta de envío que usamos para agregar el número de pedido a la etiqueta.

Sin embargo, ahora que estamos usando Magento para crear las etiquetas de envío, no vemos el número de pedido en la etiqueta de envío que desarrolló Magento y ahora nos gustaría saber cómo modificar el código para enviar el número de pedido como número de referencia a USPS cuando tengamos Magento genera la etiqueta de envío.

¿Alguna sugerencia sobre el archivo para editar?

¿Fue útil?

Solución

Para agregar información a la etiqueta, debe modificar el xml enviado a USPS.Magento recopila la información de su envío, la convierte a xml y luego la envía a la API de USPS, que devuelve un archivo de imagen.Luego, Magento toma esa imagen y crea el pdf que obtienes cuando haces clic en "Imprimir etiqueta de envío".

Hay una etiqueta en la API llamada "CustomerRefNo" que parece adecuada para sus propósitos.En caso de que quieras probar algo más, la referencia de usps es aquí

Para lograr esto, he creado un módulo llamado Yourcompany_UspsLabel.Incluiré todos los contenidos para mayor claridad.

aplicación/código/local/Suempresa/UspsLabel/etc/config.xml

Básicamente, le permite a Magento saber lo que estás reescribiendo.

<?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>

aplicación/código/local/Suempresa/UspsLabel/Model/Shipping/Shipment/Request.php

Tenga en cuenta los @methods al final: getOrderId y setOrderId para que estén disponibles cuando se pase este objeto.

/**
 * @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
{
}

aplicación/código/local/Suempresa/UspsLabel/Model/Shipping/Shipping.php

El único cambio aquí es agregar el ID de incremento del pedido. $request->setOrderId($order->getIncrementId());, al final de requestToShipment función.

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

aplicación/código/local/Suempresa/UspsLabel/Model/Usa/Shipping/Carrier/Usps.php

StackExchange me impide pegar el siguiente bit porque supera el límite de caracteres.Creé una pasta aquí.Los cambios en este archivo son simples: simplemente agregue $xml->addChild('CustomerRefNo', $request->getOrderId()); a cada una de las funciones de creación xml - _formUsExpressShipmentRequest, _formUsSignatureConfirmationShipmentRequest, y _formIntlShipmentRequest.Esto debería agregarse después el $xml->addChild('ImageType', 'PDF'); llamadas.

aplicación/etc/modules/Suempresa_UspsLabel.xmlPorque, bueno, necesitarás uno.

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

Puedes ver el número de pedido en la etiqueta - Está a la derecha, debajo del peso - Ref:145000008.

label with order number

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top