woocommence添加订单状态

我们的订单状态默认情况下有以下几种:wc-pending(等待)、wc-processing(处理中)、wc-on-hold(待定)、wc-completed(完成)、wc-cancelled(取消)、wc-refunded(退款)、wc-failed(失败)。

虽然种类很多,但是在有时候并不能完全符合我们的业务,我们可以注册自己的订单状态。

比如添加一个已派送的订单状态:

//初始化已派送订单状态
function register_already_shipped_order_status()
{
	register_post_status('wc-awaiting-shipment', array(
			'label' => 'Shipped',
			'public' => true,
			'exclude_from_search' => false,
			'show_in_admin_all_list' => true,
			'show_in_admin_status_list' => true,
			'label_count' => _n_noop('Awaiting shipment <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>')
	));
}
add_action('init', 'register_already_shipped_order_status');

//将已派送状态添加至订单
function add_already_shipped_to_order_statuses($order_statuses)
{
	$new_order_statuses = array();
	foreach ($order_statuses as $key => $status) {
		$new_order_statuses[$key] = $status;
		if ('wc-processing' === $key) {
			$new_order_statuses['wc-awaiting-shipment'] = 'Shipped';
		}
	}
	return $new_order_statuses;
}
add_filter('wc_order_statuses', 'add_already_shipped_to_order_statuses');

同时,如果你需要自动发送邮件,你需要查看woocommerce_email_classes钩子,已完成自动发邮件的目的。

或者使用另外的钩子,自己发送邮件:

function custom_send_email_notifications( $order_id, $old_status, $new_status, $order ){
    if ( $new_status == 'cancelled' || $new_status == 'failed' ){
        $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
        $customer_email = $order->get_billing_email(); // The customer email
    }

    if ( $new_status == 'cancelled' ) {
        // change the recipient of this instance
        $wc_emails['WC_Email_Cancelled_Order']->recipient = $customer_email;
        // Sending the email from this instance
        $wc_emails['WC_Email_Cancelled_Order']->trigger( $order_id );
    }
    elseif ( $new_status == 'failed' ) {
        // change the recipient of this instance
        $wc_emails['WC_Email_failed_Order']->recipient = $customer_email;
        // Sending the email from this instance
        $wc_emails['WC_Email_failed_Order']->trigger( $order_id );
    }
}
//add_action('woocommerce_order_status_changed', 'custom_send_email_notifications', 10, 4 );
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

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

    暂无评论内容