Woocommerce wc_create_order()不起作用

时间:2018-03-20 13:35:43

标签: wordpress woocommerce webhooks

我正在创建一个使用wp简单付费插件处理条带付款的网站。

我创建了一个webhook,可以让我知道付款是否成功。如果成功,我将创建一个包含数据的订单但邮递员总是返回500内部服务器错误,我无法从中看到错误。

如果我删除RegionInfo并返回$地址,则效果非常好。我怀疑我在代码中做错了什么。

这是我创建的代码

wc_create_order()

任何帮助将不胜感激。感谢

2 个答案:

答案 0 :(得分:1)

希望下面的代码对您有所帮助。

function pinion_add_order() {
    global $current_user;
    $a = '100000';
    $order          =   wc_create_order();
    $order->add_product(($a == '100000' ? get_product('2858') : get_product('2859')), 1);
    //$order->add_product( $_product, $item_quantity );
    $order->calculate_totals();

    $fname          =    get_user_meta( $current_user->ID, 'first_name', true );
    $lname          =    get_user_meta( $current_user->ID, 'last_name', true );
    $email          =    $current_user->user_email;
    $address_1      =    get_user_meta( $current_user->ID, 'billing_address_1', true );
    $address_2      =    get_user_meta( $current_user->ID, 'billing_address_2', true );
    $city           =    get_user_meta( $current_user->ID, 'billing_city', true );
    $postcode       =    get_user_meta( $current_user->ID, 'billing_postcode', true );
    $country        =    get_user_meta( $current_user->ID, 'billing_country', true );
    $state          =    get_user_meta( $current_user->ID, 'billing_state', true );

    $billing_address    =   array(
        'first_name' => $fname,
        'last_name'  => $lname,
        'email'      => $email,
        'address_1'  => $address_1,
        'address_2'  => $address_2,
        'city'       => $city,
        'state'      => $state,
        'postcode'   => $postcode,
        'country'    => $country,
    );
    $address = array(
        'first_name' => $fname,
        'last_name'  => $lname,
        'email'      => $email,
        'address_1'  => $address_1,
        'address_2'  => $address_2,
        'city'       => $city,
        'state'      => $state,
        'postcode'   => $postcode,
        'country'    => $country,
    );

    $shipping_cost = 5;
    $shipping_method = 'Fedex';

    $order->add_shipping($shipping_cost);

    $order->set_address($billing_address,'billing');

    $order->set_address($address,'shipping');

    $order->set_payment_method('check');//

    $order->shipping_method_title = $shipping_method;

    $order->calculate_totals();

    $order->update_status('on-hold');

    $order->save();
}
add_action('woocommerce_checkout_process', 'pinion_add_order'); //it does not take any parameters.

有关更多帮助,请参见文件\wp-content\plugins\woocommerce\includes\class-wc-checkout.php中的文件和类结构

答案 1 :(得分:0)

尽管LoicTheAztek是正确的,并且可能有更好的方法,但您的问题的答案是您尚未保存Order对象。

因此,在返回语句之前,请尝试添加$ order-> save();

这实际上会将值保存到数据库中。

如果这不起作用,则可能还需要添加一些其他属性,例如使用$ order-> set_payment_method($ string);的付款方式

希望有帮助。