4
4
* See COPYING.txt for license details.
5
5
*/
6
6
7
+ use Magento \Catalog \ViewModel \Product \Listing \PreparePostData ;
8
+ use Magento \Framework \App \ActionInterface ;
9
+
7
10
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis
8
11
// phpcs:disable Generic.WhiteSpace.ScopeIndent.Incorrect
9
12
// phpcs:disable Generic.Files.LineLength
@@ -156,6 +159,7 @@ switch ($type = $block->getType()) {
156
159
default :
157
160
$ exist = null ;
158
161
}
162
+ $ _item = null ;
159
163
?>
160
164
161
165
<?php if ($ exist ):?>
@@ -170,13 +174,15 @@ switch ($type = $block->getType()) {
170
174
<div class="block <?= $ block ->escapeHtmlAttr ($ class ) ?> ">
171
175
<?php endif ; ?>
172
176
<div class="block-title title">
173
- <strong id="block-<?= $ block ->escapeHtmlAttr ($ class ) ?> -heading" role="heading" aria-level="2"><?= $ block ->escapeHtml ($ title ) ?> </strong>
177
+ <strong id="block-<?= $ block ->escapeHtmlAttr ($ class ) ?> -heading" role="heading"
178
+ aria-level="2"><?= $ block ->escapeHtml ($ title ) ?> </strong>
174
179
</div>
175
180
<div class="block-content content" aria-labelledby="block-<?= $ block ->escapeHtmlAttr ($ class ) ?> -heading">
176
181
<?php if ($ type == 'related ' && $ canItemsAddToCart ):?>
177
182
<div class="block-actions">
178
183
<?= $ block ->escapeHtml (__ ('Check items to add to the cart or ' )) ?>
179
- <button type="button" class="action select" data-role="select-all"><span><?= $ block ->escapeHtml (__ ('select all ' )) ?> </span></button>
184
+ <button type="button" class="action select"
185
+ data-role="select-all"><span><?= $ block ->escapeHtml (__ ('select all ' )) ?> </span></button>
180
186
</div>
181
187
<?php endif ; ?>
182
188
<div class="products wrapper grid products-grid products-<?= $ block ->escapeHtmlAttr ($ type ) ?> ">
@@ -195,11 +201,15 @@ switch ($type = $block->getType()) {
195
201
<?php endif ; ?>
196
202
<div class="product-item-info <?= /* @noEscape */ $ available ?> ">
197
203
<?= /* @noEscape */ '<!-- ' . $ image . '--> ' ?>
198
- <a href="<?= $ block ->escapeUrl ($ block ->getProductUrl ($ _item )) ?> " class="product photo product-item-photo">
204
+ <a href="<?= $ block ->escapeUrl ($ block ->getProductUrl ($ _item )) ?> "
205
+ class="product photo product-item-photo">
199
206
<?= $ block ->getImage ($ _item , $ image )->toHtml () ?>
200
207
</a>
201
208
<div class="product details product-item-details">
202
- <strong class="product name product-item-name"><a class="product-item-link" title="<?= $ block ->escapeHtml ($ _item ->getName ()) ?> " href="<?= $ block ->escapeUrl ($ block ->getProductUrl ($ _item )) ?> ">
209
+ <strong class="product name product-item-name"><a
210
+ class="product-item-link"
211
+ title="<?= $ block ->escapeHtmlAttr ($ _item ->getName ()) ?> "
212
+ href="<?= $ block ->escapeUrl ($ block ->getProductUrl ($ _item )) ?> ">
203
213
<?= $ block ->escapeHtml ($ _item ->getName ()) ?> </a>
204
214
</strong>
205
215
@@ -209,52 +219,88 @@ switch ($type = $block->getType()) {
209
219
<?= $ block ->getReviewsSummaryHtml ($ _item , $ templateType ) ?>
210
220
<?php endif ; ?>
211
221
212
- <?php if ($ canItemsAddToCart && !$ _item ->isComposite () && $ _item ->isSaleable () && $ type == 'related ' ):?>
222
+ <?php if ($ canItemsAddToCart && !$ _item ->isComposite () && $ _item ->isSaleable ()
223
+ && $ type == 'related ' ):?>
213
224
<?php if (!$ _item ->getRequiredOptions ()):?>
214
- <div class="field choice related">
215
- <input type="checkbox" class="checkbox related" id="related-checkbox<?= $ block ->escapeHtmlAttr ($ _item ->getId ()) ?> " name="related_products[]" value="<?= $ block ->escapeHtmlAttr ($ _item ->getId ()) ?> " />
216
- <label class="label" for="related-checkbox<?= $ block ->escapeHtmlAttr ($ _item ->getId ()) ?> "><span><?= $ block ->escapeHtml (__ ('Add to Cart ' )) ?> </span></label>
225
+ <div class="field choice related"><input
226
+ type="checkbox"
227
+ class="checkbox related"
228
+ id="related-checkbox<?= $ block ->escapeHtmlAttr ($ _item ->getId ()) ?> "
229
+ name="related_products[]"
230
+ value="<?= $ block ->escapeHtmlAttr ($ _item ->getId ()) ?> " />
231
+ <label
232
+ class="label"
233
+ for="related-checkbox<?= $ block ->escapeHtmlAttr (
234
+ $ _item ->getId ()
235
+ ) ?> "><span><?= $ block ->escapeHtml (__ ('Add to Cart ' )) ?> </span>
236
+ </label>
217
237
</div>
218
238
<?php endif ; ?>
219
239
<?php endif ; ?>
220
240
221
241
<?php if ($ showAddTo || $ showCart ):?>
242
+ <?php // phpcs:disable ?>
222
243
<div class="product actions product-item-actions">
223
244
<?php if ($ showCart ):?>
224
- <div class="actions-primary">
225
- <?php if ($ _item ->isSaleable ()):?>
226
- <?php if ($ _item ->getTypeInstance ()->hasRequiredOptions ($ _item )):?>
227
- <button class="action tocart primary" data-mage-init='{"redirectUrl": {"url": "<?= $ block ->escapeUrl ($ block ->getAddToCartUrl ($ _item )) ?> "}}' type="button" title="<?= $ block ->escapeHtmlAttr (__ ('Add to Cart ' )) ?> ">
228
- <span><?= $ block ->escapeHtml (__ ('Add to Cart ' )) ?> </span>
229
- </button>
230
- <?php else :?>
231
- <?php $ postDataHelper = $ this ->helper (Magento \Framework \Data \Helper \PostHelper::class);
232
- $ postData = $ postDataHelper ->getPostData ($ block ->escapeUrl ($ block ->getAddToCartUrl ($ _item )), ['product ' => $ _item ->getEntityId ()])
233
- ?>
234
- <button class="action tocart primary"
235
- data-post='<?= /* @noEscape */ $ postData ?> '
236
- type="button" title="<?= $ block ->escapeHtmlAttr (__ ('Add to Cart ' )) ?> ">
245
+ <?php if ($ _item ->isSaleable ()):?>
246
+ <div class="actions-primary">
247
+ <?php if (!$ _item ->getTypeInstance ()->isPossibleBuyFromList ($ _item )):?>
248
+ <button
249
+ class="action tocart primary"
250
+ data-mage-init='{"redirectUrl": {"url": "<?= $ block ->escapeUrl ($ block ->getAddToCartUrl ($ _item )) ?> "}}' type="button" title="<?= $ block ->escapeHtmlAttr (__ ('Add to Cart ' )) ?> ">
251
+ <span><?= $ block ->escapeHtml (__ ('Add to Cart ' )) ?> </span>
252
+ </button>
253
+ <?php else :?>
254
+ <?php
255
+ /** @var $viewModel PreparePostData */
256
+ $ viewModel = $ block ->getViewModel ();
257
+ $ postArray = $ viewModel ->getPostData (
258
+ $ block ->escapeUrl ($ block ->getAddToCartUrl ($ _item )),
259
+ ['product ' => $ _item ->getEntityId ()]
260
+ );
261
+ $ value = $ postArray ['data ' ][ActionInterface::PARAM_NAME_URL_ENCODED ];
262
+ ?>
263
+ <form data-role="tocart-form"
264
+ data-product-sku="<?= $ block ->escapeHtmlAttr ($ _item ->getSku ()) ?> "
265
+ action="<?= $ block ->escapeUrl ($ block ->getAddToCartUrl ($ _item )) ?> "
266
+ method="post">
267
+ <input type="hidden" name="product"
268
+ value="<?= /* @noEscape */ (int )$ _item ->getEntityId () ?> ">
269
+ <input type="hidden"
270
+ name="<?= /* @noEscape */ ActionInterface::PARAM_NAME_URL_ENCODED ?> "
271
+ value="<?= /* @noEscape */ $ value ?> ">
272
+ <?= $ block ->getBlockHtml ('formkey ' ) ?>
273
+ <button type="submit"
274
+ title="<?= $ block ->escapeHtmlAttr (__ ('Add to Cart ' )) ?> "
275
+ class="action tocart primary">
237
276
<span><?= $ block ->escapeHtml (__ ('Add to Cart ' )) ?> </span>
238
277
</button>
239
- <?php endif ; ?>
278
+ </form>
279
+ <?php endif ; ?>
280
+ <?php else :?>
281
+ <?php if ($ _item ->getIsSalable ()):?>
282
+ <div class="stock available">
283
+ <span><?= $ block ->escapeHtml (__ ('In stock ' )) ?> </span>
284
+ </div>
240
285
<?php else :?>
241
- <?php if ($ _item ->getIsSalable ()):?>
242
- <div class="stock available"><span><?= $ block ->escapeHtml (__ ('In stock ' )) ?> </span></div>
243
- <?php else :?>
244
- <div class="stock unavailable"><span><?= $ block ->escapeHtml (__ ('Out of stock ' )) ?> </span></div>
245
- <?php endif ; ?>
286
+ <div class="stock unavailable">
287
+ <span><?= $ block ->escapeHtml (__ ('Out of stock ' )) ?> </span>
288
+ </div>
246
289
<?php endif ; ?>
290
+ <?php endif ; ?>
247
291
</div>
248
292
<?php endif ; ?>
249
293
250
294
<?php if ($ showAddTo ):?>
251
- <div class="secondary-addto-links actions-secondary" data-role="add-to-links">
295
+ <div class="secondary-addto-links actions-secondary"
296
+ data-role="add-to-links">
252
297
<?php if ($ addToBlock = $ block ->getChildBlock ('addto ' )):?>
253
298
<?= $ addToBlock ->setProduct ($ _item )->getChildHtml () ?>
254
299
<?php endif ; ?>
255
300
</div>
256
301
<?php endif ; ?>
257
302
</div>
303
+ <?php // phpcs:enable ?>
258
304
<?php endif ; ?>
259
305
</div>
260
306
</div>
@@ -264,4 +310,15 @@ switch ($type = $block->getType()) {
264
310
</div>
265
311
</div>
266
312
</div>
313
+ <?php if (!$ block ->isRedirectToCartEnabled () && $ _item ):?>
314
+ <script type="text/x-magento-init">
315
+ {
316
+ "[data-role=tocart-form], .form.map.checkout": {
317
+ "catalogAddToCart": {
318
+ "product_sku": "<?= $ block ->escapeJs ($ _item ->getSku ()) ?> "
319
+ }
320
+ }
321
+ }
322
+ </script>
323
+ <?php endif ;?>
267
324
<?php endif ;?>
0 commit comments