-1

How can I check that product is able to coupon apply? I need the coupon id or other coupon data in shop loop items of the woocommerce and product details of the woocommerce.

Any coupon data is stored them in the product details like in the product meta information so we can check it directly?

Thanks

After 3 Downvotes Need to more clarify the Question.

In my WooCommerce store, I have created the many coupons for the selling products but When new customer comes to my website they do not know if there are any coupons available for this xyz product. So, I need to display the flag like "Special Offer" in the shop loop of the products and product details page If the products discountable by the coupon code.

Generally, what happened the new customer does not know about the coupon code and in woocommerce there is no functionality for displaying the flag for the product is discountable if there is the coupon for that products.

I hope now all can understand the question. So, Please do let me know if you have any suggestions or inputs about the same.

Thanks again.

Ajay Ghaghretiya
  • 784
  • 1
  • 6
  • 30
  • 2
    Share you code so we can better give you some suggestions.Thanks – Peter Jul 31 '18 at 07:54
  • I have not done any code yet now for this. I just want the coupon detail by the product id something like from product meta table. – Ajay Ghaghretiya Jul 31 '18 at 07:58
  • Why the people have unvoted the question. If you want to unvoted the question then please give the proper comment about the question. – Ajay Ghaghretiya Jul 31 '18 at 08:55
  • I didn't down voted…Now your question **is not clear**. A product can allow some coupons and some others no, depending on their respective settings. So the only thing that you can really check is the applied coupons. So you should try to clarify your question. – LoicTheAztec Jul 31 '18 at 09:04

2 Answers2

0

Get product IDs this coupon can apply to.

$array = WC_Coupon::get_product_ids();

Further reading docs

Further more to get coupon details see below

$coupon_code = '33percent';

$c = new WC_Coupon($coupon_code);

You will get following details

echo "Discount Amount ".$c->amount."<br>";//Get Discount amount
echo "Discount Type ".$c->discount_type."<br>";//Get type of discount
echo "Individual Use ".$c->individual_use."<br>";//Get individual use status
echo "Usage Count ".$c->usage_count."<br>";//Get number of times the coupon has been used
echo "Uage Limit ".$c->usage_limit."<br>";//Get usage limit
echo "Coupon Description ".$c->description."<br>";//Get coupon description

I hope it helps.

Peter
  • 777
  • 2
  • 13
  • 34
0

Try this code

$Product_ID=82; /// Assig the product ID here.

        $querystr = "
            SELECT $wpdb->postmeta.meta_value,$wpdb->posts.post_name 
            FROM $wpdb->posts, $wpdb->postmeta
            WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
            AND $wpdb->postmeta.meta_key = 'product_ids' 
            AND $wpdb->posts.post_status = 'publish' 
            AND $wpdb->posts.post_type = 'shop_coupon'
            ORDER BY $wpdb->posts.post_date DESC";

         $pageposts = $wpdb->get_results($querystr, OBJECT);
         $pageposts = json_decode(json_encode($pageposts),true);


         foreach($pageposts as $coupon_product_Ids)
         {

                if($coupon_product_Ids['meta_value']!='')
                {
                        $coupon_data=explode(",",$coupon_product_Ids['meta_value']);


                        if(in_array($Product_ID, $coupon_data))
                        {
                                $Product_Available_Coupon_code[]=$coupon_product_Ids['post_name'];
                        }

                }
         }

        if(sizeof($Product_Available_Coupon_code)>0)
        {

            foreach($Product_Available_Coupon_code as $coupon_code_value)
            {

                echo $coupon_code_value; echo "<br>";
            }

        }
        else
        {
            echo 'No counpons Available'; 

        }
VinothRaja
  • 1,405
  • 10
  • 21