how to get a coupon code in magento 2.right now i am getting name i want a coupon code

public function __construct(

        \Magento\Framework\View\Element\Template\Context $context,

        \Magento\Framework\Registry $registry,

        \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory  $collection,
        \Magento\Framework\Stdlib\DateTime\Timezone $_stdTimezone,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        $this->collection = $collection;
        $this->coupon = $coupon;
        $this->_stdTimezone = $_stdTimezone;
        parent::__construct($context, $data, $collection, $coupon);
    }

public function getDiscountCoupon()

    {

        $currentTime = $this->_stdTimezone->date()->format('Y-m-d');
        $rules = $this->collection->create()
                        ->addFieldToFilter('is_active', 1)
                        ->addFieldToFilter('to_date', ['gteq' => $currentTime]);
            //echo $rules->getSelect()->__toString(); die();
        $product = $this->_coreRegistry->registry('product');

        foreach ($rules as $rule) {
            try {
                if ($rule->getActions()->validate($product)) {
                        $activeRules[] = $rule;
                }
            } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
                continue;
            }
        }
        $rulesApplied = "<h3>Applicable Offers:</h3>";
        foreach ($rules as $rule) {
                $rulesApplied .= $rule->getName()."</br>";
        }
        return $rulesApplied;
    }

Solutions

Please try $rule->getData('coupon_code') instead of $rule->getName(). Just take care that not all cart rules will have a coupon code, and if you created multiple coupon codes for a single rule, this won't work either.

Similar questions

why this error is showing "We can't add this item to your shopping cart right now." during add bundle product in cart in Magento 2.1?
I will change price of simple product in cart through observer,it will change successfully.This product is also part of bundle product.When we add this product in cart it display error We can't add this item to your shopping cart right now.below is my observer code.Please tell me how to solve it in magento 2.1?
Magento 2.3 Reviews error message - We can't post your review right now
If I click "Submit Review" button, I am getting error : "We can't post your review right now." Have a any idea? Thank you.
How to find products being viewed right now
One of our clients has a specific requirement to show some products(let's say 4 or 5) in the home page that are being viewed at the moment by other users. Your suggestions about how to begin with this would be greatly appreciated. Thanks.
Error message "cannot take that action right now" when system upgrade
I installed Magento 2 on Ubuntu 14.04, and tried to examine whether anything I need to update in Magento system. I enter "Web Setup Wizard" from admin panel, but it shows the error message at the first step. "Sorry, we can't take that action right now." I have tried to find the solution in the official document, but nothing is found.
Magento2: Product created from front-end is not adding to cart,it results "We can't add this item to your shopping cart right now."
However the sample products are adding in cart,this is problem with products that has been added programmatically. Below is the code I am using to create the product programmatically.
We can't add this item to your shopping cart right now on detail page
If someone want to add a product to the cart from the list view it works fine. But from a product detail page the Error appears: We can't add this item to your shopping cart right now.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.