Question

How to display list of coupon code in product page.

Was it helpful?

Solution

Try this,

Add a helper on your custom module and path be like

app/code/Vendor/Module/Helper/Data.php

then add the below code to get the coupon code collection

<?php                                                                 
namespace {Vendor}\{ModuleName}\Helper;                                   
class Data extends \Magento\Framework\App\Helper\AbstractHelper               
{ 
  public function __construct(
   \Magento\SalesRule\Model\CouponFactory $couponFactory
  ){
     $this->couponFactory = $couponFactory;
   }
  public function getCoupon()
  {
     $couponCollection = $this->couponFactory->create()->getCollection();
     return $couponCollection;
  }
}

then add catalog_product_view.xml in your custom module and path be like

app/code/Vendor/Module/view/frontend/layout/catalog_product_view.xml

then add the below code to it

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
   <referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template"  name="product_coupon" template="Vendor_Module::coupon.phtml">
        </block>
    </referenceContainer>
</body>
</page>

then add the phtml in the below path

app/code/Vendor/Module/view/frontend/templates/coupon.phtml

then add the below code to the phtml file to get the coupon collection

<?php $coupon = $this->helper('Vendor\Module\Helper\Data')->getCoupon();
foreach($coupon as $items)
{
  echo "Coupon Code :" . $items->getCode();
}

You'll get all the coupon code there and you can get any other attribute from this table salesrule_coupon

For more information refer this link

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top