首先谢谢你,我开始通过使用你的代码编程来实现 creditmemo,我遇到的问题是创建了 credit memom 但没有产品。我将分享我的代码,以便您使用它。
/*
I think your problem is with the content of the variable data, I'll give you an example:
array(6) {
["items"] => array(2) {
[11] => array(1) {
["qty"] => string(6) "2"
}
[13] => array(1) {
["qty"] => string(1) "0"
}
}
["do_offline"] => string(1) "1"
["comment_text"] => string(0) ""
["shipping_amount"] => string(1) "0"
["adjustment_positive"] => string(1) "0"
["adjustment_negative"] => string(1) "0"
["qtys"] => array(2) {
[11] => int(2)
[13] => int(0)
}
}
*/
/**
*
* @param array $dif array that contains my product information with qty and product_id and item_id
* @param type $info array("order_increment_id" => $order->getIncrementId(), "invoice_id" => $invoiceId);
* @return type
*/
protected function creaDevolucio($dif, $info){
$qtys = array();
foreach ($dif as $item) {
if (isset($item['qty'])) {
$qtys[$item['order_item_id']] = array("qty"=> $item['qty']);
}
if (isset($item['back_to_stock'])) {
$backToStock[$item['order_item_id']] = true;
}
}
$data = array(
"items" => $qtys,
"do_offline" => "1",
"comment_text" => "",
"shipping_amount" => "0",
"adjustment_positive" => "0",
"adjustment_negative" => "0",
);
if (!empty($data['comment_text'])) {
Mage::getSingleton('adminhtml/session')->setCommentText($data['comment_text']);
}
try {
$creditmemo = $this->_initCreditmemo($data, $info);
if ($creditmemo) {
if (($creditmemo->getGrandTotal() <=0) && (!$creditmemo->getAllowZeroGrandTotal())) {
Mage::throwException(
$this->__('Credit memo\'s total must be positive.')
);
}
$comment = '';
if (!empty($data['comment_text'])) {
$creditmemo->addComment(
$data['comment_text'],
isset($data['comment_customer_notify']),
isset($data['is_visible_on_front'])
);
if (isset($data['comment_customer_notify'])) {
$comment = $data['comment_text'];
}
}
if (isset($data['do_refund'])) {
$creditmemo->setRefundRequested(true);
}
if (isset($data['do_offline'])) {
$creditmemo->setOfflineRequested((bool)(int)$data['do_offline']);
}
$creditmemo->register();
if (!empty($data['send_email'])) {
$creditmemo->setEmailSent(true);
}
$creditmemo->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
$this->_saveCreditmemo($creditmemo);
$creditmemo->sendEmail(!empty($data['send_email']), $comment);
echo '<br>The credit memo has been created.';
Mage::getSingleton('adminhtml/session')->getCommentText(true);
return;
} else {
//$this->_forward('noRoute');
//return;
}
} catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setFormData($data);
} catch (Exception $e) {
Mage::logException($e);
$this->_getSession()->addError($this->__('Cannot save the credit memo.'));
}
}
/**
*
* @param type $data contains products info to refund
* @param type $info array("order_increment_id" => $order->getIncrementId(), "invoice_id" => $invoiceId);
* @param type $update
* @return boolean
*/
protected function _initCreditmemo($data, $info, $update = false)
{
$creditmemo = false;
$invoice=false;
$creditmemoId = null;//$this->getRequest()->getParam('creditmemo_id');
$orderId = $info['order_increment_id'];//$this->getRequest()->getParam('order_id');
$invoiceId = $data['invoice_id'];
echo "<br>abans if. OrderId: ".$orderId;
if ($creditmemoId) {
$creditmemo = Mage::getModel('sales/order_creditmemo')->load($creditmemoId);
} elseif ($orderId) {
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
if ($invoiceId) {
$invoice = Mage::getModel('sales/order_invoice')
->load($invoiceId)
->setOrder($order);
echo '<br>loaded_invoice_number: '.$invoice->getId();
}
if (!$order->canCreditmemo()) {
echo '<br>cannot create credit memo';
if(!$order->isPaymentReview())
{
echo '<br>cannot credit memo Payment is in review';
}
if(!$order->canUnhold())
{
echo '<br>cannot credit memo Order is on hold';
}
if(abs($order->getTotalPaid()-$order->getTotalRefunded())<.0001)
{
echo '<br>cannot credit memo Amount Paid is equal or less than amount refunded';
}
if($order->getActionFlag('edit') === false)
{
echo '<br>cannot credit memo Action Flag of Edit not set';
}
if ($order->hasForcedCanCreditmemo()) {
echo '<br>cannot credit memo Can Credit Memo has been forced set';
}
return false;
}
$savedData = array();
if (isset($data['items'])) {
$savedData = $data['items'];
} else {
$savedData = array();
}
$qtys = array();
$backToStock = array();
foreach ($savedData as $orderItemId =>$itemData) {
if (isset($itemData['qty'])) {
$qtys[$orderItemId] = $itemData['qty'];
}
if (isset($itemData['back_to_stock'])) {
$backToStock[$orderItemId] = true;
}
}
$data['qtys'] = $qtys;
$service = Mage::getModel('sales/service_order', $order);
if ($invoice) {
$creditmemo = $service->prepareInvoiceCreditmemo($invoice, $data);
} else {
$creditmemo = $service->prepareCreditmemo($data);
}
/**
* Process back to stock flags
*/
foreach ($creditmemo->getAllItems() as $creditmemoItem) {
$orderItem = $creditmemoItem->getOrderItem();
$parentId = $orderItem->getParentItemId();
if (isset($backToStock[$orderItem->getId()])) {
$creditmemoItem->setBackToStock(true);
} elseif ($orderItem->getParentItem() && isset($backToStock[$parentId]) && $backToStock[$parentId]) {
$creditmemoItem->setBackToStock(true);
} elseif (empty($savedData)) {
$creditmemoItem->setBackToStock(Mage::helper('cataloginventory')->isAutoReturnEnabled());
} else {
$creditmemoItem->setBackToStock(false);
}
}
}
return $creditmemo;
}
/**
* Save creditmemo and related order, invoice in one transaction
* @param Mage_Sales_Model_Order_Creditmemo $creditmemo
*/
protected function _saveCreditmemo($creditmemo)
{
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($creditmemo)
->addObject($creditmemo->getOrder());
if ($creditmemo->getInvoice()) {
$transactionSave->addObject($creditmemo->getInvoice());
}
$transactionSave->save();
return $this;
}