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()
Related
There are a few threads on here explaining how to get a product attribute in the cart or checkout. However, I just can't get it to work.
In the cart I can see the product attribute, but not in the checkout.
From what I've read, the checkout doesnt know about the product, so I need to load it. This would explain why Im not seeing it.
I've tried a lot of things, but I either don't get my shipping step to load in the cart, or it just doesn't display.
Any help would be great! Here is my code in a custom shipping module.
$cart = Mage::getSingleton('checkout/session');
//Order value by category
$gold = 0;
foreach ($cart->getQuote()->getAllItems() as $item) {
$itemPrice = $item->getPrice();
$qty = $item->getQty();
$metalType = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getAttributeText('metal');
if ($metalType == "Gold") {
//Apply some business logic
$gold = $itemPrice * $qty;
}
}
Magento get cart single item price incl. tax
Magento Product Attribute Get Value
In the afterLoad of the Mage_Sales_Model_Resource_Quote_Item_Collection object, a method is called to _assignProducts to each quote item. This method loads a product collection using the following code:
$productCollection = Mage::getModel('catalog/product')->getCollection()
->setStoreId($this->getStoreId())
->addIdFilter($this->_productIds)
->addAttributeToSelect(Mage::getSingleton('sales/quote_config')->getProductAttributes())
->addOptionsToResult()
->addStoreFilter()
->addUrlRewrite()
->addTierPriceData();
If you add the product attribute to the config.xml of your new module in the following way, it should be added as an attribute to be selected in the product collection.
<sales>
<quote>
<item>
<product_attributes>
<metal/>
</product_attributes>
</item>
</quote>
</sales>
I have customized search product grid as per client need(created new grid and disabled search grid) under sales order create page.
I have created new grid successfully after "Items Ordered" block.
When i click "Add product(s) To Item" button of my custom grid, I got product id and quantity of that selected product in my custom controller file.
I have tried to add those products to quote, Nothing has been changed. I dunno, how to add these selected products from my custom grid under "Items Ordered" block.
I have tried following code to add products to quote.
$customer_id = 26; // set this to the ID of the customer.
$customerObj = Mage::getModel('customer/customer')->load($customer_id);
$quoteObj=Mage::getModel('sales/quote')->assignCustomer($customerObj);
$quoteObj = $quoteObj->setStoreId(Mage::app()->getStore()->getId());
$productModel=Mage::getModel('catalog/product');
$productObj = $productModel->load($_id);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
$quoteItem->setQty($qty);
$quoteObj->addItem($quoteItem);
$quoteObj->collectTotals();
$quoteObj->save();
I'm not sure, That block containing quote products or else
Have any idea, How to add selected products to "Items Ordered" block in sales order create page.
Eagerly awaiting for your response!
I have found the solution after long hunt.
Just passed products array to productGridAddSelected() method like below
order.productGridAddSelected(products);
products array must be like
products[product_id]= quantity;
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!
I am currently working on the development of a management module Cart on Magento 1.9
I'm stuck on adding a product to my cart (I tried many different things) and that's why I solicits your help.
My module extends the rest API of magento and I have already managed update to my cart (quantity of products) but now I'll wish to add a new product via the POST method.
Of course I'm logged as customer. I defined the creation privileges for this role. (I can do the update without problems)
Here is my code :
protected function _create(array $data){
$store = $this->_getStore();
Mage::app()->setCurrentStore($store->getId());
$cart = Mage::getModel('checkout/cart');
$cart->init();
$productCollection = Mage::getModel('catalog/product')->load(4);
// Add product to cart
$cart->addProduct($productCollection,
array(
'product_id' => $productCollection->getId(),
'qty' => '1'
)
);
// Save cart
$cart->save();
}
In this simple example, I try to add the product id 4 in quantity 1.
My problem is that I have no error in the log, and everything seems to be past. But when I get my cart, there is no product to add...
in return I have a code 200 OK
Do you have any suggestions to help me?
Thanks a lot for your help
regards
I finally found the solution after traveling all internet;)
In fact when you want to reach the cart before checkout, magento uses the definition "Quote" ... Not easy to understand for a beginner on Magento....
So in order to facilitate research of those who are like me had troubles, here is my code to add a new product in the cart (before checkout) :
//$data['entity_id'] = The id of the product you want to add to the cart
//$data['qty'] = The quantity you want to specify
protected function _create(array $data)
{
$store = $this->_getStore();
Mage::app()->setCurrentStore($store->getId());
// Get Customer's ID
$customerID = $this->getApiUser()->getUserId();
// Load quote by Customer
$quote = Mage::getModel('sales/quote')
->loadByCustomer($customerID);
$product = Mage::getModel('catalog/product')
// set the current store ID
->setStoreId(Mage::app()->getStore()->getId())
// load the product object
->load($data['entity_id']);
// Add Product to Quote
$quote->addProduct($product,$data['qty']);
try {
// Calculate the new Cart total and Save Quote
$quote->collectTotals()->save();
} catch (Mage_Core_Exception $e) {
$this->_error($e->getMessage(),Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
}
}
I hope this can help someone
Regards
Carniflex
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.