Title: Fatal error creating coupon programmatically
Last modified: February 18, 2023

---

# Fatal error creating coupon programmatically

 *  Resolved [cmsseni](https://wordpress.org/support/users/cmsseni/)
 * (@cmsseni)
 * [3 years, 3 months ago](https://wordpress.org/support/topic/fatal-error-creating-coupon-programmatically/)
 * When creating a coupon programmatically i get this error:
 *     ```wp-block-code
       [18-Feb-2023 17:54:37 UTC] PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Automattic\WooCommerce\GoogleListingsAndAds\Coupon\SyncerHooks::Automattic\WooCommerce\GoogleListingsAndAds\Coupon\{closure}() must be of the type int, object given, called in /home/XXXXX/public_html/wp-includes/class-wp-hook.php on line 308 and defined in /home/XXXXX/public_html/wp-content/plugins/google-listings-and-ads/src/Coupon/SyncerHooks.php:107
       Stack trace:
       #0 /home/XXXXX/public_html/wp-includes/class-wp-hook.php(308): Automattic\WooCommerce\GoogleListingsAndAds\Coupon\SyncerHooks->Automattic\WooCommerce\GoogleListingsAndAds\Coupon\{closure}(Object(WP_Error), Object(WC_Coupon))
       #1 /home/XXXXX/public_html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
       #2 /home/XXXXX/public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
       #3 /home/XXXXX/public_html/wp-content/plugins/woocommerce/includes/data-stores/class-wc-coupon-data-store-cpt.php(99): do_action('woocommerce_new...', Object(WP_Error), Object(WC_Coupon))
       #4 /home/XXXXX/public_h in /home/XXXXX/public_html/wp-content/plugins/google-listings-and-ads/src/Coupon/SyncerHooks.php on line 107
       ```
   
 * Coupon is created with the following code:
 *     ```wp-block-code
       	$coupon = new WC_Coupon();
       	$coupon->set_code( 'ABC-123' );
       	$coupon->set_description( 'My coupon description' );
       	$coupon->set_discount_type( 'percent' );
       	$coupon->set_amount( 10 );
       	$coupon->set_individual_use( true );
       	$coupon->set_usage_limit( 1 );
       	$coupon->set_usage_limit_per_user( 1 );
       	$coupon->set_limit_usage_to_x_items( 0 );
       	$coupon->set_free_shipping( false );
       	$coupon->set_email_restrictions( 'myemail@gmail.com' );
       	$coupon->set_date_expires( '2023-12-31' );
       	$coupon->set_minimum_amount( 100 );
       	$coupon->save();
       ```
   
 * I was using this with no problems until i installed GoogleListingsAndAds. Disabing
   the plugin allows my function to execute and the coupon to be created.
 * Any thoughts?
 * [EDIT]
   I’m doing this in a script outside the normal WP structure, and including“
   wp-blog-header.php”, “wp-config.php”, and “wp-load.php” in the begining of that
   script. Using similar code in my FUNCTIONS.PHP file i get no errors. What am 
   i missing?!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Saif](https://wordpress.org/support/users/babylon1999/)
 * (@babylon1999)
 * [3 years, 3 months ago](https://wordpress.org/support/topic/fatal-error-creating-coupon-programmatically/#post-16484655)
 * Hello [@cmsseni](https://wordpress.org/support/users/cmsseni/),
 * This is a fairly complex development topic. I’m going to leave it open for a 
   bit to see if anyone is able to chime in to help you out.
 * I can also recommend [the WooCommerce Developer Resources Portal](https://developer.woocommerce.com/)
   for resources on developing for WooCommerce.
 * You can also visit the [WooCommerce Facebook group](https://www.facebook.com/groups/advanced.woocommerce/)
   or the `#developers` channel of the [WooCommerce Community Slack](https://woocommerce.com/community-slack/).
   We’re lucky to have a great community of open-source developers for WooCommerce,
   and many of our developers hang out there, as well.
 * Cheers!
 *  [Joey – a11n](https://wordpress.org/support/users/jricketts4/)
 * (@jricketts4)
 * [3 years, 2 months ago](https://wordpress.org/support/topic/fatal-error-creating-coupon-programmatically/#post-16557888)
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   we’ll be here if and/or when you are ready to continue.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Fatal error creating coupon programmatically’ is closed to new replies.

 * ![](https://ps.w.org/google-listings-and-ads/assets/icon.svg?rev=2775988)
 * [Google for WooCommerce](https://wordpress.org/plugins/google-listings-and-ads/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/google-listings-and-ads/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/google-listings-and-ads/)
 * [Active Topics](https://wordpress.org/support/plugin/google-listings-and-ads/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/google-listings-and-ads/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/google-listings-and-ads/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [Joey – a11n](https://wordpress.org/support/users/jricketts4/)
 * Last activity: [3 years, 2 months ago](https://wordpress.org/support/topic/fatal-error-creating-coupon-programmatically/#post-16557888)
 * Status: resolved