• Resolved Radical Dreamer

    (@avagp)


    I was wondering if i could set the variables of a smart coupon via PHP.

    For example i have this in my code already:

                $coupon_code = substr($author_email,0,4).'4LUCAS'.random_int( 1000, 9999 ); // OR generate_coupon_code();
                $discount_type = 'fixed_cart'; // type can be 'fixed_cart', 'percent' or 'fixed_product', defaults to 'fixed_cart'
                $coupon_amount = '4000'; // Amount
                $coupon->set_code( $coupon_code );
                $coupon->set_discount_type( $discount_type );
                $coupon->set_amount( $coupon_amount );
                $coupon->set_individual_use( true );
                $coupon->set_usage_limit( 1 );
                $coupon->set_email_restrictions( array( $author_email ) );
                $coupon->set_date_expires( strtotime( '+60 days' ) );
                $coupon->set_free_shipping( false );

    Is there a way i can set other variables, like:

    • Giveaway product
    • Automatically apply
    • Display in checkout/My Account
    • Category of the products needed to be applied
    • And so many other variables that smart coupons give to the woo coupons
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support WebToffee Support

    (@webtoffeesupport)

    Hi @avagp,

    Greetings from WebToffee support. Thank you for reaching out to us.

    To help us better assist you with this requirement please share the version number of the Smart Coupons for Woocommerce plugin you are using.

    We are awaiting your response.

    Plugin Support WebToffee Support

    (@webtoffeesupport)

    Hi @avagp,

    Thank you for your patience.

    You may add these meta to the coupon

    1. _wt_free_product_ids – The giveaway product IDs can be added as an array.
    2. _wt_make_auto_coupon – Set the value to ‘yes’ to enable auto apply
    3. _wc_make_coupon_available – To display coupon in My account, Cart & Checkout. Set values as array. Applicable values: ‘my_account’, ‘cart’ & ‘checkout’

    The category restriction is provided by WooCommerce. Therefore, you may use the below code to set the category.

    1. $coupon->set_product_categories( [ $category_id1, $category_id2 ] ). Add IDs of the categories on which the coupon should be applied
    Thread Starter Radical Dreamer

    (@avagp)

    thanks.

    so, to understand, should be used like this?

    $coupon->_wc_make_coupon_available

    Plugin Support WebToffee Support

    (@webtoffeesupport)

    Hi @avagp,

    No. That is for configuring the default WooCommerce coupon data.

    You may use the following code for reference to update the metadata of the coupon:
    update_post_meta(coupon_id, ‘meta_name’, meta_value);

    For more information on updating the post meta, you may refer to this article: update_post_meta() | Function

    Thread Starter Radical Dreamer

    (@avagp)

    thanks!

    every have worked but

    update_post_meta($coupon_id, '_wc_make_coupon_available', ['my_account','cart','checkout']);

    what it is incorrect in this?

    Thread Starter Radical Dreamer

    (@avagp)

    I figured it out!

    update_post_meta($coupon_id, '_wc_make_coupon_available', 'my_account,cart,checkout');

    For anyone else, you can see this example: https://gist.github.com/webtoffee-git/990524a83c7c6f02559e501d53136b85

    And you can check webtofee documentation: https://www.webtoffee.com/bulk-import-coupons-using-csv-file-coupon-data/

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to create a smart coupon in PHP’ is closed to new replies.