Magento Order total not updating after creating invoice programmatically - magento

I am creating invoice programmatically. As we are using using Fishbowl inventory and from that we directly generate shipment for orders in magento. So i am creating invoice automatically based on shipment.Now the invoice created successfully but order totals are not updating based on invoice amount.
Here is my code to create invoice from shipment items. I am using this function in sales_order_shipment_save_after event and using Purchase Order as payment method for the order so no capture for invoice.
public function autoInvoiceGenerate(Varien_Event_Observer $observer){
$shipment = $observer->getEvent()->getShipment();
$shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipment->getIncrementId());
$shippedItems = $shipment->getAllItems();
$order = $shipment->getOrder();
$order = Mage::getModel('sales/order')->load($order->getId());
try{
if(!$order->canInvoice()) {
//$order->addStatusHistoryComment('Order cannot be invoiced.', false);
//$order->save();
return $this;
}
$invoiceQtys = array();
foreach($shippedItems as $item){
$invoiceQtys[$item->getOrderItemId()] = $item->getQty();
}
if(empty($invoiceQtys)){
return $this;
}
//START Handle Invoice
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($invoiceQtys);
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(false);
$invoice->getOrder()->setIsInProcess(true);
$invoice->sendEmail(true, '');
$order->addStatusHistoryComment('Invoice created for shipment #'.$shipment->getData('increment_id').'.', false);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
}

A bit late but if you are still having this problem here my suggestion:
Save the order associated with the invoice.
Save the invoice at the end.
This works for me:
$order = Mage::getModel('sales/order')->load($orderId);
$invoice = Mage::getModel('sales/service_order', $order)
->prepareInvoice($itemsarray);
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(true);
$invoice->getOrder()->setIsInProcess(true);
$order->save();
Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder())
->save();
$invoice->save();

Related

Magento auto-confirm order instead of admin

