Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Wishlist.php
Go to the documentation of this file.
1 <?php
8 
10 
16 {
22  protected $urlBuilder;
23 
27  protected $scopeConfig;
28 
33  protected $eventManager;
34 
40  protected $layout;
41 
45  protected $wishlistHelper;
46 
50  protected $outputHelper;
51 
55  protected $imageHelper;
56 
60  protected $wishlistBlock;
61 
65  protected $request;
66 
70  protected $customerFactory;
71 
86  public function __construct(
87  \Magento\Wishlist\Helper\Rss $wishlistHelper,
88  \Magento\Wishlist\Block\Customer\Wishlist $wishlistBlock,
89  \Magento\Catalog\Helper\Output $outputHelper,
90  \Magento\Catalog\Helper\Image $imageHelper,
91  \Magento\Framework\UrlInterface $urlBuilder,
93  \Magento\Framework\Event\ManagerInterface $eventManager,
94  \Magento\Customer\Model\CustomerFactory $customerFactory,
95  \Magento\Framework\View\LayoutInterface $layout,
97  ) {
98  $this->wishlistHelper = $wishlistHelper;
99  $this->wishlistBlock = $wishlistBlock;
100  $this->outputHelper = $outputHelper;
101  $this->imageHelper = $imageHelper;
102  $this->urlBuilder = $urlBuilder;
103  $this->scopeConfig = $scopeConfig;
104  $this->eventManager = $eventManager;
105  $this->customerFactory = $customerFactory;
106  $this->layout = $layout;
107  $this->request = $request;
108  }
109 
115  public function isAllowed()
116  {
117  return (bool)$this->scopeConfig->getValue(
118  'rss/wishlist/active',
120  );
121  }
122 
128  public function getRssData()
129  {
130  $wishlist = $this->getWishlist();
131  if ($wishlist->getId()) {
132  $data = $this->getHeader();
133 
135  foreach ($wishlist->getItemCollection() as $wishlistItem) {
136  /* @var $product \Magento\Catalog\Model\Product */
137  $product = $wishlistItem->getProduct();
138  $productUrl = $this->wishlistBlock->getProductUrl($product, ['_rss' => true]);
139  $product->setAllowedInRss(true);
140  $product->setAllowedPriceInRss(true);
141  $product->setProductUrl($productUrl);
142  $args = ['product' => $product];
143 
144  $this->eventManager->dispatch('rss_wishlist_xml_callback', $args);
145 
146  if (!$product->getAllowedInRss()) {
147  continue;
148  }
149 
150  $description = '<table><tr><td><a href="' . $productUrl . '"><img src="'
151  . $this->imageHelper->init($product, 'rss_thumbnail')->getUrl()
152  . '" border="0" align="left" height="75" width="75"></a></td>'
153  . '<td style="text-decoration:none;">'
154  . $this->outputHelper->productAttribute(
155  $product,
156  $product->getShortDescription(),
157  'short_description'
158  ) . '<p>';
159 
160  if ($product->getAllowedPriceInRss()) {
161  $description .= $this->getProductPriceHtml($product);
162  }
163  $description .= '</p>';
164 
165  if (trim($product->getDescription()) != '') {
166  $description .= '<p>' . __('Comment:') . ' '
167  . $this->outputHelper->productAttribute(
168  $product,
169  $product->getDescription(),
170  'description'
171  ) . '<p>';
172  }
173  $description .= '</td></tr></table>';
174 
175  $data['entries'][] = ([
176  'title' => $product->getName(),
177  'link' => $productUrl,
178  'description' => $description,
179  ]);
180  }
181  } else {
182  $data = [
183  'title' => __('We cannot retrieve the Wish List.'),
184  'description' => __('We cannot retrieve the Wish List.'),
185  'link' => $this->urlBuilder->getUrl(),
186  'charset' => 'UTF-8',
187  ];
188  }
189 
190  return $data;
191  }
192 
196  public function getCacheKey()
197  {
198  return 'rss_wishlist_data';
199  }
200 
204  public function getCacheLifetime()
205  {
206  return 60;
207  }
208 
214  public function getHeader()
215  {
216  $customerId = $this->getWishlist()->getCustomerId();
217  $customer = $this->customerFactory->create()->load($customerId);
218  $title = __('%1\'s Wishlist', $customer->getName());
219  $newUrl = $this->urlBuilder->getUrl(
220  'wishlist/shared/index',
221  ['code' => $this->getWishlist()->getSharingCode()]
222  );
223 
224  return ['title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8'];
225  }
226 
232  protected function getWishlist()
233  {
234  $wishlist = $this->wishlistHelper->getWishlist();
235  return $wishlist;
236  }
237 
244  public function getProductPriceHtml(\Magento\Catalog\Model\Product $product)
245  {
246  $price = '';
248  $priceRender = $this->layout->getBlock('product.price.render.default');
249  if (!$priceRender) {
250  $priceRender = $this->layout->createBlock(
251  \Magento\Framework\Pricing\Render::class,
252  'product.price.render.default',
253  ['data' => ['price_render_handle' => 'catalog_product_prices']]
254  );
255  }
256  if ($priceRender) {
257  $price = $priceRender->render(
258  'wishlist_configured_price',
259  $product,
260  ['zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST]
261  );
262  }
263  return $price;
264  }
265 
269  public function getFeeds()
270  {
271  return [];
272  }
273 
277  public function isAuthRequired()
278  {
279  if ($this->request->getParam('sharing_code') == $this->getWishlist()->getSharingCode()) {
280  return false;
281  }
282  return true;
283  }
284 }
$title
Definition: default.phtml:14
__construct(\Magento\Wishlist\Helper\Rss $wishlistHelper, \Magento\Wishlist\Block\Customer\Wishlist $wishlistBlock, \Magento\Catalog\Helper\Output $outputHelper, \Magento\Catalog\Helper\Image $imageHelper, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Framework\View\LayoutInterface $layout, \Magento\Framework\App\RequestInterface $request)
Definition: Wishlist.php:86
$customer
Definition: customers.php:11
__()
Definition: __.php:13
$price
$wishlist
Definition: wishlist.php:10