how to get checkout cart specific item totat price? - magento

I have a custom html I want to implement this html into checkout cart page . I have implement it but i cant get cart item total price , eg. I have 5 item in cart . I have updated only one specific item in the cart then I don’t get these specific item total price .
default item $50 *1 =$50
After Update quantity
item price : $50 * 2 = ??(I want to get total of this specific item ).
Thanks

You can calculate the price as:
<?php
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {
$qty = $item->getQty();
$price = $item->getPrice();
$totalPrice = $qty * $price;
}
?>

To get cart item specific details, you can use :
$quote = Mage::getSingleton('checkout/session')->getQuote();
foreach ($quote->getAllItems() as $item){
$qty = $item->getQty() ;
$price = $item->getPrice();
$totalPrice = $qty * $price;
}

i am using default magento functionality to get specific item price in magento chekout cart page
<?php if ($canApplyMsrp): ?>
<span class="cart-msrp-subtotal">--</span>
<?php else: ?>
<?php if (Mage::helper('weee')->typeOfDisplay($_item, array(0, 1, 4), 'sales') && $_item->getWeeeTaxAppliedAmount()): ?>
<?php echo $this->helper('checkout')->formatPrice($_item->getRowTotal() + $_item->getWeeeTaxAppliedRowAmount() + $_item->getWeeeTaxRowDisposition()); ?>
<?php else: ?>
<?php echo $this->helper('checkout')->formatPrice($_item->getRowTotal()) ?>
<?php endif; ?>
<?php endif; ?>

Related

Cart page content, display product individual according to quantity

if i add product with quantity two then in cart contect there is only one product with 2 quantity display. but i want to display separate rows of same product according to quantity.
like cart contect should be as below.
product name Qty price
ABC 1 $10
ABC 1 $10
Finally i got solution. Here is an answer
open this file \app\code\core\Mage\Sales\Model\Quote.php and search for "_addCatalogProduct" function
and replace
$item = $this->getItemByProduct($product);
if (!$item) {
$item = Mage::getModel('sales/quote_item');
$item->setQuote($this);
if (Mage::app()->getStore()->isAdmin()) {
$item->setStoreId($this->getStore()->getId());
}
else {
$item->setStoreId(Mage::app()->getStore()->getId());
}
$newItem = true;
}
to with commenting code.
// $item = $this->getItemByProduct($product);
//if (!$item) {
$item = Mage::getModel('sales/quote_item');
$item->setQuote($this);
if (Mage::app()->getStore()->isAdmin()) {
$item->setStoreId($this->getStore()->getId());
}
else {
$item->setStoreId(Mage::app()->getStore()->getId());
}
$newItem = true;
// }
and in \app\code\core\Mage\Checkout\controllers\CartController.php
replace
$cart->addProduct($product, $params);
to
if($params['qty'] == 0 || $params['qty'] == '')
{
$params['qty'] = 1;
}
$quantity = $params['qty'];
for($loop=1; $loop<=$quantity; $loop++)
{
$params['qty'] = 1;
$cart->addProduct($product, $params);
}
If you really must do this then you could try creating an observer for the event sales_order_place_before and then in your observer you could modify the quote by looping through it and finding multiple qty items, change the qty back to 1 and then add the remainder of items as quote items to the quote object. Not sure if Magento will group them together again during/after the order is placed. You could maybe do the same thing using the event sales_quote_product_add_after which might be better as the user will see the individual lines on the cart page, but again, don't know if Magento will group the qty's back together during the checkout process without actually having a go at making it work. If it does, you could maybe try setting the product to be a super product and override the sku so it has a unique identifer tagged on (e.g. SKU100-1, SKU100-2 etc) before you add it to the quote.
What about this:
Replace this
<?php foreach($this->getItems() as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
<?php endforeach ?>
with that on line 130 default theme /app/design/frontend/yourtheme/default/template/checkout/cart.phtml
<?php foreach($this->getItems() as $_item): ?>
<?php $qty = $_item->getQty() ?>
<?php for($i=0; $i < $qty; $i++) : ?>
<?php $_item->setQty(1); ?>
<?php echo $this->getItemHtml($_item); ?>
<?php endfor; ?>
<?php endforeach ?>

unable to get product price in custom file magento

Hi i am new to magento and i need to get the products from a particular category
for this i have use
<?php
$id1=4;
$category1 = Mage::getModel('catalog/category')->load($id1);
$collection1 = $category1->getProductCollection();
$collection1->addAttributeToSelect('name');
$collection1->addAttributeToSelect('description');
$collection1->addAttributeToSelect('image');
$collection1->addAttributeToSelect('producturl');
$collection1->addAttributeToSelect('prlce');
$products1 = $collection1->getItems();
$_helper1 = $this->helper('catalog/output'); ?>
<?php foreach ($products1 as $product1){ ?>
<?php echo $this->htmlEscape($product1->getPrice()) ?>
<?php } ?>
in this it is showing the name , image and url but when i am trying to echo price it doenot show anyhing.Please suggest me where i am doing mistake
This is how you do it:
<?php
// Test Params
$cat_id = 4;
$store_id = 1;
// Load Category
$category = Mage::getModel('catalog/category')->load($cat_id);
// Load Category Products
$categoryProducts = $category->getProductCollection();
// Iterate Through Product Collection
foreach ($categoryProducts as $categoryProduct)
{
// Load Product At Specified Store Id
$product = Mage::getModel('catalog/product')
->setStoreId($store_id)
->load($categoryProduct->getId());
// Debug
print_r($product->getData());
// Get Price (e.g.)
echo 'Product Price: '. $product->getPrice()
}
?>

How to make second root category navigation in magento?

Other than the normal navigation I get when I add subcategories to the main root category, I want to be able to make a new root category, assign subcategories to it and have it display as a separate menu.
Is this possible?
May this can help you :Link 1Link 2
To retrieve another root category
<?php
echo '<pre>';
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id); // where $id will be the known category id
if(!empty($categories))
{
echo 'category name->'.$categories->getName(); //get category name
echo '<br>';
echo 'category image url->'.$categories->getImageUrl(); //get category image url
echo '<br>';
echo 'category url path->'.$categories->getUrlPath(); //get category url path
echo '<br>';
}
?>
now $id=9; is my new root category id.
To retrieve sub categories of these new root category ($id=9;) below is the following reference code.Customize it according to your requirements.
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
In an ideal world you would have found the answer by now, but in case you didn't I modified Nikhil's answer to work for me to basically do what you describe, minus any convenience at all...
$id=9;
$catagory_model = Mage::getModel('catalog/category');
$categories = $catagory_model->load($id);
if(!empty($categories))
{
$cats = explode(",", $categories->getChildren());
foreach($cats AS $c)
{
$cat = $catagory_model->load(trim($c));
echo ''.$cat->getName().'';
}
}
I'm just pasting what I used. The reality is you will have to build the html to make this do whatever you want it to do. If you have subcategories within your loop, you will have to run another fetch in the foreach part.
I am not familiar with magento enough to know what methods you can run on the $cat object. I did a print_r($cat) to examine the object and made a lucky guess that getUrlKey would be available.
Magento... pfft! you'd think ebay would have higher standards than this.

