Magento 和相关产品的帮助

发布于 2024-10-10 18:37:20 字数 843 浏览 2 评论 0原文

我有一个客户产品页面,实际上位于catalog/product/view.phtml 页面旁边。它与该页面基本相同,但有一些小例外。它基本上是一个“每日产品”类型页面,因此我无法将其与常规产品页面结合起来,因为我必须从数据库中获取数据并执行加载以获取产品信息

$_product = Mage::getModel('catalog/product')->load($row['productid']);

长话短说,一切作品(包括所有子 html 块),但相关产品除外。

加载后,我将产品保存到注册表中

Mage::register('product', $_product); 

,然后尝试使用以下内容加载相关产品:

echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate('catalog/product/list/related.phtml')->toHtml();`

所有这些都会返回错误:

Fatal error: Call to a member function getSize() on a non-object in catalog/product/list/related.phtml on line 29`, 

并且第 29 行是

<?php if($this->getItems()->getSize()): ?>`.

任何帮助加载相关产品的帮助。

I have a customer product page that literally lives beside the catalog/product/view.phtml page. It's basically identical to that page with a few small exceptions. It's basically a 'product of the day' type page so I can't combine it with the regular product page since I have to fetch the data from the DB and perform a load to get the product information

$_product = Mage::getModel('catalog/product')->load($row['productid']);

To make a long story short, everything works (including all children html blocks) with the singular exception of the related products.

After the load I save the product into the registry with

Mage::register('product', $_product); 

and then attempt to load the related products with:

echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate('catalog/product/list/related.phtml')->toHtml();`

All of which give back the error:

Fatal error: Call to a member function getSize() on a non-object in catalog/product/list/related.phtml on line 29`, 

and line 29 is

<?php if($this->getItems()->getSize()): ?>`.

Any help getting the relateds to load would be appreicated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼亦尘 2024-10-17 18:37:20

我不太明白你想要做什么,但我知道你为什么会遇到错误。您正在创建一个块,其类别名/类为

catalog/product_view
Mage_Catalog_Block_Product_View

,但您将此块的模板设置为

 catalog/product/list/related.phtml

Stock catalog/product/list/lated.phtml 模板是为了与 一起使用而构建的>catalog/product_list_lated,而不是catalog/product_view 块。

如果您查看 catalog/product_list_lated 块(即 Mage_Catalog_Block_Product_List_Related)的类定义,您可以看到有一个 getItems()方法。

public function getItems()
{
    return $this->_itemCollection;
}

它返回一个集合。该集合是在 _prepareData 方法中设置的。

protected function _prepareData()
{
    $product = Mage::registry('product');
    /* @var $product Mage_Catalog_Model_Product */

    $this->_itemCollection = $product->getRelatedProductCollection()
    ...

此集合从未使用 catalog/product_view 块进行设置,这就是您收到错误的原因。

在上面的代码中,如果您切换到创建 catalog/product_list_lated 块,您的错误应该会消失。

I didn't quite follow what you're trying to do, but I know why you're getting your errors. You're creating a block whose class-alias/class is

catalog/product_view
Mage_Catalog_Block_Product_View

but you're setting this block's template as

 catalog/product/list/related.phtml

The stock catalog/product/list/related.phtml template was built to be used with a catalog/product_list_related Block only, and not a catalog/product_view Block.

If you take a look at the class definition for a catalog/product_list_related Block (which is a Mage_Catalog_Block_Product_List_Related), you can see that there's a getItems() method.

public function getItems()
{
    return $this->_itemCollection;
}

which returns a collection. The collection is set in the _prepareData method

protected function _prepareData()
{
    $product = Mage::registry('product');
    /* @var $product Mage_Catalog_Model_Product */

    $this->_itemCollection = $product->getRelatedProductCollection()
    ...

This collection is never set with a catalog/product_view Block, which is why you're getting your errors.

In your code above, if you switch to creating a catalog/product_list_related block, your errors should go away.

北风几吹夏 2024-10-17 18:37:20
public function relatedproductsAction(){

    $this->loadLayout();
    $relatedBlock = "";

    $rec_prod_id = Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
    $_product = Mage::getModel('catalog/product')->load($rec_prod_id);
    Mage::register('product', $_product);


    $relatedBlock = $this->getLayout()->createBlock('catalog/product_list_related')->setTemplate('catalog/product/related.phtml')->toHtml();

    echo $relatedBlock;
    exit;

}

当产品添加到购物车后,通过ajax调用获取相关块的html。可能相对有帮助。

public function relatedproductsAction(){

    $this->loadLayout();
    $relatedBlock = "";

    $rec_prod_id = Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
    $_product = Mage::getModel('catalog/product')->load($rec_prod_id);
    Mage::register('product', $_product);


    $relatedBlock = $this->getLayout()->createBlock('catalog/product_list_related')->setTemplate('catalog/product/related.phtml')->toHtml();

    echo $relatedBlock;
    exit;

}

Getting html of related block through ajax call, right after when product is added to cart. might be relatively helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文