0

How to display list of coupon code in product page.

1

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.

  • Thanks @prathap. I want only available coupon code of current product not all. – 55840 Apr 2 '19 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.