2012-09-05 29 views
12

belirli kategoriden gösteri ürünler Ben altbilgi için bloğun bir "Ay Ürünü" inşa ediyorum. Bir kategorinin ürünlerini yüklemeli ve ilkini göstermelidir. CMS Page bloğu gömerken catalog/product/list.phtmlMagento: altbilgi


ÇALIŞMA bunu:

<?php $_productCollection = $this->getLoadedProductCollection() ?> 

<div class="featured-product"> 
    <h2><?php echo $this->__('Product of the Month') ?></h2> 

    <?php foreach ($_productCollection as $_product): ?> 
     <div class="item"> 
      <a class="product-image" href="<?php echo $_product->getProductUrl() ?>"> 
       <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /> 
      </a> 

      <a class="product-name" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a> 

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

     <?php 
     // Note: Exit after first product. 
     break; 
     ?> 
    <?php endforeach ?> 
</div> 

O Magento'nın ürün listesi şablonunun sadece basitleştirilmiş versiyonu:

Bu benim şablon dosyası custom/featured-product.phtml olduğunu iyi çalışıyor. Örnek: local.xml aracılığıyla bloğu gömerken

{{block type="catalog/product_list" category_id="13" template="custom/featured-product.phtml" }} 


ÇALIŞMA DEĞİL

, başarısız olur. Doğru işaretleme döndürülür, ancak belirtilen kategori yüklenmez. Bunun yerine bir ürün grubunun rastgele (Onların seçilirseniz nasıl yok) yüklenir. local.xml yılında

Kodum: Bütünlüğü için

<default> 
    <reference name="footer"> 
     <block type="catalog/product_list" name="custom.featuredProduct" as="product_of_the_month" category_id="13" template="custom/featured-product.phtml" /> 
    </reference> 
</default> 

, ben şöyle page/html/footer.phtml açıkça bloğu çevirdim:

<?php echo $this->getChildHtml('product_of_the_month') ?> 


bir fikir?

En iyi tahminle benim local.xml yanlış olmasıdır. Yüklemem gereken bir ebeveyn bloğu var mı?


[Güncellemeler]

Benim orijinal kod ürün sayfası çöker. Geçici çözüm Magento çekirdek dosya üzerinde bu kadar ağır kod dayandırarak değil geçerli: catalog/product/list.phtml.

<?php $_productCollection = $this->getLoadedProductCollection() ?> 


[Çözüm]

CMS Sayfalar ve layoutXML kullanılmak üzere örneklerle bir çalışma versiyonunu buraya dahil edilmiştir: https://stackoverflow.com/a/12288000/1497746

+0

'local.xml' - Bu dosyasının bulunduğu? (tam yol) – FlorinelChis

+0

@FlorinelChis - Bu tema yedek hiyerarşisinde bir sorun değil. Özel bir paket için normal yer: /app/design/frontend/custom/custom/layout/local.xml –

cevap

12

Alan Storm'un önerisini kullanarak bir çalışma çözümü bulundu.

/template/custom/featured-product.phtml Kısacası

<?php 
$_categoryId = $this->getCategoryId(); 

$_productCollection = Mage::getModel('catalog/category')->load($_categoryId) 
    ->getProductCollection() 
    ->addAttributeToSelect('*') 
    ->addAttributeToFilter('status', 1) 
    ->addAttributeToFilter('visibility', 4) 
    ->setOrder('price', 'ASC'); 
?> 

<div class="featured-product"> 
    <h2><?php echo $this->__($this->getLabel()); ?></h2> 

    <?php foreach ($_productCollection as $_product): ?> 
     <div class="item"> 
      <a class="product-image" href="<?php echo $_product->getProductUrl() ?>"> 
       <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /> 
      </a> 

      <a class="product-name" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a> 

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

     <?php 
     // Note: Exit after first product. 
     break; 
     ?> 
    <?php endforeach ?> 
</div> 