Magento, my cart, subtotal

My cart shows wrong subtotal and I think this is als oaffecting calculation of tax (20%)in the Shoppin Cart and in the Checkout (prices here are correct but Subtotal, and total, have no 20% Vat tax)
Any idea what could be the problem?
new user.. i can't post images
<?php
$subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
$totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals();?>
Total Items in Cart: <?php $totalItems = Mage::getModel('checkout/cart')->getQuote()->getItemsCount();
if($totalItems == 0){echo "0";}else{echo $totalItems;}?>
Subtotal: $ <strong><?php $subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal(); echo number_format($subTotal,2); ?>
<?php if(isset($totals['tax']) && $totals['tax']->getValue()) {
echo 'Tax: '.strip_tags(Mage::helper('core')->currency(number_format($totals['tax']->getValue(),2))); //Tax value if present
} ?>
<?php if(isset($totals['discount']) && $totals['discount']->getValue()) {
echo 'Discount: '.strip_tags(Mage::helper('core')->currency(number_format($totals['discount']->getValue(),2))); //Discount value if applied
} ?>
<?php if(isset($order->getShippingAmount)){
echo 'Shipping Charges:'.strip_tags(Mage::helper('core')->currency(number_format($order->getShippingAmount,2)));
}?>
Grand Total: $<?php $grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal(); echo number_format($grandTotal,2); ?>
Please use this code, it will be show all cart info.

Sorting categories in Magento according to the position in admin

I would like to know how to sort this list of categories (I followed this tutorial here http://www.devinrolsen.com/magento-custom-category-listing-block/) in magento by position in the admin panel? Currently it is sorted by id
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
<li>
<?php
$category = Mage::getModel('catalog/category')->load($catId);
echo '<a href="' . $category->getUrl() . '">';
echo $category->getName() . '</a>';
?>
</li>
<?php endforeach; ?>
</ul>
You're making way too much work for yourself trying to deal with IDs and stuff. The following is already sorted by position as standard.
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
<?php foreach($cats as $category): ?>
<li>
<?php echo $category->getName() ?>
</li>
<?php endforeach; ?>
</ul>
If you want to sort the categories by the position created in adminhtml you can then, since catalog/category is an instance of Mage_Catalog_Model_Resource_Category_Collection, make a query where you specify what you want to select, filter and/or sort.
The case here is getting categories from catalog_category_entity select only the name, filtering after the id and sort the query on the position.
<?php
$subcategories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id', $categoryId)
->addAttributeToSort('position', ASC);
?>
This is what I did:
$allCategories = Mage::getModel('catalog/category');
$CategoriesTree = $allCategories->getTreeModel()->load();
$categoriesIds =
$CategoriesTree->getCollection()->addAttributeToSort('position', 'asc')->getAllIds();
after to retrieve the categories:
$categoryChildren = array();
if ($categoriesIds) {
foreach ($categoriesIds as $categoryId){
$category = Mage::getModel('catalog/category')->load($categoryId);
$categoryChildren[] = $category;
}
}
and then:
// Sort by position
function comparePosition($a, $b) {
if ($a->position == $b->position)
return 0;
return ($a->position > $b->position) ? 1 : -1;
}
usort($categoryChildren, 'comparePosition');
Inverting the return (1 and -1) would obviously change the order.
It worked just fine for me.
Hope it helps someone.
I strongly suggest to lok here first http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections and also other articles in knowledge base are a must read for any magento dev.
<?php
$cats = Mage::getModel('catalog/category')->addAttributeToSort('yourfield', 'desc')->getCollection()->getChildren();
$catIds = explode(',',$cats);
?>
<?php
$model_category = Mage::getModel('catalog/category')->load($_category->getEntityId());
$sub_categories = $model_category->getCollection();
$sub_categories -> addAttributeToSelect('url_key')
-> addAttributeToSelect('name')
-> addAttributeToFilter('is_active',1)
-> addIdFilter($model_category->getChildren())
-> setOrder('position', 'ASC')
-> load();
?>
<?php foreach($sub_categories->getData() as $each_subcat): ?>
<?php $model_subcat = Mage::getModel('catalog/category')->load($each_subcat['entity_id']); ?>
<?php endforeach; ?>

Resources