magentoio自动创建shipment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$order = Mage::getModel('sales/order')->load($order_id);
if (!$order->getId()) {
Mage::throwException("Order does not exist, for the Shipment process to complete");
}
$qtysForProducts = array('142'=>9);
if ($order->canShip()) {
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($qtysForProducts);
// print_r($shipment);die;
// $shipment = $order->prepareShipment($qtysForProducts);
if ($shipment) {
try {
$shipment->register();
$shipment->sendEmail(true)
->setEmailSent(true)
->save();
$order->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();

echo Mage::helper('topsport')->__('create shipment ok!');
} catch (Exception $e) {
// echo Mage::helper('topsport')->__("this shipment has already exists ");
echo $e->getMessage();die;
}
}
}else{
echo Mage::helper('topsport')->__("this shipment has already exists ");
}

 

 

其中$qtysForProducts的格式为array(‘item_id’=>’qty’).
如果买的是可配置产品的子产品,里面的item_id不是子产品的item_id.而是其母产品的item_id.用$item->getParentItemId()可以获取到.
否则会报错,不能创建空shipment.

坚持原创技术分享,您的支持将鼓励我继续创作!