, koleksiyon elle bir koleksiyon alma yerine (benim ilk girişim olarak yaptığımız) oluşturulur:

<?php $_productCollection = $this->getLoadedProductCollection() ?> 
<?php $_collectionSize = $_productCollection->count(); ?> 


CMS Page Kullanımı:

{{block type="core/template" category_id="13" label="Product of the Month" template="custom/featured-product.phtml" }} 


Şablonda kullanma:

/layout/local.xml

<default> 
    <reference name="footer"> 
     <block type="core/template" name="custom.featuredProduct" as="featured_product" template="custom/featured-product.phtml"> 
      <action method="setData"><key>category_id</key><value>13</value></action> 
      <action method="setData"><key>label</key><value>Product of the Month</value></action> 
     </block> 
    </reference> 
</default> 

/template/page/html/footer.phtml

<?php echo $this->getChildHtml('featured_product') ?> 


yararlı kaynaklar:

Nasıl bir ürün koleksiyonu almak için:

kullanma sihirli getters/belirleyiciler:

+0

' getPriceHtml ($ _ product, true)? –

+0

Fiyatın oluşturulması için bir çözüm buldunuz mu? @ brendan-falkowski – Kenny

+0

@Kenny - Hayır, bu işi nasıl yapacağınızdan emin değilim. –

5

Öncelikle yaşadım rastgele sorunlar Özellikle bu hattı kaçınarak bu nedenle bugibi bir şey çalışıyorum, type veya class, name, as, template dışındaki bloklar (ilgili değerleri ayarlamak için düzen güncelleme xml öznitelik düğümleri kullanarak yılda

<default> 
    <reference name="footer"> 
     <block type="catalog/product_list" name="custom.featuredProduct" as="product_of_the_month" template="custom/featured-product.phtml"> 
      <action method="setData"><key>category_id</key><value>13</value></action> 
     </block> 
    </reference> 
</default> 
<default> 
    <reference name="footer"> 
     <block type="catalog/product_list" name="custom.featuredProduct" as="product_of_the_month" template="custom/featured-product.phtml"> 
      <action method="setCategoryId"><id>13</id></action> 
     </block> 
    </reference> 
</default> 

veya bu yardımcı olabilir ve benim ilk adım olacaktır.

Ondan sonra, koleksiyon

#File: app/code/core/Mage/Catalog/Block/Product/List.php 
class Mage_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_Abstract 
{ 
    ... 
    public function getLoadedProductCollection() 
    { 
     return $this->_getProductCollection(); 
    }   
    ... 
    protected function _getProductCollection() 
    { 
     if (is_null($this->_productCollection)) { 
      $layer = $this->getLayer(); 
      /* @var $layer Mage_Catalog_Model_Layer */ 
      if ($this->getShowRootCategory()) { 
       $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId()); 
      } 

      // if this is a product view page 
      if (Mage::registry('product')) { 
       // get collection of categories this product is associated with 
       $categories = Mage::registry('product')->getCategoryCollection() 
        ->setPage(1, 1) 
        ->load(); 
       // if the product is associated with any category 
       if ($categories->count()) { 
        // show products from this category 
        $this->setCategoryId(current($categories->getIterator())); 
       } 
      } 

      $origCategory = null; 
      if ($this->getCategoryId()) { 
       $category = Mage::getModel('catalog/category')->load($this->getCategoryId()); 
       if ($category->getId()) { 
        $origCategory = $layer->getCurrentCategory(); 
        $layer->setCurrentCategory($category); 
       } 
      } 
      $this->_productCollection = $layer->getProductCollection(); 

      $this->prepareSortableFieldsByCategory($layer->getCurrentCategory()); 

      if ($origCategory) { 
       $layer->setCurrentCategory($origCategory); 
      } 
     } 

     return $this->_productCollection; 
    }     
} 

getLoadedProductCollection yöntem _getProductCollection bir çağrı sarar yüklüyor blok koduna göz gider ve toplama aslında yüklenir nerede _getProductCollection olduğunu.

