1

I'm getting this error, that I can't get rid of. Here is my code:

function generate_coupon($coupon_name){
  global $post;
  $coupon_code = $coupon_name; // Code
  $amount = '10'; // Amount
  $discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product

  $coupon = array(
    'post_title' => $coupon_code,
    'post_content' => '',
    'post_status' => 'publish',
    'post_author' => 1,
    'post_type'     => 'shop_coupon'
  );

  $new_coupon_id = wp_insert_post( $coupon );

  // Add meta
  update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
  update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
  update_post_meta( $new_coupon_id, 'individual_use', 'no' );
  update_post_meta( $new_coupon_id, 'product_ids', '' );
  update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
  update_post_meta( $new_coupon_id, 'usage_limit', '' );
  update_post_meta( $new_coupon_id, 'expiry_date', '' );
  update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
  update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

}
generate_coupon('00112233');

Any help is much appreciated.

5
  • your code works well with last versions of WordPress and WooCommerce. maybe another plugin is creating this error.
    – Kaperto
    Commented Nov 15, 2019 at 12:37
  • Just to make sure we're looking at the same line 4153, which version of the WordPress do you have? If it's 5.1.x or 5.2.x then I make line 4153 $feeds = $wp_rewrite->feeds; so the problem is that the $wp_rewrite global isn't available in wp_unique_post_slug. Does that sound plausible?
    – Rup
    Commented Nov 15, 2019 at 12:48
  • Where is this code? How are you calling it? Is it potentially in an environment where WP_Rewrite hasn't been initialised?
    – Rup
    Commented Nov 15, 2019 at 12:54
  • I'm using the latest version of wordpress and woocommerce and still getting that error. I have tried disabling plugins, that didn't work, however, what worked is placing this code in functions.php, but I can't have that. Commented Nov 15, 2019 at 13:09
  • This code is with in a custom plugin Commented Nov 15, 2019 at 13:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.