【问题标题】:Magento Add to Wishlist code not workingMagento 添加到愿望清单代码不起作用
【发布时间】:2014-01-16 20:49:44
【问题描述】:

我正在使用这个 sn-p 代码将愿望清单添加到我的产品视图中

<a onclick="setLocation('<?php echo $this->getAddToWishlistUrl($_product) ?>')"
 class="buttons-wish" title="<?php echo $this->__('Add to Wishlist') ?>"></a>
<a onclick="setLocation('<?php echo $this->getAddToCompareUrl($_product) ?>')"
 class="buttons-compare" title="<?php echo $this->__('Add to Compare') ?>"></a>

但由于某种原因,它不起作用。

我需要帮助来解决问题

【问题讨论】:

  • 每一个说明问题的好 StackOverflow 问题都说明了问题的实质。请解释问题is not working不是问题的准确定义。
  • 我的意思是:你想要的和你得到的到底有什么区别?
  • addto.phtml 中的代码被注释掉了,所以我取消了它的注释,但我仍然没有在我的产品视图中看到添加到愿望清单
  • 我想在我的产品视图中添加到愿望清单

标签: php magento


【解决方案1】:

试试这个。

<a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a>                                                    
<a href="<?php echo $this->helper('catalog/product_compare')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Compare') ?></a>

【讨论】:

  • 在我的情况下,愿望清单 URL 总是使用 https:// 而不是 http;// 我该如何防止这种情况发生?最近我们禁用了网站上的 https://
【解决方案2】:

我在 Magento 1.8 中的愿望清单中发现的一个问题与新的 form_key 参数有关,也许这就是您遇到问题的原因。

Magento 1.8 引入了表单键(它们可能在 1.8 之前以某种形式存在,但现在它们无处不在)。如果您希望您的任何表格做任何事情,它们是强制性的。想要加入购物车/愿望清单/比较清单?需要一个表单键。想提交联系表格?需要一个表单键。想要登录?表单键。等等。

所有必要的核心模板都已更新,以将此键作为隐藏的输入元素包含在内 - 根据我的经验,您只需将这一行从十几个不同的地方复制并粘贴到您拥有的任何自定义模板中(在表单元素),它们也会很好。

一切都很好 - 进一步的安全增强/垃圾邮件防护功能,并且很容易适应。

问题在于$this-&gt;helper('wishlist')-&gt;getAddUrl($_product) 生成的 URL 包含此表单键,例如/wishlist/index/add/product/101/form_key/xyz 这根本行不通。在这种情况下,“不起作用”是指“添加到愿望清单”按钮不会向您的愿望清单添加任何内容,尽管它仍会将您带到您的愿望清单页面。

我不确定是意外还是设计使然,但 form_key 似乎不像大多数 Magento 参数那样在 URL 中起作用。它必须是标准的 HTTP 变量,例如表单字段中的输入或添加到 URL 中的查询字符串,例如 ?form_key=xyz。

至少在产品视图上,我认为在列表视图上,愿望列表按钮实际上调用productAddToCartForm.submitLight(),这意味着 URL 根本不需要表单键,因为 productAddToCartForm 已经包含它。

如何解决?与大多数事情一样,有很多方法。您可以停止使用 $this-&gt;helper('wishlist')-&gt;getAddUrl($_product) 并将其替换为 $this-&gt;getStoreUrl('wishlist/index/add/product/'.$_product-&gt;getId()) 之类的东西(我尚未测试过,因此请谨慎使用),或者您可以覆盖 Mage_Wishlist_Helper_Data::getAddUrl 这就是我选择的方式它。这种方式会影响所有尝试使用“添加到愿望清单”网址的模板等,这可能是好事也可能是坏事。我觉得很好,在这种情况下,但这取决于你。

将 Mage/Wishlist/Helper/Data.php 从 app/code/core 复制到 app/code/local 并更改:

public function getAddUrl($item)
{
    return $this->getAddUrlWithParams($item);
}

到:

public function getAddUrl($item)
{
    $productId = null;
    if ($item instanceof Mage_Catalog_Model_Product) {
        $productId = $item->getEntityId();
    }
    if ($item instanceof Mage_Wishlist_Model_Item) {
        $productId = $item->getProductId();
    }

    if ($productId) {
        $params = array('product' => $productId);
        return $this->_getUrlStore($item)->getUrl('wishlist/index/add', $params);
    }

    return false;
}

...这只是 getAddUrlWithParams 函数的副本,其中“params”硬编码为产品 ID。

【讨论】:

    【解决方案3】:

    签入后台System-&gt;Configuration-&gt;Advanced-&gt;Advanced-&gt;Disable Modules Output Mage_Wishlist 是否启用。

    【讨论】:

      【解决方案4】:

      我也遇到过类似的问题。单击“添加到愿望清单”,页面将重定向到我的收藏夹页面,并显示以下错误消息。

      我们现在无法将商品添加到愿望清单:无法将没有库存的产品添加到愿望清单..

      解决方案:愿望清单功能没有问题。默认情况下,Magento 2 附带启用了愿望清单功能。在我们的例子中,我们使用了多个自定义来源。您可以通过访问查看此内容

      Admin-> stores-> sources (if you see apart from the default source)
      

      如果您希望禁用默认来源。

      Admin -> Stores -> Configuration -> Catalog -> Inventory -> Product stock options -> Set the manage stock = NO
      

      参考https://docs.magento.com/user-guide/catalog/inventory.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多