2012-02-01 252 views
0

我在我的模板文件中的以下代碼:設置變量

<?php foreach($collection as $product): ?> 
     <?php Mage::unregister('product'); ?> 
     <?php Mage::register('product', $product); ?> 
     <?php $_product = $this->getProduct(); ?> 
     <div class="product-view"> 
      <div class="product-essential"> 
       <form action="<?php echo $this->getSubmitUrl($_product); ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>> 
        <div class="no-display"> 
         <input type="hidden" name="product" value="<?php echo $product->getId() ?>" /> 
         <input type="hidden" name="related_product" id="related-products-field" value="" /> 
        </div> 
        <div class="product-shop"> 
         <div class="product-name"> 
          <h1><?php echo $_product->getName(); ?></h1> 
         </div> 
         <?php echo $this->getChildHtml('alert_urls') ?> 
         <?php echo $this->getChildHtml('product_type_data') ?> 
         <?php echo $this->getTierPriceHtml() ?> 
         <?php echo $this->getChildHtml('extrahint') ?> 
         <?php if (!$this->hasOptions()):?> 
          <div class="add-to-box"> 
           <?php if($_product->isSaleable()): ?> 
            <?php echo $this->getChildHtml('addtocart') ?> 
            <?php if($this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?> 
             <span class="or"><?php echo $this->__('OR') ?></span> 
            <?php endif; ?> 
           <?php endif; ?> 
           <?php echo $this->getChildHtml('addto') ?> 
          </div> 
          <?php echo $this->getChildHtml('extra_buttons') ?> 
         <?php endif; ?> 
         <?php if ($_product->getShortDescription()):?> 
          <div class="short-description"> 
           <h2><?php echo $this->__('Quick Overview') ?></h2> 
           <div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div> 
          </div> 
         <?php endif;?> 
         <?php echo $this->getChildHtml('other');?> 
         <?php if ($_product->isSaleable() && $this->hasOptions()):?> 
          <?php echo $this->getChildChildHtml('container1', '', true, true) ?> 
         <?php endif;?> 
        </div> 
        <div class="product-img-box"> 
         <?php echo $this->getChildHtml('media') ?> 
        </div> 
        <div class="clearer"></div> 
        <?php if ($_product->isSaleable() && $this->hasOptions()):?> 
         <?php echo $this->getChildChildHtml('container2', '', true, true) ?> 
        <?php endif;?> 
       </form>     
      </div> 
     </div> 
    <?php endforeach; ?> 

在我的佈局XML我:

<reference name="root"> 
     <block type="catalog/product_view" name="test" template="path/to/products.phtml"/> 
    </reference> 

我知道,因爲我已經將它輸出調用成功來自它的靜態內容。然而,看起來好像

$product 

塊沒有被識別,即使它是有效的產品實例。

該代碼直接從默認/ template/catalog/product/view.phtml中提取,它可以正常工作。我所有的代碼都會嘗試去改變產品。這是我第一次與積木合作,並且非常感謝任何幫助/提示/指示我錯誤的地方。

在此先感謝:-)。

編輯

原來我塊的理解/ Magento的是不正確的。答案在於閱讀AlanStorm教程。

+0

您同時使用'$ product'和'$ _product'這是混亂的,給他們一個有意義的名字,如果他們是完全不同的。另外,當你不使用它時,註冊循環頂部的對象有什麼意義? – clockworkgeek 2012-02-01 21:52:06

+0

catalog/product_view查找$ product變量(在getProduct()中它會搜索Mage註冊表)。所以它被使用。命名約定直接來自核心Magento代碼。 – james 2012-02-01 22:44:24

回答

0

我有類似的問題,首先嚐試更換佈局xml中的所有雙引號。
我希望你只是在這裏介紹虛擬引用template="path/to/products.phtml",並在你的代碼中使用真正的Magento路徑。
不能告訴你更多的原因,我也是Magento的初學者。只是一些有用的鏈接:
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-1-introduction-to-magento
http://inchoo.net/category/ecommerce/magento/

+0

用單引號替換double是無效的XML。是的,我在我的代碼中使用了正確的路徑。我已經多次瀏覽了這兩個網站以獲得幫助,至今還沒有找到答案。我會繼續尋找。 – james 2012-02-01 21:20:55