Get magento subtotal from cart - magento

I'm currently using this snippet to show the cart totals in the topcart of my Magento shop. My problem is that it's not always updating when products is put in cart, it's just showing 0$, especially configurable products. But when a second product is put in the cart, it's working again.
Am I missing something, should there be a "check" of some kind before this piece of code?
<?php echo Mage::helper('checkout')->formatPrice($this->getSubtotal()) ?>

You can also try following code it works for me
<?php echo Mage::helper('checkout/cart')->getQuote()->getSubtotal() ?>

Make sure your top cart block is extending a relevant block type such as Mage_Checkout_Block_Cart_Sidebar. If you do, you will have access to useful functionality that will save you rewriting unnecessary code.
For example, if you extend Mage_Checkout_Block_Cart_Sidebar - you can call getSubtotal()
An alternative would be to use the following:
Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();

you can use this code:
$subtotals= Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();
echo $formattedPrice = Mage::helper('core')->currency($subtotals , true, false);

None of the above worked for me but I was able to get the subtotal using this:
$orderObj = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$orderSubTotal = $orderObj->getSubtotal();
echo $orderSubTotal;
This refers to the success.phtml page.

Related

Magento 2: Get Simple Product's Price?

I've been building a Magento 2 template however I've hit a roadblock with the way I'm pulling the prices. The prices are being pulled correctly for simple products by using the following (simplified as I'm exploding the variable to split the string):
$price = $product->getPrice();
<p><?php echo $price; ?></p>
Due to Magento 2 changing the way it processes the prices of configurable products, the price is being outputted as 0.00 for configurables and not pulling the price of the simple products that are attached to it. This was to be expected because I'm not telling it to pull the price of the simple products.
What's the best way for me to get the prices of the simple products? There's a size dropdown on the configurable so ideally, the price would change depending on which product you click on in the dropdown.
Due to me having to explode the price string, I can't just call the block in the XML file either unless I write an overkill jQuery script to split the string on the browser...
Thanks!
Try this code it will help you.
if($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE){
$product->getFinalPrice();
}
else
{
echo $product->getPrice();
}
in your block phtml file you can just use
<?php
$_product = $this->getProduct();
echo $_product->getFinalPrice();
?>
it should show you final price,it work with simple and bundle products

How to filter product based on specific arrribute in magento and call via cms page as well from controller

I need some tips to use product list functionality in my cms pages.
like in my cms page named "manufacture = apple" should show all products of apple manufacture .
same for other attribute like color, size etc .....
Thanks.
You need to create small module. I can't share complete code here, but sharing you a helpful link which I have earlier used and working fine.
Link:https://www.atwix.com/magento/products-list-cms/
Hope it will helpful!
create a template (test.phtml) under yourtheme/catalog/product and add the following code to your test.phtml
<?php
$arrParams = array_slice($this->getRequest()->getParams(),1);
$attribute = each($arrParams);
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter($attribute[0],$attribute[1]);
echo '<pre>';
print_r($collection->getData());
exit;
?>
than call this template to your cms page. Add the code below in content area of your cms page.
{{block type="catalog/product" template="catalog/product/test.phtml"}}
once you have the array of product than display it as per your requirement.

product prices not updating in configurable products

I am looking at creating a configurable product that has various prices.
Having looked at this, it seems that when you select an option, that has another price, the price field of the product options section does not update.
I have provided an image below:
Image
You can see that I have selected a product option, Oxygen, which is £273. I was expecting the product price of the option to update to match this, but it doesn't.
Under the Associated Products section, I have added a fixed price for the associated products, but this still does not update the price.
I cannot believe that this isn't available out-of-the-box with Magento.
Has anyone ever noticed this before?
I have found This link
Which seems to suggest that it has been noticed before.
Does Simple Configurable Products fix this problem?
Many thanks
SCP will solve your problem - it takes the price from the child product. Unfortunately, this will not work too well if you are also using Custom Product Options with price differentials.
Depending on the complexity of your products you may want to go with normal Magento and a script to work out what the price variants are for the super attribute options. The array for the super attribute price options can be iterated over, master and child products checked against the attribute(s) that change, e.g. colours, and a new attribute array written out. It is a bit of code you will have to write yourself, but here is an article that covers the basics:
http://www.ayasoftware.com/content/magento-update-fly-super-product-attributes-configuration
worse, scp does not allow the customer to edit the choice. My Client insisted he could edit his choice so we had to develop for this using the JSON encoded script on the product view page instead.
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<?php $jason = $this->getJsonConfig(); ?>
<?php $uJason = json_decode($jason); ?>
<?php
if ($_product->getMsrp() > 0) {
$uJason->productMsrp = sprintf("%01.2f", $_product->getMsrp());
}
$jason = json_encode($uJason);
?>
<script type="text/javascript">
var optionsPrice = new Product.OptionsPrice(<?php echo $jason ?>);
</script>
I think we had to change the code elsewhere but the above change allowed the msrp to update as well as the price.

Cant retrieve discounted product price in custom script

I have a custom script that outputs a list of particular products in csv format. The frontend of the store just runs fine, however when retrieving the price of a product in my script, the actual FinalPrice does not take my catalog price rules into account, which is kind of messed up, since the getFinalPrice() method works perfectly in template files ect..
This is my code, which I have drastically shortened for demonstration purposes:
<?php
require 'app/Mage.php';
Mage::app('default');
$product = Mage::getModel("catalog/product")->load(27809);
echo $product->getFinalPrice();
?>
This outputs the product's regular price, but not the price accounted for the catalog price rule. I have just applied all catalog rules again and have also rebuilt all indexes. As I said, the discount prices show fine in the frontend, but for some reason I am not able to retrieve them in my script.
I hope someone has an idea what could be going wrong here. Thanks in advance!
Product final price is calculated in an observer, and your script is not loading the events configuration.
See my addition below.
<?php
require 'app/Mage.php';
Mage::app('default');
//load event configuration areas
Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_FRONTEND, Mage_Core_Model_App_Area::PART_EVENTS);
$product = Mage::getModel("catalog/product")->load(27809);
echo $product->getFinalPrice();
?>
See Mage_CatalogRule_Model_Observer::processFrontFinalPrice();.

Listing Categories in Magento

I'm having a problem and getting an understanding of this will help me figure out Magento a bit more. I have a CMS page using 1column.phtml. I actually creating a vertical navigation system using 1column.phtml itself, not the CMS. The CMS portion of things is just being used to show some images. The design of the site dictates the layout which is why I am using a 1 column and putting the sidebar in there and not using the left.phtml file. Anyway, that's not the problem. I am trying to pull the main categories and don't quite get how to do that. I'd rather just put the code in the phtml file and not mess with the XML, but when I add the code I'm not getting anything back.
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php echo $this->drawItem($_category) ?>
<?php endforeach ?>
So I get nothing back. I also don't understand scope of these, so once I get this working, how would I pull only top level categories? Thanks!
I would try to get a collection of categories and see where that takes you.
$categories = Mage::getModel('catalog/category')
->getCollection();
This code will give you every single category. You can use addAttributeToSelect and addAttributeToFilter functions to furter narrow down your categories.
From there you can foreach through the categories to display them or what whatever you want to do with it.

Resources