2017-11-15 2 views
1

im tring, um die Top-Produkte, die bestellt werden und die Menge, aber ich merke, dass gibt mir die verkauften Produkte und nicht die bestellten Produkte, die mehr sein sollten, was ist falsch mit diesem Code? Warum zeigt nicht die richtigen Ergebnisse?Holen Sie sich die Liste der bestbestellten Produkte

set_time_limit(0); 
ini_set('memory_limit', '8000M'); 

require '../app/Mage.php'; 
umask(0); 
Mage::app(); 
Mage::register('isSecureArea', 1); 
function getBestsellingProducts() 
{  
    // number of products to display 
    $productCount = 400; 

    // store ID 
    $storeId = Mage::app()->getStore()->getId(); 

    // get today and last 30 days time 
    $today = time(); 
    $last = $today - (60*60*24*30); 

    $from = date("Y-m-d", $last); 
    $to = date("Y-m-d", $today); 

    // get most view ed products for current category 
    $products = Mage::getResourceModel('reports/product_collection') 
        ->addAttributeToSelect('*')   
        ->addOrderedQty($from, $to) 
        //->setStoreId($storeId) 
        //->addStoreFilter($storeId)      
        ->setOrder('ordered_qty', 'desc') 
        ->setPageSize($productCount); 

    Mage::getSingleton('catalog/product_status') 
      ->addVisibleFilterToCollection($products); 
    Mage::getSingleton('catalog/product_visibility') 
      ->addVisibleInCatalogFilterToCollection($products); 

    return $products; 
} 


$products = getBestsellingProducts(); 

foreach ($products as $key => $value) { 

    $pro = Mage::getModel('catalog/product')->load($value->getId()); 
    echo $pro->getName() ." - ". $pro->getSku() . " - " . $value->ordered_qty . "<br>"; 
} 

Antwort

0

Führen Sie die folgenden Schritte, um Top-Produkte bestellt:

Schritt 1: Erstellen Sie eine Datei app/code/local/Mage/Catalog/Block/Product/Bestseller.php und die folgenden Zeilen Code in es

lass Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract{ 
    public function __construct(){ 
     parent::__construct(); 
     $storeId = Mage::app()->getStore()->getId(); 
     $products = Mage::getResourceModel('reports/product_collection') 
      ->addOrderedQty() 
      ->addAttributeToSelect('*') 
      ->addAttributeToSelect(array('name', 'price', 'small_image')) 
      ->setStoreId($storeId) 
      ->addStoreFilter($storeId) 
      ->setOrder('ordered_qty', 'desc'); // most best sellers on top 
     Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products); 
     Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products); 

     $products->setPageSize(3)->setCurPage(1); 
     $this->setProductCollection($products); 
    } 
} 

Schritt 2: Erstellen Sie eine Datei app/design/frontend/default/YourTheme/template/catalog/product/bestseller.phtml Datei und fügen Sie die folgenden Codezeilen darin ein

<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?> 
<div class="page-title"> 
    <h2><?php echo $this->__('Best Seller Products') ?></h2> 
</div> 
<?php $_collectionSize = count($_products->getItems()) ?> 
<table class="products-grid" id="products-grid-table"> 
<?php $i=1; foreach ($_products->getItems() as $_product): ?> 
    <?php if ($i%1!==0): ?> 
    <tr> 
    <?php endif ?> 
     <td id="td_<?php echo $i;?>" <?php if($i%3==0 or $i==$_collectionSize){echo 'class="last"';} ?> > 
     <?php contentBlock('top') ?> 
     <div id="cont_<?php echo $i;?>"> 
      <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3> 
      <a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>"> 
       <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(122, 109); ?>" width="122" height="109" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /> 
      </a> 
      <div class="a-center">       
       <?php if($_product->getRatingSummary()): ?> 
        <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?> 
       <?php endif; ?> 
       <?php echo $this->getPriceHtml($_product, true) ?> 
       <?php if($_product->isSaleable()): ?> 
        <button class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button> 
        <div class="clear"></div> 
       <?php else: ?> 
        <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span></p> 
        <div class="clear"></div> 
       <?php endif; ?> 
       <ul class="add-to-links"> 
        <?php if ($this->helper('wishlist')->isAllow()) : ?> 
         <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li> 
        <?php endif; ?> 
        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?> 
         <li class="last"><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li> 
        <?php endif; ?> 
       </ul> 
       <?php if($_product->getevent_date()) {echo $_product->getevent_date();} ?> 
      </div> 
     </div> 
    </td> 
    <?php if ($i%3==0 or $i==$_collectionSize): ?> 
    </tr> 
    <?php endif ?> 
    <?php $i++; endforeach; $kol = $_collectionSize; ?> 
</table> 
<?php endif; ?> 

St ep 3: Diese oben genannten Dateien erstellen eine Liste der meistverkauften Produkte, die überall in Ihrem Magento Store gezeigt werden können. Sie müssen lediglich die folgende Zeile des Codeblocks in Ihre Vorlage einfügen, um die meistverkauften Produkte anzuzeigen.

Jetzt können wir die meistverkauften Produkte überall in unserem Magento Shop zeigen.

Verwandte Themen