I see my orders in my admin panel and all have status "pending".
Is it possible that the orders are auto-confirmed without the admin having to do this?
You can make a custom extension for it, you can get help from here.
http://www.atwix.com/magento/auto-invoice-and-custom-order-status-upon-checkout/
Or
http://inchoo.net/magento/magento-orders/automatically-invoice-ship-complete-order-in-magento/
Yes There is a way to achieve this use the below code and call in from a cron on ur time basis, The code will change the state from pending to processing after 1 hour of placing order.
<?php
require_once('app/Mage.php');
Mage::app('admin');
$orders = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status','pending')
->addFieldToFilter('created_at', array('lt' => new Zend_Db_Expr("DATE_ADD('".now()."', INTERVAL -'60:00' HOUR_MINUTE)")))
->addAttributeToSelect('customer_email')
->addAttributeToSelect('increment_id');
Mage::log("cod diret run from script cron start");
foreach ($orders as $order) {
$incrementId = $order->getIncrementId();
$individualOrder = Mage::getModel("sales/order")->loadByIncrementId($incrementId);
try {
if(!$individualOrder->canInvoice())
{
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
}
$invoice = Mage::getModel('sales/service_order', $individualOrder)->prepareInvoice();
Mage::log("next");
if (!$invoice->getTotalQty()) {
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
}
catch (Mage_Core_Exception $e) {
}
$OrderStatus = Mage::getModel("sales/order")->loadByIncrementId($incrementId);
$OrderStatus->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
$orderid= $individualOrder->getEntityId();
$email = $order->getCustomerEmail();
}

Magento Multiple Wishlist creation programmatically

I know how to add products to a customer's wishlist programmatically however it only adds to one wishlist. I have the multiple wishlist option set to enabled however I do not know how to create a new wishlist instead of merging products into the existing wishlist.
public function submitQuote(Mage_Adminhtml_Model_Session_Quote $quote)
{
$currentQuote = $quote->getQuote();
$customer = $currentQuote->getCustomer();
$items = $currentQuote->getAllVisibleItems();
//$wishlist = Mage::helper('wishlist')->getWishlist();
//Mage::register('wishlist', $wishlist);
$wishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('m/d/Y', time());
$wishlist->setCustomerId($customer->getId());
$wishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
foreach ($items as $item)
{
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($product, $buyRequest);
if(is_string($result))
{
Mage::throwException($result);
}
$wishlist->save();
}
//Mage::unregister('wishlist');
}
I've had this problem before. To add a product to a customer's wishlist you need to start a wishlist model and call the addNewItem method passing the product object. Here is how I do it.
$customer = Mage::getModel('customer/customer');
$wishlist = Mage::getModel('wishlist/wishlist');
$product = Mage::getModel('catalog/product');
$customer_id = 1;
$product_id = 1;
$customer->load($customer_id);
$wishlist->loadByCustomer($customer_id);
$wishlist->addNewItem($product->load($product_id));
Hope this helps!
EDITED:
$customerid= "YOUR CUSTOMER ID "; // Modify This
$customer=Mage::getModel("customer/customer")->load($customerid);
$wishlist=Mage::getModel("wishlist/wishlist")->loadByCustomer($customer,true);
Check for reference this: app/code/core/Mage/Wishlist/Model/Wishlist.php
LATEST EDIT:
I see that your problem is here:
$result = $wishlist->addNewItem($product, $buyRequest);
FINAL EDIT:
Then create another function and add the items and wishlist you want to add it to (as parameters). This should add items to the wishlist that you have passed as an argument when calling the function. Its the only solution I can come up with. Hope this helps!
I just stumbled across this question because I'm trying to do something similar - your code part helped me to create a new wishlist, and I've managed to do the rest so thought I'd share.
This code will;
Create a new wishlist with the name 'Quote dd/mm/YY h:m:s'
Load the wishlist collection for the current user, ordered by highest ID first
Load the newest wishlist
Add all basket items to it
Redirect the user to the new wishlist view.
public function addToQuoteAction() {
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
$customerData = Mage::getSingleton('customer/session')->getCustomer();
$customerId = $customerData->getId();
} else {
Mage::getSingleton('core/session')->addError($this->__('Please login to use this feature'));
return $this->_redirectUrl($this->_getRefererUrl());
}
// Create the wishlist
$newWishlist = Mage::getModel('wishlist/wishlist');
$curretDate = date('d/m/Y h:m:s', time());
$newWishlist->setCustomerId($customerId);
$newWishlist->setName('Quote ' . $curretDate)
->setVisibility(false)
->generateSharingCode()
->save();
// Find the newly create list and load it
$wishlistCollection = Mage::getModel('wishlist/wishlist')->getCollection()
->addFieldToFilter('customer_id',$customerId)->setOrder('wishlist_id');;
$firstWish = $wishlistCollection->getFirstItem();
$wishlist = Mage::getModel('wishlist/wishlist')->load($firstWish->getWishlistId());
$cart = Mage::getModel('checkout/cart')->getQuote();
//getAllItems
foreach ($cart->getAllVisibleItems() as $item) {
$productId = $item->getProductId();
$buyRequest = $item->getBuyRequest();
$result = $wishlist->addNewItem($productId, $buyRequest);
$wishlist->save();
}
Mage::getSingleton('core/session')->addSuccess($this->__('All items have been moved to your quote'));
return $this->_redirectUrl(Mage::getBaseUrl().'wishlist/index/index/wishlist_id/'.$firstWish->getWishlistId().'/');
}

Magento Order History Comments: Modify Date

