我遇到了以编程方式为某些订单创建货件的问题。 对于没有发票的订单,他们会毫无问题地发送。
我在这里粘贴了我的代码,请你帮我解决这个问题。
<?php
/*
check if orderId is set
*/
if(isset($orderId)){
$Semail = false;
$includeComment = true;
$shipment = false;
/*
Update order control systems table with tracking
*/
$dispatchOrder = $sql->query("update _trackingOrders SET trackingNumber = '".$trackingNumber."' where order_number = '".$orderId."'");
$dispatchOrderCustomer = $sql->query("update _trackingCustomer SET orderStatus = 'dispatched' where order_number = '".$orderId."'");
/*
End of updates
*/
if(($dispatchOrder)&&($dispatchOrderCustomer)){
/*
Load order by id
*/
$order = Mage::getModel('sales/order')->load($orderId);
/*
check if order exist
*/
if (!$order->getId()) {
echo 'The order no longer exists.';
return false;
}
**EDIT:**
/*
check if order can be invoiced
*/
if(!$order->canInvoice()){
echo 'order can not be invoiced';
return false;
}
/*
check if order can be ship
*/
if (!$order->canShip()) {
echo 'Order can not be shipped';
return false;
}
/*
* New shipment coding begins here
*/
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment();
/*
**END OF EDIT**
*/
/*
get all items from the order
*/
foreach ($order->getAllItems() as $orderItem) {
if (!$orderItem->getQtyToShip()) {
continue;
}
if ($orderItem->getIsVirtual()) {
continue;
}
$item = $convertor->itemToShipmentItem($orderItem);
$qty = $orderItem->getQtyToShip();
$item->setQty($qty);
$shipment->addItem($item);
}
/*
shipment details
*/
$tracking = array();
$tracking['carrier_code'] = 'Royalmail';
$tracking['title'] = $shippingMethod;
$tracking['number'] = $trackingNumber;
/*
add shipment tracking details
*/
$track = Mage::getModel('sales/order_shipment_track')->addData($tracking);
$shipment->addTrack($track);
/*
register current shipment
*/
Mage::register('current_shipment_'.$orderId.'', $shipment);
/*
register shipment
add comment to shipment
send shipment email
*/
$shipment->register();
$shipment->addComment($comments, $Semail && $includeComment);
$shipment->setEmailSent(true);
/*
set order state as processign
set order is in process
set order status as complete
*/
$shipment->getOrder()->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true);
$shipment->getOrder()->setIsInProcess(false);
$shipment->getOrder()->setStatus('complete');
/*
get shipment order
add statushistory
*/
$_order = $shipment->getOrder();
$_order->addStatusHistoryComment($comments, 'complete')
->setIsVisibleOnFront(1)
->setIsCustomerNotified(1);
$_order->sendOrderUpdateEmail('1', $comments);
$_order->save();
/*
save transactions
*/
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
echo "order has been dispatched";
?>
非常感谢。
JEY