绑定到WooCommerce_Payment_Complete_Order_Status的函数会阻止订单状态为“正在处理”

时间:2018-10-10 作者:warm__tape

我正在将一个函数绑定到woocommerce_payment_complete_order_status 它根据优惠券是否用于下订单创建自定义贴子。代码一切正常-创建帖子,付款完成,网站返回thankyou页面,但订单在等待付款时被卡住。因此,不知何故,运行此函数会阻止从等待付款状态到处理订单状态的移动,因此最终会过期并取消订单。

知道为什么吗?也许是个更好的地方?

以下是完整代码:

function create_fundraiser_purchase_post( $order_status, $order_id ) {

$order = new WC_Order( $order_id );

// Order Data
$order_data = $order->get_data();

// Coupon Data
if( $order->get_used_coupons() ) {
    foreach( $order->get_used_coupons() as $coupon) {
        $coupons_list .= $coupon;
    }
}

// Only make the post if the coupon is for a school 
if (strpos($coupons_list, \'school\') !== false) {

    $order_id = $order_data[\'id\'];
    $order_date_created = $order_data[\'date_created\']->date(\'F j, Y\');

    // Break up code into blocks for sorting
    $cleaned_coupon = str_replace("school","",$coupons_list);
    $pieces = explode("-", $cleaned_coupon);
    $school_id = $pieces[0];
    $class_id = $pieces[1];
    $student_id = $pieces[2];

    $school_code = $school_id;
    $class_code = $school_id . \'-\' . $class_id;
    $student_code = $cleaned_coupon;

    // Make nice title
    $title = \'Fundraiser Purchase \' . $order_id . \' \' . $cleaned_coupon;

    $order_shipping_total = $order_data[\'shipping_total\'];
    $order_total_tax = $order_data[\'total_tax\'];
    $total = $order_data[\'total\'];

    $purchase_value = ( $total - $order_total_tax - $order_shipping_total ) * 0.35;

    $user_id = $order->user_id;

    // Add Woocommerce meta
    $order = wc_get_order( $order_id );
    $order->update_meta_data( \'wpcf-purchase-student\', $student_code );
    $order->update_meta_data( \'wpcf-purchase-value\', $purchase_value );
    $order->save();

    $fundraiser_post = array(
        \'post_title\' => $title,
        \'post_content\' => \'\',
        \'post_status\' => \'publish\',
        \'post_type\' => \'fundraiser-purchase\',
        \'meta_input\' => array(
            \'wpcf-purchase-date\' => $order_date_created,
            \'wpcf-purchase-school\' => $school_code,
            \'wpcf-purchase-class\' => $class_code,
            \'wpcf-purchase-student\' => $student_code,
            \'wpcf-purchase-value\' => $purchase_value,
            \'wpcf-purchase-user-id\' => $user_id,
            \'wpcf-purchase-order-id\' => $order_id,
        ),
    );

    post_exists( $fundraiser_post ) or wp_insert_post( $fundraiser_post );

}
add_action( \'woocommerce_payment_complete_order_status\', \'create_fundraiser_purchase_post\', 10, 2 );

1 个回复
最合适的回答,由SO网友:Rudy Lister 整理而成

挂起的原因很可能是因为您使用的挂钩不完整。woocommerce\\u payment\\u complete\\u order\\u status是筛选器的挂钩,但您正在使用add\\u操作。add\\u操作的挂钩应该是woomcommerce\\u payment\\u complete\\u order\\u status\\u{$get\\u status}

筛选器的资源http://hookr.io/plugins/woocommerce/3.0.6/filters/woocommerce_payment_complete_order_status/

apply_filters( \'woocommerce_payment_complete_order_status\', \'create_fundraiser_purchase_post\', 10, 2 );
行动资源http://hookr.io/plugins/woocommerce/3.0.6/actions/woocommerce_payment_complete_order_status_get_status/

add_action( \'woocommerce_payment_complete_order_status_{$get_status}\', \'create_fundraiser_purchase_post\', 10, 2 );

结束

相关推荐