1

I'm adding a best selling products block to my sidebar, but I have encountered a problem. I simply cannot get the price of the product.

Here is my template;

<?php

$totalPerPage = ($this->show_total) ? $this->show_total : 6;
$counter = 1;
$visibility = array(
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                  );

$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
                              ->addAttributeToSelect('*')
                              ->addOrderedQty()
                              ->addAttributeToFilter('visibility', $visibility)
                              ->setOrder('ordered_qty', 'desc');
?>

<div class="content-box-header">Bestsellers</div>
<div class="content-box">
<?php foreach($_productCollection as $product): ?>


<?php if($counter <= $totalPerPage): ?>

<?php $productUrl =  $product->getProductUrl() ?>
  <a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
    <h4><?php echo $product->name ?></h4>
  </a>

  <a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
  <img src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(120); ?>" alt="Product image"  />
  </a> <br />
  <p><?php echo $product->short_description ?></p>


<?php endif; $counter++; ?>
<?php endforeach; ?>
</div>

On the 4th line from the bottom I want to display the price instead of the short describtion. I've tried replacing it with;

$_product->getPrice()

But that made the sidebar crash. What should I insert instead?

0

I ran this code of yours at my root like this, and it worked for me, only issue that came up was in the image so I replaced your image code with mine:

<?php

$magePath = 'app/Mage.php'; 

require_once $magePath;

Varien_Profiler::enable(); 

Mage::setIsDeveloperMode(true); 

ini_set('display_errors', 1);

umask(0);
$totalPerPage = 6;
$counter = 1;
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$visibility = array(
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                  );
$_productCollection = Mage::getResourceModel('reports/product_collection')
                              ->addAttributeToSelect('*')
                              ->addOrderedQty()
                              ->addAttributeToFilter('visibility', $visibility)
                              ->setOrder('ordered_qty', 'desc');
                              ?>
<?php foreach($_productCollection as $product): ?>


<?php if($counter <= $totalPerPage): ?>

<?php $productUrl =  $product->getProductUrl() ?>
  <a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
    <h4><?php echo $product->name ?></h4>
  </a>

  <a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
  <?php $product_image_url = $product->getImageUrl(); ?>
  <img src="<?php echo $product_image_url; ?>" alt="Product image"  />
  </a> <br />
  <p><?php echo $product->short_description ?></p>
<?php echo $product->getPrice(); ?>

<?php endif; $counter++; ?>
<?php endforeach; ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.