Magento : Reorder item with custom price, price always 0 - magento

I have some product that has custom prices. Depending on the option selected, there is a formula applied that add fees to the product, so the price is never the same. The problem I have is that when you reorder, the price of the reordered product is always 0.
In sales/controllers/OrderController , in the function reorder,there is this :
$order = Mage::registry('current_order');
$items = $order->getItemsCollection();
foreach ($items as $item) {
try {
$cart->addOrderItem($item);
...
If I add these lines , I’m able to retrieve the custom price, but I can’t find a way to edit the item so that is the price being added in the reorder.
$options = $item->getProductOptions();
$options = $options['info_buyRequest'];
$customPrice = $options['custom_price'];
There is what I have tried (in the loop, before $cart->addOrderItem($item) ), without success.
$item->setSpecialPrice($customPrice);
$item->setCustomPrice($customPrice);
$item->setOriginalPrice($customPrice);
$item->setBaseOriginalPrice($customPrice);
$item->setBaseCost($customPrice);
$item->setBaseRowInvoiced($customPrice);
$item->setRowInvoiced($customPrice);
$item->save();
Any help?

Several possibilities. I'd try an event observer for the checkout_cart_product_add_after event.
// observer method:
public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
$action = Mage::app()->getFrontController()->getAction();
if ($action->getFullActionName() == 'sales_order_reorder')
{
$buyInfo = $observer->getQuoteItem()->getBuyRequest();
if ($customPrice = $buyInfo->getCustomPrice())
{
$observer->getQuoteItem()->setCustomPrice($customPrice)
->setOriginalCustomPrice($customPrice);
}
}
}

Related

How to get correct quantity for child product in cart

I get cart items information using following code:
$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach( $cart_items as $items )
{
$items->getQty();
}
In above code $items->getQty() always return "float(1)" while more than 1 quantity add in cart for child product.
How to get correct quantity for child product?
Thanks in advance.
Finally I found my solution:
$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach( $cart_items as $items )
{
STATIC $qty='';
if($items->getProductType() == 'configurable') //configurable products
{
$qty = $items->getQty();
continue;
}
else // non-configurable product
{
if (!$items->getParentItem()) // product which has not parent product
{
$qty = $items->getQty();
}
}
echo $qty;
}
For simple products your code should work.
Try using a model call to see if it makes a difference. Also check sales_flat_quote table and see what items/quantity is found under the sales_flat_quote_item table and if they mismatch with the frontend display.
$oQuote = Mage::getModel( 'checkout/cart' )->getQuote();
// For all items.
$iTotalItemQty = $oQuote->getItemsQty();
echo $iTotalItemQty;
Also are you seeing this show up from a simple product with quantity > 1 or a different product type?
Did you tried getAllVisibleItems() instead of getAllItems()?
$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach( $cart_items as $items )
{
$items->getQty();
}

How to get a particular item from the cart using the item id in cart?

I want to get a particular item from the cart using id of item in cart (not product id).
http://localhost/magento81/index.php/checkout/cart/configure/id/1/
I want to load using this id, id/1
How to get that ?
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
if($item->getId() == 1)
{
$productId = $item->getProduct()->getId();
$productPrice = $item->getProduct()->getPrice();
}
}
In general if you use print_r($item->getProduct()->getData()) it will disaplay all the information related to product in the cart.
$this should do the trick.
$itemId = 1;
$item = Mage::getModel('sales/quote_item')->load($itemId);
If you want to get the product object associated to the item continue with this
$product = $item->getProduct();

MAGENTO - Load last created Product to Cart

is it possible to load the last registered (created) Product in my Cart?
How?
I know it sounds crazy but i need this for one of my project.
I think this is the part where the Product gets loaded:
cartcontroller.php
/**
* Initialize product instance from request data
*
* #return Mage_Catalog_Model_Product || false
*/
protected function _initProduct()
{
$productId = (int) $this->getRequest()->getParam('product');
if ($productId) {
$product = Mage::getSingelton('checkout/session')->getQuote()->getAllItems()
->setStoreId(Mage::app()->getStore()->getId())
->load($productId);
if ($product->getId()) {
return $product;
}
}
return false;
}
This (if I´m right) i need to be replaced with the last in shop created Product - sound weired but i need this....
You're almost there - try this code:
$collection = Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection();
$collection->getSelect()->order('created_at DESC');
$latestItem = $collection->getLastItem();
Note that when you get the latest quote item, you're not actually obtaining the product. To get the actual product, you would need to add this line:
$product = $latestItem->getProduct();
You can get the items in the cart like this:
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
Then loop through the items and see which one has the biggest id.
$max = 0;
$lastItem = null;
foreach ($items as $item){
if ($item->getId() > $max) {
$max = $item->getId();
$lastItem = $item;
}
}
if ($lastItem){
//do something with $lastItem
}

Get Simple Product from Configurable in Cart

