• Resolved paloma9

    (@paloma9)


    Hello, I need to implement a hook in Woocommerce so that the purchase flow ends in the cart when the user clicks on the “complete payment” button on the cart page, and that does not allow redirection to the final purchase page – checkout, finishing on the cart page the complete process.

    I couldn’t find this hook.

    I found this one, which ends the flow before the cart page:

    function terminar_en_carrito() {
    
        if ( is_cart() && ! is_checkout() ) {
    
            global $woocommerce;
    
            $woocommerce->cart->calculate_totals();
    
            // Crea el pedido y obtén su ID
    
            $pedido = wc_create_order();
    
            $pedido_id = $pedido->get_id();
    
            // Agrega los productos del carrito al pedido
    
            foreach ( $woocommerce->cart->get_cart() as $producto ) {
    
                $producto_id = $producto['data']->get_id();
    
                $cantidad = $producto['quantity'];
    
                $precio = $producto['data']->get_price();
    
                $pedido->add_product( wc_get_product( $producto_id ), $cantidad, array( 'regular_price' => $precio ) );
    
            }
    
            // Agrega la dirección de facturación y envío
    
            $direccion_facturacion = array(
    
                'first_name' => $woocommerce->customer->get_first_name(),
    
                'last_name'  => $woocommerce->customer->get_last_name(),
    
                'email'      => $woocommerce->customer->get_email(),
    
            );
    
            $pedido->set_address( $direccion_facturacion, 'billing' );
    
            $pedido->set_address( $direccion_facturacion, 'shipping' );
    
            // Establece el estado del pedido como "completado"
    
            $pedido->update_status( 'completed', __( 'Pedido completado automáticamente', 'woocommerce' ) );
    
            // Envía un correo electrónico de confirmación al cliente
    
            $correo = new WC_Emails();
    
            $correo->customer_completed_order( $pedido_id, true, false );
    
            // Vacía el carrito y redirige a una página de "Gracias"
    
            $woocommerce->cart->empty_cart();
    
            wp_redirect( site_url('https://zonafield.com.uy/gracias/') );
    
            exit;
    
        }
    
    }
    
    add_action( 'woocommerce_cart_totals_after_order_total', 'terminar_en_carrito' );

    It could be an option, but it also generates an error, doesn’t empty the cart, and doesn’t redirect to the “thank you” page. I already checked the redirection of the “thank you” page, activated debug mode and updated outdated woocommerce templates, and it’s all ok.

    Could you help me with either of these two things?

    Thank you!

    Paloma

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Hook skip checkout Woocommerce’ is closed to new replies.