我正在使用magento 2.1.2。我有心愿在侧栏中获取愿望清单产品属性,如颜色,尺寸。
以下是获取价格名称但无法获取大小和颜色等属性的代码
protected function getItemData(\Magento\Wishlist\Model\Item $wishlistItem) {
$product = $wishlistItem->getProduct();
return [
'image' => $this->getImageData($product),
'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem),
'product_name' => $product->getName(),
'product_color' => 'Blue',
'product_size' => 'L',
'product_price' => $this->block->getProductPriceHtml(
$product,
'wishlist_configured_price',
\Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
['item' => $wishlistItem]
),
'product_is_saleable_and_visible' => $product->isSaleable() && $product->isVisibleInSiteVisibility(),
'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product),
'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem, true),
'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem, true),
];
}
感谢
答案 0 :(得分:2)
为此破解核心并不是一个好主意。您需要create a plugin来修改此方法的结果。但是,由于这种方法受到保护,你将不能仅仅涉及到这一点。您可以挂钩getSectionData
public
。
另一个问题是,愿望清单项目由浏览器存储在本地存储中。因此,在测试时,在清除Magento缓存后,您还必须在浏览器中清除站点的本地存储并再次登录,然后将另一项添加到您的愿望清单。
创建模块后,您可以添加以下文件:
应用程序/代码/供应商/模块/插件/ Magento的/收藏/ CustomerData / Wishlist.php
<?php
namespace Vendor\Module\Plugin\Magento\Wishlist\CustomerData;
class Wishlist
{
/**
* @var \Magento\Wishlist\Helper\Data
*/
protected $wishlistHelper;
/**
* @var \Magento\Catalog\Model\ProductRepository
*/
protected $_productRepository;
public function __construct(
\Magento\Wishlist\Helper\Data $wishlistHelper,
\Magento\Catalog\Model\ProductRepository $productRepository
) {
$this->wishlistHelper = $wishlistHelper;
$this->productRepository = $productRepository;
}
public function afterGetSectionData(
\Magento\Wishlist\CustomerData\Wishlist $subject,
$result
) {
$collection = $this->wishlistHelper->getWishlistItemCollection();
$collection->clear()->setPageSize(count($result['items']))->setInStockFilter(true)->setOrder('added_at');
foreach($result['items'] as &$item) {
foreach ($collection as $wishlistItem) {
$product = $wishlistItem->getProduct();
if ($item['product_url'] == $product->getProductUrl()) {
$item['product_color'] = $product->getAttributeText('color');
}
}
}
return $result;
}
}
应用程序/代码/供应商/模块的/ etc / di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Wishlist\CustomerData\Wishlist">
<plugin disabled="false" name="Vendor_Module_Plugin_Magento_Wishlist_CustomerData_Wishlist" sortOrder="10" type="Vendor\Module\Plugin\Magento\Wishlist\CustomerData\Wishlist"/>
</type>
</config>
然后在覆盖app / code / Magento / Wishlist / view / frontend / templates / sidebar.phtml
的文件中您可以执行类似
的操作<span data-bind="visible: product_color, text: product_color"></span>
里面的
<li class="product-item">
答案 1 :(得分:0)
另一种在心愿单侧边栏中显示产品属性的方法,
首先需要删除所有愿望清单项目,清除缓存并将新产品添加到愿望清单。
app / code / Vendor / Module / etc / module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "urn:magento:framework:Module/etc/module.xsd">
<module name="Yourtheme_YourModule" setup_version="1.0.0">
<sequence>
<module name="Magento_Wishlist" />
</sequence>
</module>
</config>
app / code / Vendor / Module / etc / di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Wishlist\CustomerData\Wishlist" type="Yourtheme\yourmodule\CustomerData\Wishlist" />
</config>
app /代码/供应商/模块/CustomerData/Wishlist.php
namespace YourTheme\YourModule\CustomerData;
/**
* Wishlist section
*/
class Wishlist extends \Magento\Wishlist\CustomerData\Wishlist
{
/**
* Get wishlist items
*
* @return array
*/
protected function getWishlistItems()
{
$this->view->loadLayout();
$collection = $this->wishlistHelper->getWishlistItemCollection();
$collection->clear()->setPageSize(self::SIDEBAR_ITEMS_NUMBER)
->setInStockFilter(true)->setOrder('added_at');
$items = [];
foreach ($collection as $wishlistItem) {
$items[] = $this->getProductItemData($wishlistItem);
}
return $items;
}
/**
* Retrieve wishlist item data
*
* @param \Magento\Wishlist\Model\Item $wishlistItem
* @return array
*/
protected function getProductItemData(\Magento\Wishlist\Model\Item $wishlistItem)
{
$product = $wishlistItem->getProduct();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$model = $objectManager->create('\Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface');
$custpomProductAttribute = $product->getYourAttribute();
return [
'image' => $this->getImageData($model->getFinalProduct($wishlistItem)),
'product_sku' => $product->getSku(),
'product_id' => $product->getId(),
'product_url' => $this->wishlistHelper->getProductUrl($wishlistItem),
'product_name' => $product->getName(),
'product_price' => $this->block->getProductPriceHtml(
$product,
'wishlist_configured_price',
\Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
['item' => $wishlistItem]
),
'product_is_saleable_and_visible' => $product->isSaleable() &&
$product->isVisibleInSiteVisibility(),
'product_has_required_options' => $product->getTypeInstance()->hasRequiredOptions($product),
'add_to_cart_params' => $this->wishlistHelper->getAddToCartParams($wishlistItem),
'delete_item_params' => $this->wishlistHelper->getRemoveParams($wishlistItem),
'your_attribute' => $custpomProductAttribute,
];
}
答案 2 :(得分:0)
感谢@VictorS。 尝试了第一种方法,可以通过首选项覆盖类,但是很幸运,一个小时后尝试编译和重新编译依赖项注入,找到答案并使用插件创建了一个模块,可以神奇地工作。 Magento 2.2.7。
谢谢