在WooCommerce中设置优惠券说明

时间:2017-11-17 23:41:07

标签: php wordpress woocommerce coupon

我的网站动态为用户提供优惠券,如果他们已成为会员足够长的时间。当我生成优惠券时,我想为优惠券分配说明。但是,我似乎无法通过使用密钥description更新帖子的元数据来指定说明,因为docs建议我应该能够。

目前我正在尝试分配如下说明:

$percent = 25;//DISCOUNT PERCENTAGE
$coupon_code = 'testcoupon'; //Coupon Code
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product

//ASSIGN COUPON AND DISCOUNT PERCENTAGE

$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 );//SET DICOUNT TO BE PERCENTAGE BASED
update_post_meta( $new_coupon_id, 'coupon_amount', $percent );//SET DISCOUNT PERCENTAGE
update_post_meta( $new_coupon_id, 'individual_use', 'yes' );//ONLY ONE CUPON BE USED AT A TIME
update_post_meta( $new_coupon_id, 'product_ids', '' ); //INCLUDE ALL PRODUCTS
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );//DO NOT EXCLUDE ANY PRODUCTS 
update_post_meta( $new_coupon_id, 'usage_limit', '1' );//ONE TIME USE
update_post_meta( $new_coupon_id, 'expiry_date', strtotime("+6 months") );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );//DO NOT GIVE FREE SHIPPING

//ASSIGN DESCRIPTION TO COUPON
update_post_meta( $new_coupon_id, 'description', 'This is an example description used for the example coupon');

我还应该如何添加说明?

1 个答案:

答案 0 :(得分:3)

优惠券说明必须在帖子数据中添加为 post_excerpt (但不包含在后元数据中) ...

所以你的代码应该是:

$percent = 25;//DISCOUNT PERCENTAGE
$coupon_code = 'testcoupon'; //Coupon Code
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product
$description = __('This is an example description used for the example coupon');

//ASSIGN COUPON AND DISCOUNT PERCENTAGE

$coupon = array(
    'post_title' => $coupon_code,
    'post_content' => '',
    'post_excerpt' => $description, // <== HERE goes the description
    'post_status' => 'publish',
    'post_author' => 1,
    'post_type'     => 'shop_coupon'
);

$new_coupon_id = wp_insert_post( $coupon );

## … / … and so on

或者您可以改为使用WC_Coupon Object and methods

// Get an instance of the WC_Coupon object
$wc_coupon = new WC_Coupon($coupon_code);

// Some data
$percent = 25; // DISCOUNT PERCENTAGE
$coupon_code = 'testcoupon'; // Coupon Code
$discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product
$description = __('This is an example description used for the example coupon'); // Description

// Set the coupon data
$wc_coupon->set_code($coupon_code);
$wc_coupon->set_description($description);
$wc_coupon->set_discount_type($discount_type);
$wc_coupon->set_amount( floatval($percent) );
$wc_coupon->set_individual_use( true );
$wc_coupon->set_usage_limit( 1 );
$wc_coupon->set_date_expires( strtotime("+6 months") );
## $wc_coupon->apply_before_tax( true ); // ==> Deprecated in WC 3+ with no replacement alternatie
$wc_coupon->set_free_shipping( false );

// Test raw data output before save
var_dump($wc_coupon);

// SAVE the coupon
$wc_coupon->save();