繁体   English   中英

Magento 添加到愿望清单代码不起作用

[英]Magento Add to Wishlist code not working

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

<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>

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

我需要帮助来解决问题

试试这个。

<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>

检入后端System->Configuration->Advanced->Advanced->Disable Modules Output Mage_Wishlist是否已启用。

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

Magento 1.8引入了表单密钥(它们可能以某种形式存在于1.8之前,但现在无处不在)。 如果您希望任何表单执行任何操作,则它们是必需的。 要添加到购物车/愿望清单/比较清单吗? 需要一个表单键。 要提交联系表吗? 需要一个表单键。 要登录吗? 表单键。 等等。

所有必需的核心模板均已更新,以将该键作为隐藏的输入元素包括在内-以我的经验,您只需要从十几个不同位置中的任何行复制并粘贴此行到您拥有的任何自定义模板中(在form元素内的某个位置)他们也会没事的。

一切顺利–进一步的安全性增强/垃圾邮件阻止功能,并且易于容纳。

问题是$this->helper('wishlist')->getAddUrl($_product)生成的URL 包含此表单密钥,例如/wishlist/index/add/product/101/form_key/xyz根本不包含此表单密钥“工作。 在这种情况下,“无效”是指“添加到愿望清单”按钮不会将任何内容添加到您的愿望清单中,尽管它仍然可以将您带到您的愿望清单页面。

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

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

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

将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。

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

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

解决方案:心愿单功能没有问题。 默认情况下,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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM