I want to get the remain quantity of a given product. I'm going to create a report in admin panel which highlights the remaining quantity and re-order level quantity against each product. I was able to get the re-order level quantity by inventory details. And I want to get remain quantity for a given product. Please any suggestions?
Try
<?php
$num= Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
echo "Remaining products: ".$num;
?>
If you are looking for the remaining quantity for products in the cart, you need to take into account configurable vs simple products:
<?php
$quote = Mage::getModel('checkout/cart')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item) {
$cart_product = $item->getProduct();
if ($option = $item->getOptionByCode('simple_product')) {
$cart_simple_product = $option->getProduct();
} else {
$cart_simple_product = $this->getProduct();
}
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($cart_simple_product);
$max_available_quantity = (int)$stock->getQty();
}
?>
Related
I'm trying to get the subtotal of each row/line item in the success page but I'm not sure what to use.
I've tried getRowTotal() and getRowTotalInclTax() but both are showing up blank.
I'm able to get the whole cart subtotal but want I need is the individual product subtotal.
Here's part of the code I'm using:
// Get order details
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId());
// Get Each Product Details
$items = $order->getAllItems();
foreach($items as $i) {
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($i->getProductId());
echo $product->getName();
echo $product->getSku();
echo $product->getRowTotal(); // This is where its just coming back blank/null
}
There you are loading the product entity: The product entity cannot contain the information of the order.
You must use the Order Item entity that you already use to have the total row
Something like this :
// Get order details
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId());
// Get Each Product Details
$items = $order->getAllItems();
foreach($items as $i) {
echo $i->getProduct()->getName();
echo $i->getProduct()->getSku();
echo $i->getRowTotal();
}
Hi for a Magento website i need to load a custom attribute in the shopping cart.
i use getmodel function to load the items. but i have no clue how i load the attribute. maybe i haven't configure the attribute right. i have enabled yes for enable on product listing. de attribute code is 'staffel_percentage'. it is just a regular string
also when i change the price per product it doesn't change the subtotal maybe this is because we already change the price for the product on the rest of the website?
maybe it is in the event? i use this event: controller_action_layout_render_before.
this is the code in observer i use for it
$cart = Mage::getModel('checkout/cart')->getQuote(); // Gets the collection of checkout
foreach ($cart->getAllItems() as $item) { // adds all items to $item and does the following code for each item
if ($item->getParentItem()) { // If items is part of a parent
$item = $item->getParentItem(); // Get parentItem;
}
//echo 'ID: '.$item->getProductId().'<br />';
$percentage = 80;// init first percentage
$quantity = $item->getQty(); //quantity
//$staffelstring = loadattribute//loads attribute
//gives the right percentage per quantity
//$staffelarray = explode(';', ^);
//foreach($staffelarray as $staffels){
//$stafel = explode(':', $staffels);
//if($quantity >= $stafel[0]){
//$percentage = $Stafel[1];
//}
//}
$currency = Mage::app()->getStore()->getCurrentCurrencyRate(); // Currencyrate that is used currently
$specialPrice = (($this->CalculatePrice($item) * $currency)* $percentage) / 100; //New prices we want to give the products
if ($specialPrice > 0) { // Check if price isnt lower then 0
$item->setCustomPrice($specialPrice); //Custom Price will have our new price
$item->setOriginalCustomPrice($specialPrice); //Original Custom price will have our new price, if there was a custom price before
$item->getProduct()->setIsSuperMode(true); //set custom prices against the quote item
}
}
You need to load the product right after your first if statement
$product = Mage::getModel('catalog/product')->load($item->getId()); // may be $item->getProductId() instead here
Then on the next line after that you can add some logging statements that will appear in var/log/system.log
Mage::log($product->getData()); // this will output all of the product data and attributes. You will see your custom attribute value here if it is set on this product.
If there is a value set for your product for your custom attribute then you can get it like this
$value = $product->getData('staffel_percentage');
As for the prices changing, not sure how you have the prices set up. You need to set a positive or negative number to add or subtract from the price in the fields found in the parent product configuration page in Catalog > Products > Your product > Associated Products.
See this image for what the section looks like.
I am calculating shipping charge for each product with respect to their weight,i want to get each product weight added to a cart separately instead of total weight.
$individual_weight=array();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {
$weight = 0;
$weight = ($item->getWeight() * $item->getQty()) ;
$individual_weight[] = $weight;
}
You will get all the individual product weight stored in the array
I am creating a magento extension. In which I want to update the item quantity in cart programmatically. I am using the following code to display the items in cart.
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
//Do something
}
What i want is to update the quantity on cart for a specific product. I know it can be done like this
$pid=15;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
if($pid==$item->getId())
$item->setQty($qty);
}
But I don't like this method as it would go through each and every product to update the quantity of a single product. I wonder if there is a way to update the quantity in one line i:e without using for loop.
You have the product_id, not the item_id right ?
If it's the case it's impossible to get the product id without performing the whole items.
Look at Mage_Sales_Model_Quote::getItemByProduct($product); you'll see it performs the whole catalog.
Basically I'll do it like this :
//get Product
$product = Mage::getModel('catalog/product')->load($pid);
//get Item
$item = $quote->getItemByProduct($product);
$quote->getCart()->updateItem(array($item->getId()=>array('qty'=>$qty)));
$quote->getCart()->save();
You should optimise this with some quick tricks :
$quote->hasProductId($pid) to check if product is in the cart
and
($qty!=$item->getQty())
to check if quantity is not already good...
Please note that it is untested code, and some kind of reflexions to help you find the solution, I didn't do the job for you.
Kind Regards,
I am sure it will work. Try this. It's save's my time.
public function updateCartAction(){
$item_id = $this->getRequest()->getParam('item_id');
$qty = $this->getRequest()->getParam('qty');
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->updateItem($item_id, array( 'qty' => $qty));
$quote->save();
echo "Sssssss";
}
another solution
$quote = Mage::getModel('sales/quote')->setStoreId($storeId)->load($cartid);
$cartItems = $quote->getAllItems();
foreach ($cartItems as $item) {
if($cartiemid==$item->getId()){
$item->setQty($qty);
$item->save(); // you need to save the item after qty update
}
}
$quote->save();
I'm setting up a tracking code for an affiliate programs. Now we give different commissions. One for Food related products and one for Non-Food related products. These are also the tax classes we have (Food, Non Food).
I need to display the sub total for food products and a subtotal for non food products.
I use the following code but that doesn't work:
<?php
//Get Order Number & Order Total
$order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$grandamount = number_format($order->getGrandTotal(),2);
$coupon = $order->getCouponCode();
$amountfood = number_format($order->getSubtotal('Food'), 2);
$amountnonfood = number_format($order->getSubtotal('Non_Food'), 2);
$discount = number_format(0.00 - $order->getDiscountAmount(), 2);
?>
If I use $amountfood = number_format($order->getSubtotal(), 2); it does work for the subtotal including both the food and non food values.
Could someone please help me with that.
Thanks,
Daniel
I don't think this information is available directly: magento stores the subtotal and taxes in a global way, only the total, no detailed information.
What you could do, is fetch the ordered products, for each one get his tax class and store in an array the sale value.
Something like this:
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$subtotals = array();
foreach ($items as $_item) {
if (array_key_exists($subtotals[$_item->getTaxClassId()])) {
$subtotals[$_item->getTaxClassId()] += $_item->getRowTotal();
} else {
$subtotals[$_item->getTaxClassId()] = $_item->getRowTotal();
}
}
not sure if the "if" is needed though.
hope that helps