【问题标题】:WooCommerce create an order programmatically and redirect to paymentWooCommerce 以编程方式创建订单并重定向到付款
【发布时间】:2015-08-03 12:26:45
【问题描述】:

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

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

这个答案几乎完成了创建订单,但我如何开始付款? Wordpress (Woocommerce extension) - Create new order programatically

【问题讨论】:

  • 确实是个好问题,我也在寻找解决方案。

标签: php wordpress woocommerce


【解决方案1】:

这是为我做的:

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

【讨论】:

  • 如果你还包括 javascript 那就太棒了。
  • 你为我节省了 2 周,900 美元,我找不到更多的词来感谢!
  • 嗨.. 我完全按照上面提到的做了,但我无法存储帐单和送货地址。你能看看stackoverflow.com/questions/50668922/…
  • 我确实回复了@AlaksandarJesusGene。但是您还应该在帖子中提供 $_POST 的 print_r。
  • @MaartendeWolf,您应该为 $available_gateways[ 'ideal' ] 使用正确的密钥;检查关键“理想”;
【解决方案2】:

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

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

【讨论】:

  • 我想跳过购物车和结帐页面。例如,在我的产品页面上,人们可以填写他们的联系信息并按下“立即付款”按钮。之后,需要创建订单并将其重定向到付款页面(例如 paypal.com)。
  • 我不确定你为什么还要使用 WooCommerce。但是,话虽如此,这听起来很定制,也许您可​​以编写一个函数,在 PayPal 验证订单时创建订单。但这似乎是很多工作却收效甚微。我认为您最好投资 SSL 证书和高级 PayPal 插件并在您的网站上直接接受付款。
猜你喜欢
  • 2020-06-07
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
相关资源
最近更新 更多