Skip Navigation

[Resolved] How can I add a coupon to the cart automatically

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by guillermoG-2 6 years, 3 months ago.

Assigned support staff: Shane.

Author
Posts
#316137

I want to give different discounts according to the quantities of a product that I have, I want to use the coupons for that.
So How can I add a coupon to the cart automatically after filling the cred form and appears in the checkout form?

I think that I can use cred_commerce_after_add_to_cart hook but really don't know which commands need it to do it.

Let's assume that I've created a coupon ABCD, how do I insert it with shortcodes or hooks in the payment form automatically

Thanks in advance

#316144

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Guillermo,

Thanks for contacting our support forum.

I took a look at this and went ahead and did some research on it.

It seems there is a plugin available that can do this.

Try using the plugin in the link below.
https://wordpress.org/plugins/woocommerce-auto-added-coupons/

Please let me know if this helps.

Thanks

#316178

Hi Shane,

Thanks for your response.
The plugin you recommend me looks great, but in my case the purchase must fulfill several conditions to apply a coupon that can not be configured in this plugin. the conditions are somewhat limited.

If you do not mind I would like to know if this change can be done with cred, in essence it is almost the same result that I need.

I prefer do it with cred than modify that plugin.

Thanks

#316388

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Guillermo,

This should be possible with the use of our hooks.

Unfortunately this seems to be a highly custom solution. currently custom solutions are out of the scope of our forum support.

For a more advanced level of support and custom development I would recommend that you contact our custom development team that can be found by going to the link below.
hidden link

Thanks

#316543

Hi Shane,

I find this hook of woocommerce
add_action( 'woocommerce_after_checkout_form', 'apply_matched_coupons' );
function apply_matched_coupons() {
$coupon_code = 'abcdg'; // your coupon code here
$woocommerce->cart->add_discount( $coupon_code );
wc_print_notices();
}
it works perfect but I need to apply some conditions
Now my question is how to do this:

add_action( 'woocommerce_after_checkout_form', 'apply_matched_coupons' );
function apply_matched_coupons() {

if ( [custom_field_apply_coupon] == 'Yes' ){ // <-- [custom_field_apply_coupon] that I use in my cred form
$coupon_code = 'abcdg'; // your coupon code here
$woocommerce->cart->add_discount( $coupon_code );
wc_print_notices();
}
}

or how to pass some variable that use in my cred form?

Thanks a lot for your help

#316743

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Guillermo,

I'm happy that you were able to find a hook to use for this.

You should be able to do this with the CRED Hook. The reason is that you need to get some values from our CRED form and that can only be done when using our CRED Hooks.

So we can check the conditions of the Custom Fields by getting the post values with.

$val = $_POST[wpcf-custom-field-name];

Note you will need to the wpcf- prefix before the slug on the custom field you will like to check.

So an example of this would be below.

add_action('cred_commerce_after_add_to_cart','my_hook',10,2);
function my_hook($form_id, $post_id)
{
$val = $_POST[wpcf-custom-field-name];

if ( $val == 'Yes' ){ // <-- [custom_field_apply_coupon] that I use in my cred form
$coupon_code = 'abcdg'; // your coupon code here
$woocommerce->cart->add_discount( $coupon_code );
wc_print_notices();
}
}

Please let me know if this was able to guide you in the correct direction.

Thanks

#320215

Thanks Shane,

After serveral test this is what works to me.

add_action( 'woocommerce_after_checkout_form', 'apply_matched_coupons' );
function apply_matched_coupons() {
    global $woocommerce;
    $coupon_code = 'abcdg'; // your coupon code here
	$woocommerce->cart->remove_coupon( $coupon_code );
	if ( $_SESSION['cupon']->cupon == 'SI' ){
		$woocommerce->cart->add_discount( $coupon_code );
	}
	wc_print_notices();
}

thank you very mucho for your help