【问题标题】:Woocommerce wc_create_order() not workingWoocommerce wc_create_order()不起作用
【发布时间】:2018-03-20 13:35:43
【问题描述】:

我正在创建一个网站,它使用 wp simple pay 插件处理来自 Stripe 的支付。

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

如果我删除 wc_create_order() 并返回 $address,它会完美运行。我怀疑我的代码做错了什么。

这是我创建的代码

add_action('woocommerce_checkout_process', 'pinion_add_order');
function pinion_add_order($m, $a) {
    global $woocommerce;
    $address = array(
        'first_name' => 'Project Paid ',
        'last_name'  => $m
    );

    $order = wc_create_order();
    $order->add_product(($a == '100000' ? get_product('2858') : get_product('2859')), 1);
    $order->set_address($address, 'billing');
    $order->set_address($address, 'shipping');
    // $order->set_total($amount);
    $order->calculate_totals();
    $order->update_status("Completed", 'Imported order', TRUE);  

    return $order;
}

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

【问题讨论】:

  • 您不应该在 WC_Checkout 钩子中以这种方式将 wp simple pay 插件与 woocommerce 一起使用……这永远不会以干净的方式工作。您应该更好地使用 Woocommerce Stripe 插件或在 stackOverFlow 中搜索以编程方式创建订单(因为这已经得到了很多回答)...WC_Checkout 方法和钩子可以与 Woocommerce 集成支付方式或/和网关一起使用...

标签: wordpress woocommerce webhooks


【解决方案1】:

虽然 LoicTheAztek 是正确的并且可能有更好的方法来做到这一点,但您的问题的答案是您尚未保存 Order 对象。

所以在您的退货声明之前,请尝试添加 $order->save();

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

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

希望对您有所帮助。

【讨论】:

    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 2015-02-18
      • 2015-05-05
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 2015-01-06
      • 1970-01-01
      相关资源
      最近更新 更多