更改Woocommerce结帐以在单独的WordPress网站上处理订单?

时间:2019-01-30 01:01:57

标签: php wordpress woocommerce

我需要创建Woocommerce处理订单的方式,以便可以在Site A上创建订单并将该订单发送到Site B

因此,当客户将商品添加到购物车并单击结帐时,它会同时在Site ASite B上创建订单,但是,它还会将用户重定向到Site B来处理付款。

到目前为止,我只能更改结帐按钮:

add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );

function my_change_checkout_url( $url ) {
   $url = "your checkout url ";
   return $url;
}

并创建订单。

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

尽管我不确定如何将所有这些放在一起,所以我可以同时使用站点A和站点B。

我该怎么做?

编辑:为了进一步说明我需要执行的操作,我需要在另一个包含产品的网站上处理付款。

因此,客户在网站A上购买产品,然后单击“用Paypal付款”(即时结帐)并“进行付款”,然后将客户及其填写的信息带到网站B,他们可以最终确定付款,或者直接带到站点B上的Paypal网关。

2 个答案:

答案 0 :(得分:4)

尝试这种方式

将此代码添加到站点A中。

function action_woocommerce_new_order( $order_id ) { 

    //get all order details by $order_id    
    //post your data to site B

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,"http://yoursiteB.com/wp-json/api/v2/create_wc_order");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2");

    // In real life you should use something like:
    // curl_setopt($ch, CURLOPT_POSTFIELDS, 
    //          http_build_query(array('postvar1' => 'value1')));

    // Receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec($ch);

    curl_close ($ch);
}; 
add_action( 'woocommerce_payment_complete', 'action_woocommerce_new_order', 10, 1 );

将此代码添加到网站B中。

Class WC_Rest_API {

    public function __construct() {
        add_action( 'rest_api_init', array( $this, 'init_rest_api') );
    }

    public function init_rest_api() {
        register_rest_route('api/v2', '/create_wc_order', array(
           'methods' => 'POST',
           'callback' => array($this,'create_wc_order'),
        ));
   }

    public function create_wc_order( $data ) {

            //$data  - all the details needs to be send from site A

            global $woocommerce;
            $user_id = $data['user_id'];
            $user = get_user_by( 'ID', $user_id);
            $phonenumer = get_user_meta( $user_id,'user_phoneno', true);

            if($user) {         

                $address = array(
                  'first_name' => $user->display_name,
                  'last_name'  => $user->display_name,
                  'email'      => $user->user_email,
                  'phone'      => $phonenumer,            
                );

                $order = wc_create_order();
                //get product details from rest api site A
                $order->add_product(); 
                $order->set_address( $address, 'billing' );

                $order->calculate_totals();         
                $order = wc_get_order( $order->id );

                $response['status'] = 'success';
                $response['message'] = 'Order created on wordpress site B.';    
                return new WP_REST_Response($response, 200);

          }

    }       
}

$rest_api = new WC_Rest_API();

通过URL下方将所有数据从站点A发送到站点B。

http://yoursiteB.com/wp-json/api/v2/create_wc_order

方法应为“ POST”

数据应包含站点A的所有详细信息或顺序。

如果您有任何问题,请告诉我。

答案 1 :(得分:0)

您可以使用WooCommerce提供的Web挂钩来更新有关产品添加和订单创建的站点B

Woocommerce Web Hooks

之后,您可以在站点A 中挂出结帐,然后重定向到站点B 结帐页面。

https://metorik.com/blog/how-to-create-woocommerce-custom-redirects https://developer.wordpress.org/reference/hooks/template_redirect/