Topic Resolution: Resolved
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #735
    info17
    Participant

    How to obtain coupon info in functions?

    i.e.
    Coupon: Buy *** 4 Get *** free

    I’d like to get *** and 4(number) to show a message on product page.
    number be dynamic depend on items in the cart so static message is not I want.

    #736
    Soft79
    Keymaster

    Minimum amount of matching items: get_post_meta( $coupon_id, '_wjecf_min_matching_product_qty', true ) (Where $coupon_id is the id of the coupon)

    Ids of the products that must be matched: $coupon->product_ids (Where $coupon is the WC_Coupon object)

    Free product ids: WJECF_API()->get_coupon_free_product_ids( $coupon ) (Where $coupon is the WC_Coupon object, coupon id or coupon code)

    #739
    info17
    Participant

    Thank you.
    But in order to match product id with coupon, I have to gather all coupon data but single coupon with code.

    #740
    Soft79
    Keymaster

    Yes, you’d have to query all existing coupons for that.

    See https://www.skyverge.com/blog/get-a-list-of-all-woocommerce-coupons/.

    #743
    info17
    Participant

    Thank you.

    Is there anyway I can set certain product as free gift dynamically through a hook?
    I want to change the product for free gift programmatically depend on situation but user select manually.

    #745
    Soft79
    Keymaster

    Hi,

    the product id’s are retrieved using this call: get_post_meta( $coupon_id, '_wjecf_free_product_ids', true ). It will go through the filter apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single ); in wp-includes/post.php.

    So you can probably use the following snippet to manipulate the product ids:

    
    add_filter( 'get_post_metadata', 'my_get_post_metadata', 10, 4 );
    function my_get_post_metadata ( $meta_value, $object_id, $meta_key, $single ) {
      if ( $meta_key == '_wjecf_free_product_ids' ) {
        //EXAMPLE: replace the product id by 123
        $meta_value = 123;
      }
      return $meta_value;
    }
    

    You can use $object_id to retrieve the WC_Coupon object. (It’s the coupon id)

    #748
    info17
    Participant

    I’d like to use Auto Coupon option but I want to let customer select free gift manually on other page than cart.
    (So I don’t want to show gift selection element on cart page)
    But I want free goods that is single selection to be added automatically.

    How to configure this?

    #749
    Soft79
    Keymaster

    The free gift is saved in the session. You can read the currently selected free item using:

    WJECF_API()->get_session_selected_product( $coupon_code )

    Setting the free item is not yet in the API of the plugin, but you can use the following method:

    WJECF()->get_plugin('WJECF_Pro_Free_Products')->set_session_selected_product( $coupon_code, $product_id )

    #750
    info17
    Participant

    Thank you.
    How do I disable free gift selection on cart page?
    Modify /template/cart/select-free-product.php?

    #751
    Soft79
    Keymaster

    You can override that file by placing it in wp-content/themes/YOUR-CHILD-THEME/woocommerce-auto-added-coupons/cart/select-free-product.php. Just place an empty file there.

    Do the same for wp-content/themes/YOUR-CHILD-THEME/woocommerce-auto-added-coupons/checkout/select-free-product.php .

    Make sure a free gift has been selected upon checkout. Otherwise the user can’t checkout.

    #773
    info17
    Participant

    Is it possible to restrict rule to be applied to certain product using a hook?

    I’m using this extension of WooCommerce Subscription
    woocommerce-subscribe-all-the-things
    https://github.com/Prospress/woocommerce-subscribe-all-the-things

    Since this extension handles regular product and subscription as one but variation, I can not filter out from GUI.
    I need to check see if cart item has this value:
    cart[id][wccsub_data][active_subscription_scheme_id]

    #775
    Soft79
    Keymaster

    Use the woocommerce_coupon_is_valid filter and return false to invalidate the coupon if the product is not in the cart.

    http://hookr.io/filters/woocommerce_coupon_is_valid/

    #778
    info17
    Participant

    Thank you.
    But that filter apparently disables coupon itself if there is any match item.
    So if customer have both a regular product and a subscription product in the cart, coupon will not be applied to any item.

    #779
    info17
    Participant

    another filter woocommerce_coupon_is_valid_for_product worked!

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.