How can i add buynow button on Homepage widget list products ?
hello, I amm using this module magento2-buynow
but this add in category page and product page.
I need to add buy now button to homepage widget product list.
This my widget code
<?php
$type = 'widget-product-carousel';
$mode = 'grid';
$image = 'new_products_content_widget_grid';
$items = $block->getProductCollection()->getItems();
$showWishlist = true;
$showCompare = true;
$showCart = true;
$templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::DEFAULT_VIEW;
$description = false;
$data = $this->helper('Megnor\Framework\Helper\Data');
?>
<div class="block widget block-products-list <?= /* @noEscape */ $mode ?>">
<?php if ($block->getTitle()): ?>
<!-- <div class="block-title">
<strong><?= $block->escapeHtml(__($block->getTitle())) ?></strong>
</div> -->
<?php endif ?>
<div class="block-new-content">
<?= /* @noEscape */ '<!-- ' . $image . '-->' ?>
<div class="products-<?= /* @noEscape */ $mode ?> <?= /* @noEscape */ $mode ?> wrapper products">
<ol class="product-items <?= /* @noEscape */ $type ?>">
<?php $iterator = 1; ?>
<?php foreach ($items as $_item): ?>
<?php if ($iterator++ != 1): ?></li><?php endif ?>
<li class="product-item">
<div class="product-item-info">
<div class="product-block-inner">
<div class="product-item-image">
<a href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>" class="product-item-photo">
<?= $block->getImage($_item, $image)->toHtml() ?>
</a>
<?php
$is_new = $data->checkProductIsNew($_item);
if($is_new) { ?>
<span class="new-label"><?php echo __("New");?></span>
<?php } ?>
<?php
$specialprice = $_item->getSpecialPrice();
$specialPriceFromDate = $_item->getSpecialFromDate();
$specialPriceToDate = $_item->getSpecialToDate();
$today = time();
if ($specialprice) {
if($today >= strtotime($specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime($specialPriceFromDate) && is_null($specialPriceToDate)) {
$originalPrice = $_item->getPrice();
$finalPrice = $_item->getFinalPrice();
$percentage = $percentage = round(($originalPrice - $finalPrice) * 100 / $originalPrice);
?>
<span class="sale-label"><?php echo "-".$percentage."%";?></span>
<?php
}
}
?>
<?php if ($showWishlist): ?>
<div class="actions-secondary" data-role="add-to-links">
<?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow() && $showWishlist): ?>
<a href="#"
data-post='<?= /* @noEscape */ $block->getAddToWishlistParams($_item) ?>' class="action towishlist" data-action="add-to-wishlist" title="<?= $block->escapeHtmlAttr(__('Add to Wish List')) ?>">
<span><?= $block->escapeHtml(__('Add to Wish List')) ?></span>
</a>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<div class="product-item-details">
<?php
echo $block->getProductPriceHtml($_item, $type);
?>
<strong class="product-item-name">
<a title="<?= $block->escapeHtml($_item->getName()) ?>"
href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>"
class="product-item-link">
<?= $block->escapeHtml($_item->getName()) ?>
</a>
</strong>
<?php if ($templateType): ?>
<?= $block->getReviewsSummaryHtml($_item, $templateType,true) ?>
<?php endif; ?>
<?php if ($showWishlist || $showCompare || $showCart): ?>
<div class="product-item-inner">
<?php if ($showCart): ?>
<div class="actions-primary">
<?php if ($_item->isSaleable()): ?>
<?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
<button class="action tocart primary" data-mage-init='{"redirectUrl":{"url":"<?= $block->escapeUrl($block->getAddToCartUrl($_item)) ?>"}}' type="button" title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>">
<span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
</button>
<?php else: ?>
<?php
$postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
$postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
?>
<button class="action tocart primary" data-post='<?= /* @noEscape */ $postData ?>' type="button" title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>">
<span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
</button>
<?php endif; ?>
<?php else: ?>
<?php if ($_item->getIsSalable()): ?>
<div class="stock available"><span><?= $block->escapeHtml(__('In stock')) ?></span></div>
<?php else: ?>
<div class="stock unavailable"><span><?= $block->escapeHtml(__('Out of stock')) ?></span></div>
<?php endif; ?>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if ($showCompare): ?>
<div class="actions-secondary" data-role="add-to-links">
<?php if ($block->getAddToCompareUrl() && $showCompare): ?>
<?php $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare');?>
<a href="#" class="action tocompare" data-post='<?= /* @noEscape */ $compareHelper->getPostDataParams($_item) ?>' title="<?= $block->escapeHtmlAttr(__('Add to Compare')) ?>">
<span><?= $block->escapeHtml(__('Add to Compare')) ?></span>
</a>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?= ($iterator == count($items)+1) ? '</li>' : '' ?>
<?php endforeach ?>
</ol>
</div>
<?= $block->getPagerHtml() ?>
</div>
</div>
inrsaurabh
1,6763 gold badges28 silver badges54 bronze badges
asked Mar 31, 2018 at 21:22
1 Answer 1
You can add buynow button in custom widget after add to cart
Final Code:
<div class="actions-primary">
<?php if ($_item->isSaleable()): ?>
<?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
...
// Add to cart code
...
<?php endif; ?>
<?php
// Get buynow block html
$buyNowHtml = $this->getLayout()
->createBlock('Prince\Buynow\Block\Product\ListProduct')
->setProduct($_item)
->setTemplate('Prince_Buynow::buynow-list.phtml')
->toHtml();
echo $buyNowHtml;
?>
<?php else: ?>
...
//In stock out of stock code
...
<?php endif; ?>
</div>
answered Apr 2, 2018 at 6:51
-
Hello, Please can you explain more. I have the same case. Your code not workingMohammad Hammadi– Mohammad Hammadi2018年06月11日 12:12:45 +00:00Commented Jun 11, 2018 at 12:12
default