2014-05-20 2 views
0

J'essaie de créer mes pages de destination pour mes produits sur mon site Web, mais je souhaite qu'il affiche des produits aléatoires en haut de la page (dans la zone de description). J'ai tout le code pour obtenir les produits aléatoires dans random.phtml et il fonctionne parce que je l'ai testé sur une page CMS « Test » avec le code:Comment afficher des produits aléatoires dans la "description" des catégories

{{block type="midmedssettings/product_list" num_products="5" category_id="145" template="catalog/product/random.phtml"}} 

Cependant quand je mets cela dans la zone Description en Dans ma catégorie, il ne reconnaît pas le code et affiche uniquement le code. J'ai essayé

<block type="midmedssettings/product_list" num_products="5" category_id="145" template="catalog/product/random.phtml"> 

mais cela n'affichait rien.

random.phtml

<?php 
/** 
* Magento 
* 
* NOTICE OF LICENSE 
* 
* This source file is subject to the Open Software License (OSL 3.0) 
* that is bundled with this package in the file LICENSE.txt. 
* It is also available through the world-wide-web at this URL: 
* http://opensource.org/licenses/osl-3.0.php 
* If you did not receive a copy of the license and are unable to 
* obtain it through the world-wide-web, please send an email 
* to [email protected] so we can send you a copy immediately. 
* 
* @category design_default 
* @package Mage 
* @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 
*/ 
?> 
<?php 
/** 
* Product list template 
* 
* @see Mage_Catalog_Block_Product_List 
*/ 
?> 

<?php 
Mage::getSingleton('catalog/layer')->setData("product_collection",NULL); 
$this->_productCollection = null; 
$_productCollection=$this->getLoadedProductCollection(); 
?> 

<?php $_productCollection=$this->getLoadedProductCollection() ?> 
<?php if(!$_productCollection->count()): ?> 
<div class="padder"> 
<div class="note-msg"> 
    <?php echo $this->__('There are no products matching the selection.') ?> 
</div> 
</div> 
<?php else: ?> 

<?php // Grid Mode ?> 
<div class="listing-type-grid catalog-listing padder"> <!-- the class name will change to .listing-type-cell if viewing in list mode --> 
<?php $_collectionSize = $_productCollection->count() ?> 

    <?php $_items = $_productCollection->getItems(); 
      shuffle($_items); ?> 

    <table cellspacing="0" class="generic-product-grid" id="product-list-table"> 
    <?php $i=0; foreach ($_items as $_product): ?> 
    <?php if ($i++%4==0): ?> 
    <tr> 
    <?php endif ?> 
     <td> 
      <p class="product-image"> 
       <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"> 
        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170, 170); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/> 
       </a> 
      </p> 
      <h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h5> 
      <?php if($_product->getRatingSummary()): ?> 
       <!--?php echo $this->getReviewsSummaryHtml($_product, 'short') ?--> 
       <?php echo $this->getReviewsSummaryHtml($_product) ?> 
      <?php endif; ?> 

      <?php echo $this->getPriceHtml($_product, true) ?> 

      <div class="clear"></div> 
      <ul class="add-to"> 

       <?php if($_product->isSaleable()): ?> 
       <li class="add-to-cart-textlink"><a href="#" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span>Add to Cart</span></a></li> 
       <?php else: ?> 
       <li><?php echo $this->__('Out of stock') ?></li> 
       <?php endif; ?> 

       <?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><a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li> 
       <?php endif; ?> 
      </ul> 
     </td> 
    <?php if ($i%4==0 && $i!=$_collectionSize): ?> 
    </tr> 
    <?php endif ?> 

    <?php if ($i==4) break; // show 4 products max ?> 

    <?php endforeach ?> 
    <?php for($i;$i%4!=0;$i++): ?> 
      <td class="empty-product">&nbsp;</td> 
    <?php endfor ?> 
    <?php if ($i%4==0): ?> 
    </tr> 
    <?php endif ?> 
    </table> 
    <script type="text/javascript">decorateTable('product-list-table')</script> 
</div> 

<?php endif; ?> 

Quelqu'un pourrait-il s'il vous plaît me dire où je me trompe.

Merci d'avance

