Magento 2: Get Simple Product's Price? - magento

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

Related

Static Block for specific products in magento

Is there any way to do this :
I have two kinds of products, Handmade and ready women accessories. for handmade products I want a block under the price that tell the customer "This Product is 100% Handmade" and for the ready product nothing. So it will be only for the Handmade.
I already have a block but it is visible to all products, so I want it to be visible only for Handmade products.
Any Ideas ?
Can you not create an attribute specifically for the Handmade products? e.g the attribute could be called 'IsHandmade'.
Then on your product page (view.phtml) under the price have something like this?:
<?php if ($product->getIsHandMade == 'Yes'):
echo 'This Product is 100% Handmade';
endif; ?>

Add tier price to upsell block

I searched high and low on google for this answer. I need to display the tier prices for the products that show on the upsell block in the product page.
I tried this:
<?php echo $this->getTierPriceHtml() ?>
However, it echos the tier price for the main product on the page instead of the tier prices for the products in the upsell block.
Does anyone know a work around here?
You're almost done
$this->getTierPriceHtml($_link);
Indeed, this method accept a parameter for the $product to show, if you don't set it it will show the price for Mage::registry('current_product') aka the main product, just pass the upsold product as argument and you're ok.

Get magento subtotal from cart

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.

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();.

Resources