WooCommerce中使用代码创建优惠券

前面一篇文章,我使用代码创建了优惠券,如果放到一起会变得杂乱、不全面,所以单独拉出一个文章,讲讲如何使用代码创建优惠券。

我们回到优惠券的本质,其实优惠券也是文章类型,储存在wp_posts表中,一些其他设置保存在wp_post_meta表中。所以,创建优惠券能使用创建文章那样的操作,同时WooCommerce也有自己封装的一套函数供我们使用。最终,创建优惠券其实有两种方式:

1、使用WordPress自带的新建文章方式

// 创建一个新的优惠券数组
$coupon_data = array(
	'post_title' => 'Custom Coupon', // 优惠券标题
	'post_content' => '', // 优惠券内容
	'post_status' => 'publish', // 发布状态
	'post_author' => 1, // 作者ID,默认为管理员
	'post_type' => 'shop_coupon' // 优惠券的自定义类型
);

// 插入新的优惠券
$coupon_id = wp_insert_post( $coupon_data );

// 设置优惠券的元数据
update_post_meta( $coupon_id, 'discount_type', 'fixed_cart' ); // 折扣类型:固定金额(其他可能值:percent、fixed_product、percent_product)
update_post_meta( $coupon_id, 'coupon_amount', '10' ); // 优惠券金额:10
update_post_meta( $coupon_id, 'free_shipping', 'yes' ); // 允许免运费
update_post_meta( $coupon_id, 'expiry_date', strtotime( '+1 month' ) ); // 优惠券过期时间:一个月后

update_post_meta( $coupon_id, 'minimum_amount', '50' ); // 最低花费金额:50
update_post_meta( $coupon_id, 'maximum_amount', '200' ); // 最高花费金额:200
update_post_meta( $coupon_id, 'individual_use', 'yes' ); // 仅限单独使用
update_post_meta( $coupon_id, 'exclude_sale_items', 'yes' ); // 不含促销产品

update_post_meta( $coupon_id, 'product_ids', array( 10, 20, 30 ) ); // 产品ID
update_post_meta( $coupon_id, 'exclude_product_ids', array( 40, 50, 60 ) ); // 排除产品ID
update_post_meta( $coupon_id, 'product_categories', array( 'clothing', 'electronics' ) ); // 产品类别
update_post_meta( $coupon_id, 'exclude_product_categories', array( 'books', 'music' ) ); // 排除类别

update_post_meta( $coupon_id, 'email_restrictions', array( 'user1@example.com', 'user2@example.com' ) ); // 允许的电子邮件

update_post_meta( $coupon_id, 'usage_limit', '100' ); // 每个优惠券的使用次数限制:100
update_post_meta( $coupon_id, 'limit_usage_to_x_items', '2' ); // 限制X个物品使用:每个订单只能使用2个物品
update_post_meta( $coupon_id, 'usage_limit_per_user', '5' ); // 每个用户的使用次数限制:每个用户最多使用5次

// 输出优惠券ID以供检查
echo '优惠券已创建,ID为:' . $coupon_id;

2、使用WooCommerce封装的函数

也就是使用WooCommerce的Coupon类

// 导入WooCommerce Coupon类
if ( ! class_exists( 'WC_Coupon' ) ) {
    include_once WC()->plugin_path() . '/includes/class-wc-coupon.php';
}

// 创建一个优惠券对象
$coupon = new WC_Coupon();

// 设置基本信息
$coupon->set_code( 'CUSTOMCODE' ); // 设置优惠券代码
$coupon->set_discount_type( 'fixed_cart' ); // 折扣类型:固定金额(其他可能值:percent、fixed_product、percent_product)
$coupon->set_amount( 10 ); // 优惠券金额:10
$coupon->set_free_shipping( true ); // 允许免运费
$coupon->set_expiry_date( strtotime( '+1 month' ) ); // 优惠券过期时间:一个月后

// 设置使用限制
$coupon->set_minimum_amount( 50 ); // 最低花费金额:50
$coupon->set_maximum_amount( 200 ); // 最高花费金额:200
$coupon->set_individual_use( true ); // 仅限单独使用
$coupon->set_exclude_sale_items( true ); // 不含促销产品

// 设置产品和产品类别的限制
$coupon->set_product_ids( array( 10, 20, 30 ) ); // 产品ID
$coupon->set_excluded_product_ids( array( 40, 50, 60 ) ); // 排除产品ID
$coupon->set_product_categories( array( 'clothing', 'electronics' ) ); // 产品类别
$coupon->set_excluded_product_categories( array( 'books', 'music' ) ); // 排除类别

// 设置允许使用的邮箱
$coupon->set_email_restrictions( array( 'user1@example.com', 'user2@example.com' ) ); // 允许的电子邮件

// 设置使用次数限制
$coupon->set_usage_limit( 100 ); // 每个优惠券的使用次数限制:100
$coupon->set_limit_usage_to_x_items( 2 ); // 限制X个物品使用:每个订单只能使用2个物品
$coupon->set_usage_limit_per_user( 5 ); // 每个用户的使用次数限制:每个用户最多使用5次

// 将优惠券保存到数据库中
$coupon->save();

// 输出优惠券代码以供检查
echo '优惠券已创建,代码为:' . $coupon->get_code();
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称常用语 夸夸
夸夸
还有吗!没看够!
表情代码图片

    暂无评论内容