I'm trying to load the simple products that have been added to a customer's cart, but when I retrieve the items, it's showing the parent configurable.
$cart = Mage::getSingleton('checkout/cart');
$productIds = array();
foreach ($cart->getQuote()->getAllVisibleItems() as $item) {
$productIds[] = $item->getProduct()->getId();
}
var_dump($productIds);
For instance, this will return all the same configurable id when I've added a small, medium, and large to my cart. How can I get the individual simple products? I'm trying to retrieve an attribute value that's set on the simple product level.
After taking a look at how Magento renders the items in your cart on the checkout/cart page, I was able to find this in app/code/core/Mage/Checkout/Block/Cart/Item/Renderer/Configurable.php
/**
* Get item configurable child product
*
* #return Mage_Catalog_Model_Product
*/
public function getChildProduct()
{
if ($option = $this->getItem()->getOptionByCode('simple_product')) {
return $option->getProduct();
}
return $this->getProduct();
}
So, applying it to the snippet in the question, it would be
foreach ($cart->getQuote()->getAllVisibleItems() as $item) {
$productId = $item->getProduct()->getId();
if ($option = $item->getOptionByCode('simple_product')) {
$productId = $option->getProduct()->getId();
}
$productIds[] = $productId;
}

Magento: Increase "Qty" upon cancel a shipped order

I'm on Magento 1.7.0.2. I'm using "Cash On Delivery" as my payment method.I'll tell you exactly the steps That i follow on any order. When an order is placed(Qty decreased 1 item), I create a shipment for it and if customer paid the order grand total price. I create an invoice for that order.
My problem, If an order is placed(Qty decreased 1 item), I create a shipment for this order. If the customer refused to pay, I open this order and "Cancel" it and on this case the "Qty" doesn't increase so How can I make it increase?
If order status is Processing
Create a custom module with observer for 'order_cancel_before' (see example # Change Magento default status for duplicated products change <catalog_model_product_duplicate> to <order_cancel_before>
since <order_cancel_before> is not defined in app/code/core/Mage/Sales/Model/Order.php
You could override/rewrite order model class see e.g http://phprelated.myworks.ro/how-to-override-rewrite-model-class-in-magento/
In your local module do
public function cancel()
{
if ($this->canCancel()) {
Mage::dispatchEvent('order_cancel_before', array('order' => $this));
$this->getPayment()->cancel();
$this->registerCancellation();
Mage::dispatchEvent('order_cancel_after', array('order' => $this));
}
return $this;
}
Or you could create a new method increaseProductQty() in your model and copy the code below into it (this way you would not need an observer). Then replace the line Mage::dispatchEvent('order_cancel_before'... with $this->increaseProductQty()
In your observer method (pseudo code)
$curr_date = date('Y-m-d H:i:s');
$order = $observer->getEvent()->getOrder();
foreach ($order->getItemsCollection() as $item)
{
$productId = $item->getProductId();
$qty = $item->getQty();
// you need to check order status to make sure it processing
//$order->getStatus() (assuming you are canceling entire order)
//$order->getPayment();
$product = Mage::getModel('catalog/product')->load($product_id);
$stock_obj = Mage::getModel('cataloginventory/stock_item')->load($product_id);
$stockData = $stock_obj->getData();
$product_qty_before = (int)$stock_obj->getQty();
$product_qty_after = (int)($product_qty_before + $qty);
$stockData['qty'] = $product_qty_after;
$productInfoData = $product->getData();
$productInfoData['updated_at'] = $curr_date;
$product->setData($productInfoData);
$product->setStockData($stockData);
$product->save();
}
If you have issue with updating stock see Set default product values when adding new product in Magento 1.7
Reference http://pragneshkaria.com/programatically-change-products-quantity-after-order-cancelled-magento/
If order status is Pending
Take a look at System > Configuration > Inventory
Set Items’ Status to be In Stock When Order is Cancelled — Controls whether products in pending orders automatically return to the stock if orders are cancelled. Scope: STORE VIEW.
Read more #
How to Manage Magento Store Inventory?
ADMIN: System → Configuration → Inventory Tab
Thanks to R.S as he helped me more & more.
I followed all instructions on R.S's reply https://stackoverflow.com/a/13330543/1794834 and I've only changed the observer code. Here is the observer code that worked with me on Magento 1.7.0.2.
$curr_date = date('Y-m-d H:i:s');
$order = $observer->getEvent()->getOrder();
foreach ($order->getItemsCollection() as $item)
{
$productId = $item->getProductId();
$qty = (int)$item->getQtyOrdered();
$product = Mage::getModel('catalog/product')->load($productId);
$stock_obj = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockData = $stock_obj->getData();
$product_qty_before = (int)$stock_obj->getQty();
$product_qty_after = (int)($product_qty_before + $qty);
$stockData['qty'] = $product_qty_after;
/*
* it may be case that admin has enabled product add in stock, after product sold,
* he set is_in_stock = 0 and if order cancelled then we need to update only qty not is_in_stock status.
* make a note of it
*/
if($product_qty_after != 0) {
$stockData['is_in_stock'] = 1;
}else{
$stockData['is_in_stock'] = 0;
}
$productInfoData = $product->getData();
$productInfoData['updated_at'] = $curr_date;
$product->setData($productInfoData);
$product->setStockData($stockData);
$product->save();
}

Resources