Magento: How to get the lowest price (i.e. Grouped products)?

I am trying to display the grouped price of a product on the product preview page in Magento 1.7.0.2, exactly as it appears in the list of product categories ("Starting at: xx.xx"). I thought I could just use

$this->getPriceHtml($_product, true);

because it works the same in the product list of a category, but like everything I tried to do in Magento, it’s not so simple because this method does not return anything on the product view page.

I went a little deeper into Magento code and realized that the cause of this problem is the minimum product price data that is not set on the product view page ($ _product-> getMinimalPrice () returns null).

So I did some research on how to download a minimum product price that triggered ideas like

$_product->getPriceModel()->getMinimalPrice($_product);

which does not work because obviously this method was deprecated and removed in one of the latest updates or

$priceModel = Mage::getResourceModel('catalogindex/price');
$priceModel->setStoreId(Mage::app()->getStore()->getId());
$priceModel->setCustomerGroupId(Mage::getSingleton('customer/session')->getCustomerGroupId());

$minimalPrices = $priceModel->getMinimalPrices(array($_product->getId()));
$minimalPrice = $minimalPrices[0];
$_product->setData('minimal_price', $minimalPrice['value']);
$_product->setData('minimal_tax_class_id', $minimalPrice['tax_class_id']);

which does not work because the table 'catalogindex_minimal_price' is empty.

So my question is: how does Magento upload the lowest price to the product list of a category?

+5
source share
4 answers

Magento , Magento . Mage_Catalog_Model_Layer, , SQL, , .

SQL-, CatalogIndex, , , , .

$data_grouped = Mage::getModel("catalogindex/data_grouped");
$minimal = $data_grouped->getMinimalPrice(array($_product->getId()), Mage::app()->getStore());

, :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_index_price.value' in 'field list'

CatalogIndex , Im , .

price.phtml, , : :

$this->setProduct(
    Mage::getModel("catalog/product")->getCollection()
        ->addAttributeToSelect(Mage::getSingleton("catalog/config")->getProductAttributes())
        ->addAttributeToFilter("entity_id", $this->getProduct()->getId())
        ->setPage(1, 1)
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->load()
        ->getFirstItem()
);

, , , .

+9

, HTML Suburf. , . . , , . list.phtml content.phtml. getMinimalPrice() NULL, , getPriceHtml(), price.phtml .

content.phtml. , , indexController.php :

$block = $this->getLayout()->createBlock(
    'Mage_Core_Block_Template',
    'b2b',
    array('template' => 'b2b/content.phtml')
);

$block = $this->getLayout()->createBlock(
    'Mage_Catalog_Block_Product_List',
    'b2b',
    array('template' => 'b2b/content.phtml')
);

, .


, , content.phtml, , Suburf foreach(), .

    $_productCollection = Mage::getModel('catalog/product')
        ->getCollection()
//          ->addAttributeToSelect('*')
// doesn't work     ->addAttributeToSelect('special_price')
        ->addAttributeToSelect('sku')
        ->addAttributeToFilter('status', 1)
        ->addAttributeToFilter('visibility', 4)
        // http://stackoverflow.com/questions/1332742/magento-retrieve-products-with-a-specific-attribute-value
        ->addFieldToFilter( array(
                array('attribute'=>'manufacturer','eq'=>'143')
                , array('attribute'=>'manufacturer','eq'=>'139')
            ))
        ;

echo "<p>There are ".count($_productCollection)." products: </p>";
echo '<div class="category-products">';
// List mode 
if($this->getMode()!='grid') { 
    $_iterator = 0;
    echo'<ol class="products-list" id="products-list">';
    foreach ($_productCollection as $_product) {
        ?>          <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
        <?php 
        $_product=Mage::getModel("catalog/product")->getCollection()
            ->addAttributeToSelect(Mage::getSingleton("catalog/config")->getProductAttributes())
            ->addAttributeToFilter("entity_id", $_product->getId())
            ->setPage(1, 1)
            ->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->load()
            ->getFirstItem()
            );
        echo "Minimal price: ".$_product->getMinimalPrice()."<br>\n";       

, , $this->setProduct() . , , , , $_product=$this->getProduct() price.phtml, . , Mage:: setProduct() $_product.

, Subsurf!!!

+2

There is another way that you can use $this->getPriceHtml($_product, true);in your template, which then processes all the rules for displaying complex prices and puts “Starting from” before grouped product prices.

I use this for a custom product. In the block class, I have this function:

class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract {
  public function setGroupedProductPrice($group_product) {
   $prices = array();
   foreach ($group_product->getTypeInstance()->getChildrenIds($group_product->getId()) as $ids) {
     foreach ($ids as $id) {
       $product = Mage::getModel('catalog/product')->load($id);
       $prices[] = $product->getPriceModel()->getPrice($product);
     }
   }
   ksort($prices);
   $group_product->setPrice(array_pop($prices));
  }
}

Then in the template:

<?php 
  if ($_product->getTypeId() == 'grouped')
    $this->prepareGroupedProduct($_product);
  echo $this->getPriceHtml($_product, true);
?>
0
source

The following is a trick without using a product collection:

$priceResource = Mage::getResourceSingleton('catalogindex/price');

$select = $priceResource->getReadConnection()->select();
$select->from(
    array('price_table' => $priceResource->getMainTable())
)
    ->where('price_table.entity_id = ?', $_product->getId())
    ->where('price_table.website_id = ?', Mage::app()->getStore()->getWebsiteId())
    ->where('price_table.customer_group_id = ?', Mage::getSingleton('customer/session')->getCustomerGroupId());

$result = $priceResource->getReadConnection()->fetchRow($select);

This code is adapted from \Mage_CatalogIndex_Model_Resource_Price::getMinimalPricesand is suitable for a single product.

0
source

All Articles