I want to get the shopping cart details, by using Magento's getQuote function. How can I do this?
$cart = Mage::getModel('checkout/cart')->getQuote();
When I print the $cart Page stops execution and blank page is shown.
But when I write
$cart = Mage::getModel('checkout/cart')->getQuote()->getData();
and print the $cart an array will show. But I want to track the complete cart data (product Id,Product price like all information).
Is there any other method by which I can find the shopping card data?
The object returned by getQuote is a Mage_Sales_Model_Quote. It has a method getAllItems which in turn returns a collection of Mage_Sales_Model_Quote_Item objects.
All this means you can inspect products like this:
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
$productId = $item->getProduct()->getId();
$productPrice = $item->getProduct()->getPrice();
}
PS. The reason you get a blank page is because dumping a whole object likely fell into recursion and the page timed out, or PHP ran out of memory. Using getData or debug is safer but, as you saw, doesn't return the protected/private variables.
Get shopping cart items using getQuote() method
$cart = Mage::getModel('checkout/cart')->getQuote()->getItemsCollection();
echo "<pre>";
print_r($cart->getData());
Get Shopping Cart items without getQuote() method
$cart = Mage::getModel('checkout/cart')->getItems();
echo "<pre>";print_r($cart->getData());
Related
I have added a simple code to track each purchase on the site. All products have different types of rules based on categories to subtract a predefined percent of amount of product price.
But when there are configurable products in the cart then it is displaying me both products, original product price and the price of the selected option for the product details in the order detail.
If I have a lipstick in my cart and the color I selected is "RED" then lipstick price will change. But it is displaying me the original lipstick price as well as the red lipstick price in the order details.
I have added the code on order success page.
$orders = Mage::getModel('sales/order')->getCollection()
->setOrder('created_at','DESC')
->setPageSize(1)
->setCurPage(1);
$orderId = $orders->getFirstItem()->getEntityId();
$order = Mage::getModel('sales/order')->load($orderId);
$items = $order->getAllItems();
foreach ($items as $itemId => $item)
{
$pid = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($pid);
$_finalPrice = $product->getFinalPrice();
echo $price = Mage::helper('core')->currency($_finalPrice,true,false);
// Some Code
}
I have also tried
$tempmain = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
$temp = $tempmain->getAllItems();
$total = $tempmain->getGrandTotal();
foreach ($temp as $itemId => $item)
{
$pid = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($pid);
$_finalPrice = $product->getFinalPrice();
echo $price = Mage::helper('core')->currency($_finalPrice,true,false);
// Some Code
}
With the same results.
How to get only "RED" lipstick price in Order details?
Thanks in advance.
This is the details for all the availabel lipstick and their values:
when I select "Tango" as the color for lipstick on front end:
I am getting the Price of both products on success page:
Here is the Order Details Screen shot:
In frontoffice, when a customer add a configurable produt in his cart and order it, you'll see (in Database) 2 lines (2 quote_items and 2 order_item).
It's normal because magento needs to store the configurable product and the linked simple produt corresponding to the user selection (and linked to the master configurable product).
When displaying order details, Magento handles that difference by checking if a product has a parent id. You can do the same to ignore some order_item :
foreach($items as $item){
if ($item->getParentProductId() {
continue; // ignoring simple product associated to master order item (configurable)
}
// your code
}
I'm NOT 100% sure what your tying to do, but if you are on the order success page then this is the way to retrive the current order info (get the the last order for the db may not always be the current order on the success page if two user make a purchase millisecond apart)
Try adding this to Success.phtml
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$items = $order->getItemsCollection();
foreach($items as $item){
if ($item->getParentItem()) {
continue;
}
echo Mage::helper('core')->currency($item->getPrice(),true,false);
}
I have a catalog with near 90000 products. I need to retrieve all the products sku that have no image associated. With a sql query I can get the id list of all the products without image. From that ID I need to get the product sku. So, I have an $ids array with all the products without image (near 60000).
I'm trying to get all the corresponding skus by using something easy with the magento api:
foreach ($ids as $id){
$product = Mage::getModel('catalog/product')->load($id);
echo $product->getSku()."\n";
}
But this causes a PHP Fatal error: Allowed memory size... (memory size is 1024Mb and I cannot change it).
My question is: from this $ids array, how can I get all the corresponding sku without causing a memory size error? Is there a lighter way of getting a product attribute having the product id?
Currently you are loading a lot of not needed product data and this causes a fatal error. In your case you need only product sku. I am suggestion to use Magento collections.
I guess, that the following code snippet will work for you:
$productsCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('entity_id', array('in' => $ids));
foreach($productsCollection as $product) {
echo $product->getSku();
}
Magento automatically adds the sku to the select, but pay attention, that sometimes you may want to get some custom product attribute, e.g. attribute with code "color". Then you need to add ->addAttributeToSelect('color') and the code will look like:
$productsCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('entity_id', array('in' => $ids))
->addAttributeToSelect('color');
foreach($productsCollection as $product) {
echo $product->getSku();
echo $product->getColor();
}
If you want to get all product attributes you can use ->addAttributeToSelect('*')
To get separate attribute without loading product
$resource = Mage::getResourceSingleton('catalog/product');
$sku = $resource->getAttributeRawValue($productId, 'sku', $storeId)
I'm trying to determine what products are currently in my cart from inside the UPS shipping module in Magento 1.6. In the function (USA/Model/Carrier/Ups.php):
public function setRequest(Mage_Shipping_Model_Rate_Request $request)
I've tried to call:
$r->getProduct();
But all that it returns is a non-sensical string "GNDRES". Any help here would be greatly appreciated.
You could try this (untested) code:
$items = Mage::getSingleton("checkout/session")->getQuote()->getAllVisibleItems();
foreach ($items as $item) {
$product = $item->getProduct();
// Do something with $product
}
I'm trying to display the number of total number of sales for the currently viewing product on a block, so far i have, with the help of a stackoverflow thread:
$product = Mage::registry('current_product')->getId();
$productID = Mage::getModel('catalog/product')->load($product)->getId();
$productReport = Mage::getResourceModel('reports/product_sold_collection')->addOrderedQty()->addAttributeToFilter('id',$productID);
foreach ($productReport as $product) {
$product1 = $product->getOrderedQty();
var_dump($product1);
}
I am able to load all sales quantities, but when i add the addAttributeToFilter
Fatal error: Call to a member function getBackend() on a non-object in C:\wamp\www\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816
It also happens if i pass $product directly, both are strings though. i don't know how to get the object i should pass to the addAttributeToFilter method, or if it should work with a string parameter.
Without trying it myself I suspect you need to filter by entity_id (and your second line is redundant).
$product = Mage::registry('current_product');
$productReport = Mage::getResourceModel('reports/product_sold_collection')
->addAttributeToFilter('entity_id', $product->getId())
->addOrderedQty();
$qty = $productReport->getFirstItem()->getOrderedQty();
EDIT: Product and category collections also have an addIdFilter() method for that purpose, e.g
->addIdFilter($product->getId())
See Mage_Catalog_Model_Resource_Product_Collection::addIdFilter() for more details.
I'm using grouped products to track promotions. Simple products will at times belong to multiple grouped products, so checking parentProductIds is of no use. I'm wondering how to track the grouped product ID when a product is purchased through the grouped (promotion) SKU. I can see it's being stored in info_buyRequest and super_product_config within the orders, but how do I get that information back out? And is there a way to get it out in the cart/quote?
I was able to get it with the following code in cart.phtml, in the foreach($this->getItems() as $_item):
$values = unserialize($_item->getOptionByCode('info_buyRequest')->getValue());
$parentId = $values['super_product_config']['product_id'];
Depending on where you want to get this information, you could get it after a checkout process when the sales is saved. Then you could use the events sales_order_save_after and create a method in a class to get the items of a grouped product.
What is important here is the object of a class Mage_Sales_Model_Order_Item which has information about the product and the parents of a product
Here is an example:
public function processSalesOrder($observer)
{
$order = $observer->getOrder()
$quoteItems = $order->getItemsCollection(null, true);
/*#var $item Mage_Sales_Model_Order_Item */
foreach ($quoteItems as $item) {
$parent = $item->getParentItem();
if(!is_null($parent)){
// do your stuff - you have a product parent which has children product
// $item is the children
echo 'The parent product is ' . $parent->getSku();
echo 'One of the children product is' .$item->getSku();
}
}
On cart page grouped product is treated as a simple product. In Magento 2 you can get the parent id of these simple products from session. This worked for me:
<?php
$catalogSession = $_SESSION['catalog'];
$parentId = $catalogSession['last_viewed_product_id'];
?>