M
M
MaKS_Ibl42019-09-08 22:06:46
WordPress
MaKS_Ibl4, 2019-09-08 22:06:46

How to checkout without a shopping cart?

Hello, here is the issue. I need to make it possible to place an order from a custom form, where there is only a phone number and a name. And after filling out this form and selecting a product in the select, immediately send it to the gateway. In general, the chip works, but as it turned out, because I was logged in. In the case of a normal user, the redirect goes to an empty cart via the checkout link. Tell me where to dig? I use the following code:

$variant_id = $_POST['count'];
        $address = array(
            'first_name' => $_POST['name'],
            'phone'      => $_POST['phone'],
        );
        $order = wc_create_order();
        $order->add_product(wc_get_product( $variant_id ), 1 );
        $order->set_address( $address, 'billing' );
        $order->set_address( $address, 'shipping' );
        $order->calculate_totals();
        update_post_meta( $order->get_id(), '_payment_method', 'rbspayment' );
        update_post_meta( $order->get_id(), '_payment_method_title', 'Альфабанк' );
        WC()->session->order_awaiting_payment = $order->get_id();
        $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
        $result = $available_gateways[ 'rbspayment' ]->process_payment( $order->get_id() );
        
        if ( $result['result'] == 'success' ) {
           echo $result['redirect'];
            exit;
        }
        exit;

I send the form with Ajax, and if I am authorized, it throws me into the gate, if not, into an empty basket, with an additional message saying that your basket is empty, there is nothing to pay.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaKS_Ibl4, 2019-09-09
@MaKS_Ibl4

Issue resolved.
Added:

global $woocommerce;
$woocommerce->cart->add_to_cart(id товара);

Before create order.
Now everything worked as it should. Like

X
xmoonlight, 2019-09-09
@xmoonlight

WC()->session->order_awaiting_payment = $order->get_id();
        $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
        $result = $available_gateways[ 'rbspayment' ]->process_payment( $order->get_id() );

WC()->session->there's a problem!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question