在WooCommerce中,有一个订单状态改变的钩子,这个钩子叫做:woocommerce_order_status_changed。
这个钩子有很大的用途,像网上那些积分插件、分销插件,底层就是这个钩子处理订单的。或者你还想在支付完成做一些事情,都可以使用这个钩子进行处理;或者对购买某商品的用户发送特定优惠券;订单金额超过某数值时发送优惠券;使用了某张优惠券后再次处理某些事情等都可以用这个钩子进行处理。可以这么说,订单相关的逻辑,很多都可以使用这个钩子。
我是在我们公司有个特定商品购买成功就发送优惠券给用户,我的代码是这样的。
// 当订单变为processing时,判断生成优惠券add_action( 'woocommerce_order_status_changed', 'yinjiangbi_createRecyclingCoupons', 9, 4 );function yinjiangbi_createRecyclingCoupons($order_id, $from_status, $to_status, $order_obj){if (!empty($order_id)) {$order_status = $order_obj->get_status();//获取订单状态$payment_id = $order_obj->get_payment_method(); //支付方式ID$order_items = $order_obj->get_items();// 订单项// 处理添加优惠券$has_add_coupon = false;foreach ( $order_items as $item ) {$total = $item->get_total();$product = $item->get_product();$product_id = $product->get_id();if($product_id == 164442){//包含产品$has_add_coupon = true;break;}}$add_homecoming_counpon = get_post_meta( $order_id, 'add_homecoming_counpon' , true );if($add_homecoming_counpon == "yes"){ // 之前已经发过了$has_add_coupon = false;}if($has_add_coupon and $order_status == "processing"){// 生成三张优惠券,生成的代码就不放了,下一篇文章介绍。yingjiangbi_generate_fixed_cart_coupon();// 生成优惠券添加描述$order_obj->add_order_note( 'Generate Homecoming Coupon Successfully.' );//添加订单描述update_post_meta( $order_id, 'add_homecoming_counpon', 'yes' );// 添加标记,防止再次添加优惠券}}}// 当订单变为processing时,判断生成优惠券 add_action( 'woocommerce_order_status_changed', 'yinjiangbi_createRecyclingCoupons', 9, 4 ); function yinjiangbi_createRecyclingCoupons($order_id, $from_status, $to_status, $order_obj){ if (!empty($order_id)) { $order_status = $order_obj->get_status();//获取订单状态 $payment_id = $order_obj->get_payment_method(); //支付方式ID $order_items = $order_obj->get_items();// 订单项 // 处理添加优惠券 $has_add_coupon = false; foreach ( $order_items as $item ) { $total = $item->get_total(); $product = $item->get_product(); $product_id = $product->get_id(); if($product_id == 164442){//包含产品 $has_add_coupon = true; break; } } $add_homecoming_counpon = get_post_meta( $order_id, 'add_homecoming_counpon' , true ); if($add_homecoming_counpon == "yes"){ // 之前已经发过了 $has_add_coupon = false; } if($has_add_coupon and $order_status == "processing"){ // 生成三张优惠券,生成的代码就不放了,下一篇文章介绍。 yingjiangbi_generate_fixed_cart_coupon(); // 生成优惠券添加描述 $order_obj->add_order_note( 'Generate Homecoming Coupon Successfully.' );//添加订单描述 update_post_meta( $order_id, 'add_homecoming_counpon', 'yes' );// 添加标记,防止再次添加优惠券 } } }// 当订单变为processing时,判断生成优惠券 add_action( 'woocommerce_order_status_changed', 'yinjiangbi_createRecyclingCoupons', 9, 4 ); function yinjiangbi_createRecyclingCoupons($order_id, $from_status, $to_status, $order_obj){ if (!empty($order_id)) { $order_status = $order_obj->get_status();//获取订单状态 $payment_id = $order_obj->get_payment_method(); //支付方式ID $order_items = $order_obj->get_items();// 订单项 // 处理添加优惠券 $has_add_coupon = false; foreach ( $order_items as $item ) { $total = $item->get_total(); $product = $item->get_product(); $product_id = $product->get_id(); if($product_id == 164442){//包含产品 $has_add_coupon = true; break; } } $add_homecoming_counpon = get_post_meta( $order_id, 'add_homecoming_counpon' , true ); if($add_homecoming_counpon == "yes"){ // 之前已经发过了 $has_add_coupon = false; } if($has_add_coupon and $order_status == "processing"){ // 生成三张优惠券,生成的代码就不放了,下一篇文章介绍。 yingjiangbi_generate_fixed_cart_coupon(); // 生成优惠券添加描述 $order_obj->add_order_note( 'Generate Homecoming Coupon Successfully.' );//添加订单描述 update_post_meta( $order_id, 'add_homecoming_counpon', 'yes' );// 添加标记,防止再次添加优惠券 } } }
© 版权声明
THE END
暂无评论内容