WooCommerce以编程方式创建订单并重定向到付款

时间:2015-08-03 12:26:45

标签: php wordpress woocommerce

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

使用复选框添加产品后,我想创建订单并重定向到付款方式。

使用此答案几乎可以创建订单,但如何开始付款? Wordpress (Woocommerce extension) - Create new order programatically

2 个答案:

答案 0 :(得分:18)

这对我有用:

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;
    }
}

答案 1 :(得分:0)

要跳过结帐页面,您可以过滤添加到购物车网址。

function so_31787244_redirect_to_checkout( $url ) {

    // Remove default cart message
    WC()->clear_messages();

    // Redirect to checkout
    $url = WC()->cart->get_checkout_url();

    return $url;
}
add_filter( 'add_to_cart_redirect', 'so_31787244_redirect_to_checkout' );

您还可以使用One Page Checkout

等插件