How to call mycart product names in sidebar - magento

I'm using magento 1.6.1.0. I included new sidebar in my cart page where it'll show product title, total amount & proceed to checkout button as one by one. I included total amount by calling <?php echo $this->getChildHtml('totals'); ?> and proceed to checkout button by calling <?php echo $this->getChildHtml('methods') ?>
But i don't know how to call Product name.
Anyone know how to call my cart products name which are currently added in basket? Please share your idea to do this!
Thanks

You can get access to your cart object via Mage::getSingleton('checkout/cart').
Calling Mage::getSingleton('checkout/cart')->getItems() will get you your current cart item collection that you can iterate through to get product names as well as other details.
$items = Mage::getSingleton('checkout/cart')->getItems();
foreach ($items as $item) {
echo $item->getName();
}

Related

Magento get stock quantity at related products, upsells, crosssells in frontend

I need to get the stock quantity for each product in the section where "Related products" is showed in frontend. Using Magento 1.9.
This function will not help me show the actual qty in my related products section:
Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
The function shows stock qty in the product view page and the catalog collection but does not work for related products.
What to do?
I am not sure if you are actually calling the above piece of code inside a PHTML file.
Assuming you are using the RWD theme and calling it inside a PHTML directly (which is not as per Magento Standards), the file location would be app/design/frontend/rwd/default/template/catalog/product/list/related.phtml
Inside, the FOREACH loop, call the below piece of code (screenshot attached for reference - frontend display):
<?php echo Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty(); ?>
The ideal solution would be overriding the CORE file:
app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
like below:
app/code/local/Namespace/Modulename/Model/Stock/Item.php
in order to add a new function:
<?php
class Namespace_Modulename_Model_CatalogInventory_Stock_Item extends Mage_CatalogInventory_Model_Stock_Item
{
public function getItemStockQty($product)
{
return $this->loadByProduct($product)->getQty();
}
}
And inside the PHTML file, under the FOREACH LOOP call this function as below:
<?php echo $this->getItemStockQty($_item); ?>
Screenshot
Hope this helps.
Happy Coding...

How to add Rating of a product in products listing?

I am displaying products of a particular category to the homepage content section.
I have made a separate .phtml file to display my homepage.
Now I want to show the ratings of a product (products are already rated). How do I show it?
If you look at the category listing template it's pretty easy to work out how category pages render out the review summary to show the rating block.
First load the product in question:
$product = Mage::getModel('catalog/product')->load($id);
Then create a product listing block to give access to the correct methods:
$block = Mage::app()->getLayout()->createBlock('catalog/product_list');
Finally run the getReviewsSummaryHtml() method and pass it the product to get the summary HTML.
$html = $block->getReviewsSummaryHtml($product, 'short');
You can do this.
$_product = Mage::getModel('catalog/product')->load($id);
if ($_product->getRatingSummary() && $rating = $this->getReviewsSummaryHtml($_product, 'short')) :
echo $rating;
else:
echo "<a href='$_product->getProductUrl()'>" . $this->__('Be the first to review this') . "</a>";
endif;

Directly go to Product Detail page on click the category?

In my store, one of the category has only one product. Is it possible to take the user directly to the product detail page of this one product whenever they click this category in the nav bar? i want to use some code to get this?
Put this in the product grid or list foreach loop in /app/design/frontend/default/[template]/template/catalog/product/list.phtml
<?php if ($_productCollection->count() == 1) {
$url = $_product->getProductUrl();
Mage::app()->getFrontController()->getResponse()->setRedirect($url); }
?>

magento tracking pixel

There are two variables which are required in the tracking pixel which needs to be placed on category, product info, cart and confirmation page.
I've managed to get the Prod list and Prod working, however, the second two are causing me problems.
I can echo out the sku in the cart, however, the products are configurable products so it's duplicating the sku in the output. The code I'm using is below:
<?php
// $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();foreach($items as $item) { echo ''.$item->getSku().' ';}
?>
How do i change this to just display the one configurable SKU?
The second element is the Category name that the product exists in, Any one got any ideas on that? I've tried multiple variations but they've either broke the page or returned nothing.
Any help would be appreciated. If someone could also give me examples of how these would work on the confirmation page as well, that would be great.
Thanks for your help.
Check for the products visibility (simple products attached to a configurable would not be visible):
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {
if ($item->getProduct()->isVisibleInSiteVisibility()) {
echo ''.$item->getSku().' ';
}
}
With regards to the category name, a product can appear in multiple categories so im not sure how you want to handle that. Also, there is a concern that you are beginning to duplicate code across several template files. You will want to consider moving this all out to a block.
Anyway, to get the category names that the product belongs to here is at least one method of doing this...
$categoryCollection = $item->getProduct()->getCategoryCollection()
->addAttributeToSelect('name');
foreach($categoryCollection as $category) {
echo $category->getData('name') . "<br/>";
}

Magento:Getting selected attribute value of product in Checkout Shipping module

In my website, some products are not for sale in California. While the user checkouts, I have to do a validation like if the cart has items not for sale in California and user's shipping address is in CAlifornia, prevent user from checking out.
The sale in CA is set from admin side using an 'avl' attribute.
here is the code I use to iterate through the cart and check for the attribute
.....................
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $_item){
$_product = $_item->getProduct();
//If atleast one product has availability status set to California, then set the flag and break.
if($_product->getResource()->getAttribute('avl')->getFrontend()->getValue($_product) == 'NC'){
$flag = true;
break;
}
......................
Well, now the problem is I'm not able to get the 'avl' value as set from the Admin side.
This code is in local//Checkout/Block/Onepage/Shipping.php
Any idea about how to retrieve the attribute value?
Thanks in advance.
To get a product's attributes during checkout, the easiest way is to just completely load the product:
$_product = Mage::getModel('catalog/product')->load($_item->getProduct());
$_avl = $_product->getAvl();
There are of course overheads with loading a product but this is the fastest way to get an attribute value from a cart item.
<?php $_item = $this->getItem()?>
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId())?>
<?php echo $_product->getResource()->getAttribute('attribute_code')
->getFrontend()->getValue($_product); ?>
app/design/frontend/your_default/your_default/template/checkout/cart/item/defaul‌​t.phtml

Resources