【发布时间】:2018-03-24 06:18:36
【问题描述】:
我想动态生成 woocommerce 优惠券代码。
我的要求是在完成订单后自动在管理员端 woocommerce 优惠券代码列表中为特定产品生成一个优惠券代码。
所以任何人都知道我的上述需求解决方案然后请帮助我。
谢谢, 凯坦。
【问题讨论】:
标签: wordpress plugins woocommerce orders coupon
我想动态生成 woocommerce 优惠券代码。
我的要求是在完成订单后自动在管理员端 woocommerce 优惠券代码列表中为特定产品生成一个优惠券代码。
所以任何人都知道我的上述需求解决方案然后请帮助我。
谢谢, 凯坦。
【问题讨论】:
标签: wordpress plugins woocommerce orders coupon
您可以使用woocommerce_order_status_completed 操作挂钩来完成订单。并使用wp_insert_post为coupan创建帖子类型为shop_coupon的帖子。检查下面的代码
function action_woocommerce_order_status_completed( $order_id ) {
$order = wc_get_order( $order_id );
$order_items = $order->get_items();
// Iterating through each item in the order
$item_quantity=0;
foreach ($order_items as $item_id => $item_data) {
$item_quantity=$order->get_item_meta($item_id, '_qty', true);
if($item_quantity>1){
$product_ids[]=$item_data['product_id'];
$coupon_code = 'UNIQUECODE'.$order_id.$item_id; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // 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',$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' );
unset($product_ids);
}
}
};
// add the action
add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 1 );
【讨论】:
.。请检查编辑后的答案
编写一个函数,当使用woocommerce_order_status_completed 或woocommerce_order_status_processing 钩子完成或下订单时将触发该函数。在函数内部,使用wp_insert_post 创建一个shop_coupon 类型的帖子。使用 wp_insert_post,您可以指定优惠券的标题、金额等。
【讨论】: