Magento: Can't get attribute value for last item in cart - php

I'm running into a problem with getting attribute values for products that have been placed in a users cart.
I have the following code:
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
foreach ($session->getQuote()->getAllItems() as $item) {
$_product = Mage::getModel('catalog/product')->load($item->getId());
$attributeValue = $_product->getAttributeText('availability');
echo $attributeValue;
}
And it works fine for all products in the cart except the very last one. For example, I'm trying to get the value of an "availability" attribute I've created that can have only 1 of the following values "Backorder", "Preorder", "Out of Stock". If I have 3 items in my cart I can get the correct values for the first 2, however for the last item it just displays "No".
I have double checked each item to make sure all attributes are set correctly and it happens with any number of items in the cart.
Hopefully it's just a stupid mistake on my part.
Any help would be appreciated.
Thanks.
SOLVED:
Changing:
$_product = Mage::getModel('catalog/product')->load($item->getId());
to
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
Fixes the issue.

The problem is in that line:
$_product = Mage::getModel('catalog/product')->load($item->getId());
With $item->getId() you are getting ID of Mage_Sales_Model_Quote_Item and not Mage_Catalog_Model_Product. You have to use $item->getProductId() instead to get ID of actual product associated with current quote item.

Related

How to pass first item unit price to dataLayer in cart?

I'm setting up a dataLayer in the WooCommerce cart trying to pass the first item value to it. The code below passes both the values as one variable and that's not what I want - I want an array to be built for each item in the cart.
One array for the prices, and one for the quantity.
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'firstItemUnitPrice': '<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$price = get_post_meta($values['product_id'] , '_price', true);
echo $price;
}?>'
});
</script>
The output from the code above is:
firstItemunitprice: 1144
There are two products in cart, one worth $11 and one $44.
I know how to separate them but I want them to be added as an array to the dataLayer. Help please?
I understand I need to loop through the cart, but I want each item price and each item quantity to be placed in a dataLayer.

Magento - duplicate quote item to new row and new price

I have an observer where I add a new quote item when the qty is more than one. Heres the code I use to create this quote item:
$quote_item = Mage::getModel('sales/quote_item');
$_product = Mage::getModel('catalog/product')->load($oldQuoteItem->getProduct()->getId());
$quote_item->setProduct($_product);
$quote_item->setQuote($quote);
$quote_item->setOriginalCustomPrice($oldQuoteItem->getProduct()->getPrice());
$quote_item->save();
It work's fine for simple products, but when I will duplicate a quote item with a configurable product, it will only take the parent product, and not the simple product that belongs to it, and therefore I get an error where the product is not on stock.
Anyone who got an idea, on how to duplicate the excact product to the new quote item ?
I have had success with the following for all product types.
$quote = Mage::getSingleton('checkout/session')->getQuote();
$oldQuoteItem = $quote->getItemById(ITEM_ID);
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($oldQuoteItem->getProductId());
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct($product, $oldQuoteItem->getBuyRequest());
$cart->save();
You should be able to then modify the item price.
Bear in mind if you want to do this for multiple products you must only save the cart once at the end after every $cart->addProduct()

How to override existing quantity already in cart?

Problem I am having here is that people go to a Product page, set a quantity on how much they want of that product and add to cart (which than takes them to the cart page). Client wants the ability to go back to the shop/product page if they want to make further changes to that product (honestly this is strange, since they can do this on the cart page, but whatever). But when people go to update the quantity on the product page, instead of updating the quantity, it adds to the existing quantity for that product.
How to get it to update the existing quantity when the quantity has been submitted more than once on the product page? Is there a woocommerce loop that I can take advantage of here?
Thanks guys, I have dug through the plugin and coded the solution below:
add_action('woocommerce_add_to_cart_handler', 'update_product_in_cart', 11, 2);
function update_product_in_cart($p, $q) {
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
$wCart = $woocommerce->cart->get_cart();
// If cart already exists, and product exists, than remove product, and add the new product to it.
if ($wCart)
{
$cart_item_keys = array_keys($wCart);
foreach($cart_item_keys as $key)
{
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
if ($productItemId == $currentProductId)
{
// If you want to empty the entire cart...
// $woocommerce->cart->empty_cart();
// If you want to remove just the product from the cart...
$woocommerce->cart->set_quantity($key, 0);
}
}
}
}
// This adds the product to the cart...
return $q;
}
This gets added to functions.php, and basically, if the product id exists in the cart, it removes the product and the return of $q adds in the new product info.
Works a charm!

Magento: Update cart quantity programmatically

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();

Magento get price including tax in a none-template file

At the moment i am trying to get the product price including tax in a php file for my product feed. I have this code at the moment:
$_product = Mage::getModel('catalog/product')->load($productId);
$_priceIncludingTax = $this->helper('tax')
->getPrice($_product, $_product->getFinalPrice());
Problem is that since that of course the '$this->' part doesn't work so well from the file.
Anyone know how i can still get the price including tax in this file?
You can get a helper-instance in any file using:
Mage::helper('tax')
Your full code is:
$_product = Mage::getModel('catalog/product')->load($productId);
$_priceIncludingTax = Mage::helper('tax')
->getPrice($_product, $_product->getFinalPrice());
Thanks #Alex:
If the product has FinalPrice special price is the final price of the product to access the most serious tax base price:
$_product = Mage::getModel('catalog/product')->load($p->getId());
$_specialPriceIncTax = Mage::helper('tax')
->getPrice($_product, $_product->getFinalPrice());
$_priceTax = Mage::helper('tax')
->getPrice($_product, $_product->getPrice());

Categories