Created
April 22, 2013 21:22
-
-
Save Zifius/5438660 to your computer and use it in GitHub Desktop.
Adding an item to existing order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WhatEver | |
{ | |
/** | |
* Adds a product to existing order | |
* | |
* @param string $orderIncrementId Order increment id | |
* @param string $itemSku Product SKU | |
* @param float|false $price Product price | |
* | |
* @return void | |
*/ | |
public function addOrderItem($orderIncrementId, $itemSku, $price) | |
{ | |
$order = Mage::getModel('sales/order'); | |
if ($price === false) { | |
$price = 0.0; | |
} | |
/* @var $order Mage_Sales_Model_Order */ | |
$order->loadByIncrementId($orderIncrementId); | |
if (!$order->getId()) { | |
echo 'Order ' . $orderIncrementId . ' not found' . PHP_EOL; | |
return; | |
} | |
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $itemSku); | |
if (!$product->getId()) { | |
echo 'Product ' . $itemSku . ' not found' . PHP_EOL; | |
return; | |
} | |
$configurableProduct = Mage::getModel('catalog/product_type_configurable'); | |
$configurableProductId = $configurableProduct | |
->getParentIdsByChild($product->getId()); | |
if (count($configurableProductId) == 1) { | |
$configurableProductId = $configurableProductId[0]; | |
} else { | |
$configurableProductId = false; | |
} | |
if ($configurableProductId == false) { | |
echo 'Configurable product not found ' . PHP_EOL; | |
} | |
// Required to get proper store-view attribute's value labels | |
$store = Mage::app()->getStore('store_code'); | |
Mage::app()->setCurrentStore($store); | |
$invoice = $order->getInvoiceCollection()->getFirstItem(); | |
$productOptions = array( | |
'info_buyRequest' => array( | |
'product' => $configurableProductId, | |
'qty' => '1.0000', | |
'related_product' => '', | |
), | |
'attributes_info' => array(), | |
'simple_name' => $product->getName(), | |
'simple_sku' => $itemSku, | |
'product_calculations' => 1, | |
'shipment_type' => 0, | |
'giftcard_lifetime' => null, | |
'giftcard_is_redeemable' => 0, | |
'giftcard_email_template' => null, | |
'giftcard_type' => null, | |
); | |
$configurableProduct = $this->_getProductAttributes($configurableProductId, $product, $productOptions); | |
$itemData = array( | |
'order_id' => $order->getId(), | |
'store_id' => $store->getId(), | |
'product_id' => $configurableProductId, | |
'product_type' => Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE, | |
'product_options' => serialize($productOptions), | |
'weight' => 1, | |
'is_virtual' => 0, | |
'sku' => $itemSku, | |
'name' => $configurableProduct->getName(), | |
'is_qty_decimal' => 0, | |
'qty_invoiced' => 1, | |
'qty_ordered' => 1, | |
'price' => $price, | |
'base_price' => $price, | |
'original_price' => $configurableProduct->getPrice(), | |
'base_original_price' => $configurableProduct->getPrice(), | |
'row_total' => $price, | |
'base_row_total' => $price, | |
'row_invoiced' => $price, | |
'base_row_invoiced' => $price, | |
'row_weight' => 1, | |
'price_incl_tax' => $price, | |
'base_price_incl_tax' => $price, | |
'row_total_incl_tax' => $price, | |
'base_row_total_incl_tax' => $price, | |
); | |
$parentItem = Mage::getModel('sales/order_item') | |
->setData($itemData) | |
->save(); | |
$invoiceItemData = array( | |
'parent_id' => $invoice->getId(), | |
'base_price' => $price, | |
'qty' => 1, | |
'product_id' => $configurableProductId, | |
'order_item_id' => $parentItem->getId(), | |
'sku' => $itemSku, | |
'name' => $configurableProduct->getName() | |
); | |
Mage::getModel('sales/order_invoice_item') | |
->setData($invoiceItemData) | |
->save(); | |
$itemData['parent_item_id'] = $parentItem->getId(); | |
$itemData['product_type'] = Mage_Catalog_Model_Product_Type::TYPE_SIMPLE; | |
$itemData['product_id'] = $product->getId(); | |
$itemData['name'] = $product->getName(); | |
unset($itemData['price']); | |
unset($itemData['base_price']); | |
unset($itemData['original_price']); | |
unset($itemData['base_original_price']); | |
unset($itemData['row_total']); | |
unset($itemData['base_row_total']); | |
unset($itemData['row_invoiced']); | |
unset($itemData['base_row_invoiced']); | |
unset($itemData['row_weight']); | |
unset($itemData['price_incl_tax']); | |
unset($itemData['base_price_incl_tax']); | |
unset($itemData['row_total_incl_tax']); | |
unset($itemData['base_row_total_incl_tax']); | |
unset($productOptions['attributes_info']); | |
unset($productOptions['simple_name']); | |
unset($productOptions['simple_sku']); | |
unset($productOptions['product_calculations']); | |
unset($productOptions['shipment_type']); | |
$itemData['product_options'] = serialize($productOptions); | |
$childItem = Mage::getModel('sales/order_item') | |
->setData($itemData) | |
->save(); | |
$invoiceItemData = array( | |
'parent_id' => $invoice->getId(), | |
'base_price' => $price, | |
'qty' => 1, | |
'product_id' => $product->getId(), | |
'order_item_id' => $childItem->getId(), | |
'sku' => $itemSku, | |
'name' => $product->getName() | |
); | |
Mage::getModel('sales/order_invoice_item') | |
->setData($invoiceItemData) | |
->save(); | |
} | |
/** | |
* Fills product attributes values | |
* | |
* @param int $configurableProductId Configurable product id | |
* @param Mage_Catalog_Model_Product $product Simple product instance | |
* @param array &$productOptions Product options | |
* | |
* @return Mage_Catalog_Model_Product_Type_Configurable | |
*/ | |
protected function _getProductAttributes($configurableProductId, $product, &$productOptions) | |
{ | |
$configurableProduct = Mage::getModel('catalog/product')->load($configurableProductId); | |
$configurableAttributes = $configurableProduct->getTypeInstance()->getConfigurableAttributesAsArray(); | |
foreach ($configurableAttributes as $attribute) { | |
$attributeValue = $product->getData($attribute['attribute_code']); | |
$attributeValueReadable = ''; | |
$productOptions['info_buyRequest']['super_attribute'][$attribute['attribute_id']] = $attributeValue; | |
foreach ($attribute['values'] as $attributeValueData) { | |
if ($attributeValue == $attributeValueData['value_index']) { | |
$attributeValueReadable = $attributeValueData['label']; | |
break; | |
} | |
} | |
$productOptions['attributes_info'][] = array( | |
'label' => $attribute['label'], | |
'value' => $attributeValueReadable, | |
); | |
} | |
return $configurableProduct; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment