最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Keep getting Notice: Trying to get property of non-object in ...wp-includespost.php on line 4153 when generating woocommerce cou

programmeradmin2浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论