在 WooCommerce 中设置优惠券说明 [英] Set a coupon description in WooCommerce

查看:24
本文介绍了在 WooCommerce 中设置优惠券说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户成为会员足够长的时间,我的网站会动态地向他们提供优惠券.当我生成优惠券时,我想为优惠券指定一个描述.但是,我似乎无法通过使用键 description 作为 docs 表明我应该可以.

My site dynamically gives users coupons if they have been a member for a long enough time. When I am generating the coupon I want to assign a description to the coupon. However, I seem to be unable to assign a description by updating the post's metadata with the key description as the docs suggest I should be able to.

目前我正在尝试分配这样的描述:

Currently I am trying to assign the description like so:

$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');

我还应该如何添加描述?

How else should I go about adding a description?

推荐答案

优惠券描述必须添加到帖子数据中作为 post_excerpt(但是不在后期元数据中)...

The coupon description has to be added in the post data as post_excerpt key (but not in post meta 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');

//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

或者,从 WooCommerce 3 开始,您可以使用任何相关方法WC_Coupon 对象上.在您的情况下,您将使用 setter 方法来设置数据(因为 getter 方法用于获取现有优惠券对象上的数据):

Or instead, since WooCommerce 3, you can use any related method on a WC_Coupon Object. In your case you will use setter methods to set the data (as getter methods are used to get the data on an existing coupon object):

// 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();

这篇关于在 WooCommerce 中设置优惠券说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