Not sure if this is a simple setting or something I am missing but it is really bugging me. Say I have a configurable product that has different prices for each variation of £10, £20 and £30. When looking in the category view it just shows £10 next to the product. Rather than "from £10". Is there a way of adding that for products that have more than one price? Thanks.
Magento – Magento2: Showing price “from” on configurable products
categorymagento2templatetheme
Related Solutions
Painfully debugged Magento core code (log all queries and reviewed step-by-step), the issue is in:
vendor/magento/module-configurable-product/Model/ResourceModel/Product/Indexer/Price/Configurable.php
line 192
$priceColumn = $this->_addAttributeToSelect($select, 'price', 'l.product_id', 0, null, true);
The fourth parameter to Magento\Catalog\Api\Data\ProductInterface\AbstractIndexer::_addAttributeToSelect()
is supposed to the the store_id for the (non-default) price to join from the attribute "decimal" value table, instead Magento passes the hard-coded "0", which is causing default store (website) prices to be put in product price index for configurable products, for any website_id.
Quick&Dirty Solution:
Replace in vendor/magento/module-configurable-product/Model/ResourceModel/Product/Indexer/Price/Configurable.php
line 192
:
$priceColumn = $this->_addAttributeToSelect($select, 'price', 'l.product_id', 0, null, true);
with:
$select->join(
['sg' => $this->getTable('store_group')],
'sg.website_id = i.website_id',
[]
);
$priceColumn = $this->_addAttributeToSelect($select, 'price', 'l.product_id', 'sg.default_store_id', null, true);
This way, Magento will select the price for non-default websites from the correct associated value for the default store in those websites, instead of statically assigning the default website price to each result.
Don't forget to reindex:
php bin/magento indexer:reindex catalog_product_price
Proper solution:
Create a custom module and overload the
Magento\ConfigurableProduct\Model\ResourceModel\Product\Indexer\Price\Configurable
class with your own modified version (see Quick&Dirty Solution) using DI preference ({your module}/etc/di.xml).
Don't forget to specify that your module depends on Magento_ConfigurableProduct module in your etc/module.xml and composer.json files.
I was getting same issue when Migrated data from Magento to Magento 2.As Magento 2 change scenario to set the Special price for configurable product and we need to require set special price for every associated product of configurable product.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
use Magento\Framework\App\Bootstrap;
try{
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$obj->get('Magento\Framework\Registry')->register('isSecureArea', true);
$appState = $obj->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$products = $obj->create('\Magento\Catalog\Model\Product')->getCollection();
$products->addAttributeToSelect(array('special_price','price'))
->addFieldTofilter('type_id','configurable')
->addFieldToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->addFieldToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->load();
echo "Total products count: " . $products->count() . "<hr>";
echo "<pre>";
if ($products->getSize() > 0) {
foreach ($products as $product) {
$configSpecial = $product->getSpecialPrice();
if($configSpecial > 0)
{
echo 'product id<br/>'.$product_id = $product->getId();
$configProduct = $obj->create('Magento\Catalog\Model\Product')->load($product_id);
$_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);
foreach ($_children as $child){
$childProductId = $child->getID();
$childproduct = $obj->create('Magento\Catalog\Model\Product')->load($childProductId);
$childproduct->setStoreId(9);
//$childproduct->getName();
$childproduct->setSpecialPrice($configSpecial);
$childproduct->save();
//echo $childproduct->getSpecialPrice();
}
//echo "count: ".count($_children);
}
//print_r($product->getData());
}
}
} catch (Exception $e) {
echo $e->getMessage();
}
I hope this script will help you.
Best Answer
I asked a similar question yesterday and it got closed (for some reason). So I saw your question as related and decided to answer it.
It's not a complete solution yet, but this will help you add "Price from:" next to configurable products. Further logic can and should be added so the text only shows if there are simple products with different prices associated to the configurable.
This is what I did (Magento 2.2):
Copy
to
And add
'display_label' => __('Price from:'),
to this block od code:You can check my complete solution here: Add "Price from" to configurable products which contain different price simple products - Magento 2