Skip to main content

[magento] Trigger shipment against an order

 
$order = Mage::getModel('sales/order')->load($orderId);

//create shipment
$itemQty =  $order->getItemsCollection()->count();
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$shipment = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $shipment->create( $order->getIncrementId(), array(), 'Shipment created through ShipMailInvoice', true, true); 
 
 
***** For example *****
 
 
<?php

/**
 * Class Trigger_ShippingController
 */
class Trigger_ShippingController extends Mage_Core_Controller_Front_Action
{
    /**
     * We provide this fixed uuid to a staff
     */
    const UUID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

    /**
     * @var array of parameters required
     */
    private $query = array(
        'token',
        'order',
    );

    /**
     * Get order model object by id
     *
     * @param  int $orderId
     * @return Mage_Sales_Model_Order $order
     * @throws Exception
     */
    private function getOrderById($orderId)
    {
        $order   = Mage::getModel('sales/order')->loadByIncrementId($orderId);

        if (is_null($order) || !$order->getId()) {
            throw new Exception("The order not exists by the id : {$orderId}");
        }

        if (!$order->canShip()) {
            throw new Exception("We could not ship the order : {$orderId}");
        }

        return $order;
    }

    /**
     * Process shipping
     *
     * @param  Mage_Sales_Model_Order $order
     * @return string $shipmentId
     */
    private function doShipping($order)
    {
        $itemQty    =  $order->getItemsCollection()->count();
        Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);

        $shipment   = new Mage_Sales_Model_Order_Shipment_Api();
        $shipmentId = $shipment->create($order->getIncrementId(), array(), null, true, false);

        return $shipmentId;
    }

    /**
     * Trigger shipping
     *  - required params:
     *    token: uuid v4 
     *    order: integer (order id)
     */
    public function indexAction()
    {
        $get = $this->getRequest()->getQuery();

        try {
            foreach ($this->query as $key) {
                if (!array_key_exists($key, $get)) {
                    throw new Exception("{$key} is required.");
                }
            }

            if ($this::UUID !== $get['token']) {
                throw new Exception("UUID is invalid.");
            }

            $orderId = $get['order'];

            /** @var $order Mage_Sales_Model_Order */
            $order = $this->getOrderById($orderId);

            $shipmentId = $this->doShipping($order);

            echo $shipmentId;

        } catch (Mage_Api_Exception $e) {
            echo $e->getCustomMessage();

        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}
 
 
***** If you want to add tracking info **** 
 
(1) One way
 
$shipment->addTrack($shipmentId, 'usps', 'Tracking ID', $trackinNumber);
 
(2) Another way
 
 //add tracking info
$shipment_collection = Mage::getResourceModel('sales/order_shipment_collection');
$shipment_collection->addAttributeToFilter('order_id', $orderId);
foreach($shipment_collection as $sc)
{
$shipment = Mage::getModel('sales/order_shipment');
$shipment->load($sc->getId());
                                if($shipment->getId() != '')
                                { 
                                try
                                {
                                     Mage::getModel('sales/order_shipment_track')
                                     ->setShipment($shipment)
                                     ->setData('title', 'carrier')
                                     ->setData('number', $trackInfo)
                                     ->setData('carrier_code', 'custom')
                                     ->setData('order_id', $shipment->getData('order_id'))
                                     ->save();

                                }catch (Exception $e)
                                {
                                    Mage::getSingleton('core/session')->addError('order id '.$orderId.' no found');
                                }
                                }
                        }
// change order status to complete
                        $order->addStatusToHistory(Mage_Sales_Model_Order::STATE_COMPLETE);
                        $order->setData('state', Mage_Sales_Model_Order::STATE_COMPLETE);
                        $order->save();

Comments

Popular posts from this blog

Browser User Agent List

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/601.5.17 (KHTML, like Gecko) Version/9.1 Safari/601.5.17 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0 Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0;...

[linux] Adjusting child processes for PHP-FPM (Nginx)

Adjusting child processes for PHP-FPM (Nginx) Problem: The following warning message appears in the logs: [26-Jul-2012 09:49:59] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 8 idle, and 58 total children [26-Jul-2012 09:50:00] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it It means that there are not enough PHP-FPM processes. Solution: We need to calculate and change these values based on the amount of memory on the system: /etc/php-fpm.d/www.conf pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 - the following command will help us to determine the memory used by each (PHP-FPM) child process: ps -ylC php-fpm --sort:rss The RSS column shows non-swapped physical memory usage by PHP-FPM processes in kilo Bytes. On an average each PHP-FPM process took ~75MB of RAM on my machine. Appropriate valu...

[symfony] Assert in Entity

* @Assert\NotBlank() * @Assert\Blank() * @Assert\NotNull() * @Assert\Null() * @Assert\True(message = "The token is invalid") * @Assert\False( *     message = "You've entered an invalid state." * ) * @Assert\Type(type="integer", message="The value {{ value }} is not a valid {{ type }}.") is_ array bool callable float double int integer long null numeric object real resource scalar string ctype_ alnum alpha cntrl digit graph lower print punct space upper xdigit * @Assert\Email( *     message = "The email '{{ value }}' is not a valid email.", *     checkMX = true * ) * @Assert\Length( *      min = 2, *      max = 50, *      minMessage = "Your first name must be at least {{ limit }} characters long", *      maxMessage = "Your first name cannot be longer than {{ limit }} characters long" * ...