+0

il ny a pas dans un fichier, il est dans le back-end de magento –

+0

Vous ne pouvez pas ajouter un bloc dans la description de la catégorie. Au lieu de cela, vous pouvez affecter un bloc statique CMS à votre catégorie et ce bloc affichera vos produits aléatoires – Slimshadddyyy

+0

cela signifie-t-il que je vais devoir créer un bloc statique pour chaque catégorie? –

Répondre

1

Pour un produit aléatoire s'il vous plaît suit ces étapes:

goto app/code/core/Mage/Catalog/Block/Product/List/ here you will create a Random.php file and add below code in this file. 

<?php 
    class Mage_Catalog_Block_Product_List_Random extends Mage_Catalog_Block_Product_List 
      { 
       protected function _getProductCollection() 
       { 
        if (is_null($this->_productCollection)) { 
         $categoryID = $this->getCategoryId(); 
         if($categoryID) 
         { 
          $category = new Mage_Catalog_Model_Category(); 
          $category->load($categoryID); // this is category id 
          $collection = $category->getProductCollection(); 
         } else 
         { 
          $collection = Mage::getResourceModel('catalog/product_collection'); 
         } 
         Mage::getModel('catalog/layer')->prepareProductCollection($collection); 
         $collection->getSelect()->order('rand()'); 
         $collection->addStoreFilter(); 
         $numProducts = $this->getNumProducts() ? $this->getNumProducts() : 3; 
         $collection->setPage(1, $numProducts)->load(); 

         $this->_productCollection = $collection; 
        } 
        return $this->_productCollection; 
       } 
      } 


    now create static block insert that code or get static block on file for view product. 
{{block type="catalog/product_list_random" category_id="YOUR_CATEGORY_ID" template="catalog/product/list.phtml" column_count="4" num_products="12"}} 
+0

sur le code que je ajoute au bloc statique dois-je remplacer "your_category_id "avec mon id de chat ou juste le laisser comment c'est? –

+0

oui remplacer avec votre identifiant de catégorie. –

+0

c'est le problème que je ne veux pas avoir à écrire dans l'ID car je veux faire un bloc qui fonctionnera sur chaque page, où comme si je devais écrire un ID de catégorie, je devrais faire un nouveau bloc pour chaque catégorie. –

0

Essayez à partir de la vue.

  1. Modifier le modèle/catalogue/catégorie/view.phtml
  2. Placez votre bloc d'appel du code à la ligne préférée. code d'appel noir ressemble ci-dessous:

$this->getLayout() ->createBlock("midmedssettings/product_list") ->setTemplate("catalog/product/random.phtml") ->toHtml();

0

1) Créer un bloc statique CMS de votre admin fin avec identifer random-products et contenu

{{block type="catalog/product_list" template="catalog/product/random.phtml"}} 

2) Dans votre modèle /catalogue/category/view.phtml, selon l'endroit où vous voulez afficher les produits, collez le code ci-dessous

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('random-products')->toHtml();?> 

3) Dans votre catalogue /produit/random.phtml, collez le code ci-dessous,

<?php 


$categoryId = Mage::registry('current_category')->getEntityId(); 
$_helper = $this->helper('catalog/output'); 
$_category = Mage::getModel('catalog/category')->load($categoryId); 
$_productCollection = Mage::getResourceModel('reports/product_collection') 
         ->addAttributeToSelect('*') 
         ->addCategoryFilter($_category) 
         ->setVisibility(array(2,3,4)); 
$_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));     
$_productCollection->setPage(1, 5); 

?> 


<?php foreach ($_productCollection as $_product): ?> 

<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"> 
      <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a> 


      <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?> 
        <h3><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h3> 

<?php endforeach; ?> 

Vous pouvez modifier votre conception pour random.phtml au besoin et à chaque rechargement de la page, il sera afficher 5 random produits assignés à votre catégorie.

+0

dois-je remplacer tout mon code dans random.phtml par le précédent ou simplement l'ajouter? –

+0

@AdamAllen remplacer avec cela et modifier selon votre besoin .. – Slimshadddyyy

+0

ok je vais essayer et vous faire savoir comment je vais, merci –

Questions connexes