I am creating a script to add orders programmatically in Magento. I need help to change the date of the entries in the Comments History (quote, invoice, shipping, etc.). I can manipulate the date of the order itself (setCreatedAt) and some of the comments related to the creation of the order are correct (e.g. "Sep 29, 2008 8:59:25 AM|Pending Customer Notification Not Applicable"), but I cannot change the date of the comment when I use addStatusHistoryComment...
Here's a snippet of my code:
try {
if(!$order->canInvoice()) {
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
}
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
if (!$invoice->getTotalQty()) {
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->setCreatedAt('2008-09-23 13:05:20');
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(true);
$invoice->getOrder()->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
- >addObject($invoice->getOrder());
$transactionSave->save();
//END Handle Invoice
//START Handle Shipment
$shipment = $order->prepareShipment();
$shipment->setCreatedAt('2008-09-23 14:20:10');
$shipment->register();
$order->setIsInProcess(true);
$order->addStatusHistoryComment('Shipping message goes here...', true);
$shipment->setEmailSent(true);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
$track = Mage::getModel('sales/order_shipment_track')
->setShipment($shipment)
->setData('title', 'Some tracking no.')
->setData('number', '111222333444')
->setData('carrier_code', 'fedex') //custom, fedex, ups, usps, dhl
->setData('order_id', $shipment->getData('order_id'))
->save();
//END Handle Shipment
}
catch (Mage_Core_Exception $ex) {
echo "Problem creating order invoice and/or shipment: ".$ex."\n";
}
Thanks in advance.
If I understand your question correctly, you just need to do this:
$comments = $order->getStatusHistoryCollection(true);
$comments now contains a collection of all the status history comments, and you can loop over them with whatever sort of criteria you like.
foreach ($comments as $c) {
if ( /* some stuff */ ) {
$c->setData('created_at',$new_date)->save();
}
}
So this is untested, but should work:
You need to create a new method based off addStatusHistoryComment:
/app/code/core/Mage/Sales/Model/Order.php
/*
* Add a comment to order
* Different or default status may be specified
*
* #param string $comment
* #param string $status
* #return Mage_Sales_Model_Order_Status_History
*/
public function addStatusHistoryComment($comment, $status = false)
{
if (false === $status) {
$status = $this->getStatus();
} elseif (true === $status) {
$status = $this->getConfig()->getStateDefaultStatus($this->getState());
} else {
$this->setStatus($status);
}
$history = Mage::getModel('sales/order_status_history')
->setStatus($status)
->setComment($comment)
->setEntityName($this->_historyEntityName);
->setCreatedAt('2008-09-23 14:20:10'); //I added this line
$this->addStatusHistory($history);
return $history;
}
Obviously you either need to rewrite this method, or refactor the code to do the same.

how to set order status as 'complete' in magento

how do i set order status as 'complete' manually.
I am using the following code, but its giving error saying,
The Order State 'complete' must not be set manually.
$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE);
$order->save();
i found a solution for my self,
$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
$order->setData('state', "complete");
$order->setStatus("complete");
$history = $order->addStatusHistoryComment('Order was set to Complete by our automation tool.', false);
$history->setIsCustomerNotified(false);
$order->save();
well, the actual way to make order state COMPLETE is to create invoice and shipment, after which the order state auto gets COMPLETE state. Like:
//create invoice for the order
$invoice = $order->prepareInvoice()
->setTransactionId($order->getId())
->addComment("Invoice created from cron job.")
->register()
->pay();
$transaction_save = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transaction_save->save();
//now create shipment
//after creation of shipment, the order auto gets status COMPLETE
$shipment = $order->prepareShipment();
if( $shipment ) {
$shipment->register();
$order->setIsInProcess(true);
$transaction_save = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
}
kindly change object manager to di
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderRepository = $objectManager->create('\Magento\Sales\Api\OrderRepositoryInterface');
$order = $orderRepository->get($orderId);
$order->setState('awaiting_stock');
$order->setStatus('awaiting_stock');
$orderRepository->save($order);
Set order status programmatically:
http://blog.chapagain.com.np/magento-how-to-change-order-status-programmatically/
change order status to 'Completed'
$orderIncrementId = YOUR_ORDER_INCREMENT_ID;
$order = Mage::getModel('sales/order')
->loadByIncrementId($orderIncrementId);
$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE,true)->save();

Send shipment email programmatically for the completed orders in magento

