Kurzinfo

Andreas von Studnitz
- Diplom-Informatiker
- 6 Jahre Entwicklungs- und Projektleitungserfahrung in einer Internet-Agentur
- Magento-Freelancer
- auch TYPO3, PHP, SQL
- JavaScript, jQuery, Prototype, AJAX
Kontakt
Telefon:
0241 912 75 912 oder
0170 486 0 464
E-Mail:
avs(at)avs-webentwicklung.de
Twitter: twitter.com/avstudnitz
< Anlegen neuer Magento-Bestellungen per Quellcode
Magento: Versand zu einer Bestellung per Quellcode erzeugen
11.05.2010 00:04
Vor allem beim Zusammenspiel mit Schnittstellen kommt es immer wieder vor, dass ein Versand zu einer Bestellung per Skript ausgelöst werden soll. Hierbei hilft folgender Code:
/**
* Creates a shipment for an order
*
* @param Mage_Sales_Model_Order $order
* @return Mage_Sales_Model_Order_Shipment
*/
protected function createShipment($order) {
$convertor = Mage::getModel('sales/convert_order');
$shipment = $convertor->toShipment($order);
// count items
$savedQtys = array();
$items = $order->getAllItems();
foreach($items as $item) {
$savedQtys[$item->getId()] = $item->getQtyOrdered();
}
// add items to shipment
foreach ($order->getAllItems() as $orderItem) {
if (!$orderItem->isDummy(true) && !$orderItem->getQtyToShip()) {
continue;
}
if ($orderItem->isDummy(true) && !$this->_needToAddDummy($orderItem, $savedQtys)) {
continue;
}
if ($orderItem->getIsVirtual()) {
continue;
}
$item = $convertor->itemToShipmentItem($orderItem);
if (isset($savedQtys[$orderItem->getId()])) {
if ($savedQtys[$orderItem->getId()] > 0) {
$qty = $savedQtys[$orderItem->getId()];
} else {
continue;
}
}
else {
if ($orderItem->isDummy(true)) {
$qty = 1;
} else {
$qty = $orderItem->getQtyToShip();
}
}
$item->setQty($qty);
$shipment->addItem($item);
}
$shipment->register();
$shipment->setEmailSent(true);
// save shipment and order
$order->setCustomerNoteNotify(true);
$order->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($order)
->save();
// send email
$shipment->sendEmail(true, '');
return $shipment;
}
I am facing a problem in case of importing orders from a csv file. I hava a csv file having order information but unable to insert to magento order related tables. Hope you help me in this context.
Thanks & regards,
Alok
I do have a problem in fetching orderid of a customer (with customer ID) who purchased last time in the context of sales rule.I tried required once '/mage.php' and mage::app(); inside the class that validates sales rule. Can you please help me, I am amateur php programmer.
Thanks in Advance;
Vishnu
I fear that you won't be able to make good use of the scripts if you don't have basic magento development knowledge. In any case: you can get all orders of a customer by the following code:
$orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_id', $customerId);
You can put a foreach loop around $orders and get the order id by $order->getId() (or $order->getIncrementId() if you need the order number which is visible to the customer).
Kommentar hinzufügen