Snippets with the tag: sales

Create a shipment

Description

code to create a shipment for an order

Code

$order = Mage::getModel('sales/order')->loadByIncrementId('100000001');
try {
    if($order->canShip()) {
        //Create shipment
        $shipmentid = Mage::getModel('sales/order_shipment_api')
                        ->create($order->getIncrementId(), array());
        //Add tracking information
        $ship = Mage::getModel('sales/order_shipment_api')
                        ->addTrack($order->getIncrementId(), array());       
    }
}catch (Mage_Core_Exception $e) {
 print_r($e);
}

All items from an order

Description

Get all items from an order.

Code

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

Round price

Description

Round product price

Code

echo Mage::getModel('sales/order')->formatPricePrecision($_product->getFinalPrice(), 3);

Get a list of bestsellers

Description

Get a list of bestsellers

Code

$collection = Mage::getResourceModel('sales/report_bestsellers_collection')
        ->setModel('catalog/product')
        ->addStoreFilter(Mage::app()->getStore()->getId()) //if you want the bestsellers for a specific store view. if you want global values remove this
    ->setPageSize(5)//set the number of products you want
    ->setCurPage(1);
foreach ($collection as $_product){
    $realProduct = Mage::getModel('catalog/product')->load($_product->getProductId());
    //do something with $realProduct;
}

Get order information on success page

Description

Get order information on success.phtml

Code

$_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$_totalData =$order->getData();
$_grand = $_totalData['grand_total'];