I am using magento default shipment from the admin side.
so its working fine and sending email to the customers perfectly.
I want to create one script that can send email with the shipment details for all the completed orders in magento. This will be only for certain orders coming through CSV.
My script working fine when we are using the order_id of the processing orders but its not working for the completed orders . and not sending the Order items details in the email
please suggest me any solution for it..
Thanks a lot.
I am using the below script to do so :
function completeShipment($orderIncrementId,$shipmentTrackingNumber,$shipmentCarrierCode){
$shipmentCarrierTitle = $shipmentCarrierCode;
$customerEmailComments = '';
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
if (!$order->getId()) {
Mage::throwException("Order does not exist, for the Shipment process to complete");
}
try {
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment(_getItemQtys($order));
$arrTracking = array(
'carrier_code' => isset($shipmentCarrierCode) ? $shipmentCarrierCode : $order->getShippingCarrier()->getCarrierCode(),
'title' => isset($shipmentCarrierTitle) ? $shipmentCarrierTitle : $order->getShippingCarrier()->getConfigData('title'),
'number' => $shipmentTrackingNumber,
);
$track = Mage::getModel('sales/order_shipment_track')->addData($arrTracking);
$shipment->addTrack($track);
$shipment->register();
_saveShipment($shipment, $order, $customerEmailComments);
}catch (Exception $e) {
throw $e;
}
return $save;
}
function _getItemQtys(Mage_Sales_Model_Order $order){
$qty = array();
foreach ($order->getAllItems() as $_eachItem) {
if ($_eachItem->getParentItemId()) {
$qty[$_eachItem->getParentItemId()] = $_eachItem->getQtyOrdered();
} else {
$qty[$_eachItem->getId()] = $_eachItem->getQtyOrdered();
}
}
return $qty;
}
function _saveShipment(Mage_Sales_Model_Order_Shipment $shipment, Mage_Sales_Model_Order $order, $customerEmailComments = ''){
$shipment->getOrder()->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)->addObject($order)->save();
//$emailSentStatus = $shipment->getData('email_sent');
$ship_data = $shipment->getOrder()->getData();
$customerEmail = $ship_data['customer_email'];
$emailSentStatus = $ship_data['email_sent'];
if (!is_null($customerEmail)) {
$shipment->sendEmail(true, $customerEmailComments);
$shipment->setEmailSent(true);
}
return $this;
}
Thanks a lot dagfr...! the below code works for me to get done my task:
function completeShipment($orderIncrementId,$shipmentIncreamentId){
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$ship_data = $order->getData();
$customerEmail = $ship_data['customer_email'];
$shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentIncreamentId);
$customerEmailComments = '';
$sent = 0;
if (!is_null($customerEmail)) {
$sent = $shipment->sendEmail(true, $customerEmailComments);
$shipment->setEmailSent(true);
}
return $sent;
}
Your script tries to create the shipment then send the email.
For completed orders the shipment is already created and you can't create it again, so your try fails before the email sending.
Just before :
try {
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment(_getItemQtys($order));
Check the status of the order, if it's complete, just need to get the shipment and send the mail
$shipment->sendEmail(true, $customerEmailComments);
$shipment->setEmailSent(true);
Else you can perform your script.
I found success using the order_shipment_api instead of the service_order model.
If you want to include the tracking number in the shipment email, make sure you add the tracking first and then use Mage::getModel('sales/order_shipment_api')->sendInfo($shipmentIncrementId).
Code Snippet:
$shipmentApi = Mage::getModel('sales/order_shipment_api');
//pass false for email, unless you want Magento to send the shipment email without any tracking info
//could also be written as $shipmentIncrementId = $shipmentApi->create($order->getIncrementId());
$shipmentIncrementId = $shipmentApi->create($order->getIncrementId(), array(), '' , false, 0);
//add tracking info ($shippingCarrier is case sensitive)
$shipmentApi->addTrack($shipmentIncrementId, $shippingCarrier, $shippingTitle, $trackingNumber);
//send shipment email with tracking info
$shipmentApi->sendInfo($shipmentIncrementId);
See app\code\core\Mage\Sales\Model\Order\Shipment\Api.php for all methods.
You can(should) also perform checks first:
//check for existing shipments if you want
if ($order->getShipmentsCollection()->count() == 0){
}
// and/or
if($order->canShip()){
}

Resources