WooCommerce 以编程方式创建订单并重定向到付款 [英] WooCommerce create an order programmatically and redirect to payment

查看:35
本文介绍了WooCommerce 以编程方式创建订单并重定向到付款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 WooCommerce,我正在寻找一种以编程方式创建订单的解决方案(我的网站只有 1 个带有某些字段的主页)进行订购.

For WooCommerce i'm lookin' for a solution to create an order programmaticly (my site just has 1 homepage with some fields) to order.

添加带有复选框的产品后,我想创建一个订单并重定向到付款方式.

After products are added with a checkbox i'd like to create an order and redirect to the payment method.

这个答案几乎完成了创建订单,但我如何开始付款?Wordpress(Woocommerce 扩展程序)- 以编程方式创建新订单

Creating an order is almost done with this answer, but how do i start a payment? Wordpress (Woocommerce extension) - Create new order programatically

推荐答案

这对我来说:

if (isset($_POST['isOrder']) && $_POST['isOrder'] == 1) {
    $address = array(
        'first_name' => $_POST['notes']['domain'],
        'last_name'  => '',
        'company'    => $_POST['customer']['company'],
        'email'      => $_POST['customer']['email'],
        'phone'      => $_POST['customer']['phone'],
        'address_1'  => $_POST['customer']['address'],
        'address_2'  => '', 
        'city'       => $_POST['customer']['city'],
        'state'      => '',
        'postcode'   => $_POST['customer']['postalcode'],
        'country'    => 'NL'
    );

    $order = wc_create_order();
    foreach ($_POST['product_order'] as $productId => $productOrdered) :
        $order->add_product( get_product( $productId ), 1 );
    endforeach;

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

    $order->calculate_totals();

    update_post_meta( $order->id, '_payment_method', 'ideal' );
    update_post_meta( $order->id, '_payment_method_title', 'iDeal' );

    // Store Order ID in session so it can be re-used after payment failure
    WC()->session->order_awaiting_payment = $order->id;

    // Process Payment
    $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
    $result = $available_gateways[ 'ideal' ]->process_payment( $order->id );

    // Redirect to success/confirmation/payment page
    if ( $result['result'] == 'success' ) {

        $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order->id );

        wp_redirect( $result['redirect'] );
        exit;
    }
}

这篇关于WooCommerce 以编程方式创建订单并重定向到付款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