Yani,

protected function _getProductCollection() 
{ 
    var_dump(__METHOD__); 
    var_dump($this->getCategoryId()); 
    Mage::Log(__METHOD__); 
    Mage::Log($this->getCategoryId()); 
} 

bazı geçici ayıklama kodu kategori id bloğuna düzen güncelleme XML hale getirmekte olduğunu sağlanmasına yardımcı olabilir.

Ancak, _getProductCollection numaralı telefondan biraz daha derine bakarsanız, numaralı telefon numarasının numarasının sıfırlandığı birkaç koşul olduğunu fark edeceksiniz.

if ($this->getShowRootCategory()) { 
    $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId()); 
} 
... 
if (Mage::registry('product')) { 
    // get collection of categories this product is associated with 
    $categories = Mage::registry('product')->getCategoryCollection() 
     ->setPage(1, 1) 
     ->load(); 
    // if the product is associated with any category 
    if ($categories->count()) { 
     // show products from this category 
     $this->setCategoryId(current($categories->getIterator())); 
    } 
} 
... 

Magento başka bir kod parçası show_root_category özelliğini belirledi veya kayıt bir ürün nesne var bir sayfada mı, Magento kategori numarası geçersiz kılar.

yapma işler daha da karmaşık, koleksiyon yüklendikten sonra bir korumalı özelliği hiçbir kamu alıcı yöntemi vardır

$this->_productCollection = $layer->getProductCollection(); 

üzerinde ayarlanır.

Buraya ilerlemenin yolları sayısızdır. Yerinde olsaydım ben Mage_Catalog_Block_Product_List uzanır ve yükleme yeni bir koleksiyon

  • bir koleksiyon üzerinde kategoriyi sıfırlamayı veya yükleme için bir yöntem olan özel bir blok sınıfını kullanarak aşağıdaki

    1. birini düşünün product/list

  • +0

    '' yöntemi ve ' category_id . * Ürün Görünümü * üzerine bir istisna atılır. İnceleniyor ... –

    +0

    Magento *, * Ürün Görünümü * üzerindeki koleksiyonun geçersiz kılınmasına neden oluyor, istisna oluşturuyor ve nasıl izleneceğimi bilmiyorum, böylece son iki öneriye geçtim. # 2 benim için çalıştı. Kısa bir cevap olarak gönderilecek. –

    1

    kod dayanmadan toplama kendim, başarıyla Magento CE 1.7.0.2 altında sorunu yeniden yarattı. gerekli ekledikten sonra

    <?xml version="1.0"?> 
    <layout> 
        <default> 
         <reference name="footer"> 
          <block type="catalog/product_list" name="custom.featuredProduct" as="product_of_the_month" category_id="13" template="custom/featured-product.phtml" /> 
         </reference> 
        </default> 
    </layout> 
    

    : Bazı ekstra satırları bazı sarma XML öğeleri eksik olduğunu ben anladım

    <default> 
        <reference name="footer"> 
         <block type="catalog/product_list" name="custom.featuredProduct" as="product_of_the_month" category_id="13" template="custom/featured-product.phtml" /> 
        </reference> 
    </default> 
    

    ve ekledi:

    Öncelikle bu içeriğe sahip bir local.xml yarattı Çalıştığı XML öğeleri.

    +0

    - İyi bir fikir, ancak 'local.xml'im de bunları içerir. Sadece kısaltılmış, ilgili kodu yayınladım. –

    +0

    Benim özel durumum var. Sadece kayıt için: Sadece ana sayfa için test ettim. Ayrıca local.xml ve custom/featured-product.phtml uzantılarının yüklü olmadığı kukla temayı oluşturdum. Ürünler örnek verileri kullanıldı ve yalnızca kategori kimliğini değiştirdim. – ceckoslab

    +0

    Belirtilen kategoriden doğru ürün koleksiyonunu yüklediniz mi? –