Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Remove product (item) from WooCommerce checkout page using AJAX

// Concatenate remove link after item qty
function filter_woocommerce_checkout_cart_item_quantity( $item_qty, $cart_item, $cart_item_key ) {
    $remove_link = apply_filters('woocommerce_cart_item_remove_link',
    sprintf(
        '<a href="#" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s" data-cart_item_key="%s">×</a>',
        __( 'Remove this item', 'woocommerce' ),
        esc_attr( $cart_item['product_id'] ),
        esc_attr( $cart_item['data']->get_sku() ),
        esc_attr( $cart_item_key )
    ),
    $cart_item_key );

    // Return
    return $item_qty . $remove_link;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'filter_woocommerce_checkout_cart_item_quantity', 10, 3 );

// jQuery - Ajax script
function action_wp_footer() {
    // Only checkout page
    if ( ! is_checkout() )
        return;
    ?>
    <script type="text/javascript">
    jQuery( function($) {
        $( 'form.checkout' ).on( 'click', '.cart_item a.remove', function( e ) {
            e.preventDefault();
            
            var cart_item_key = $( this ).attr( "data-cart_item_key" );
            
            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'woo_product_remove',
                    'cart_item_key': cart_item_key,
                },
                success: function ( result ) {
                    $( 'body' ).trigger( 'update_checkout' );
                    //console.log( 'response: ' + result );
                },
                error: function( error ) {
                    //console.log( error );
                }
            });
        });
    });
    </script>
    <?php

}
add_action( 'wp_footer', 'action_wp_footer', 10, 0 );

// Php Ajax
function woo_product_remove() { 
    if ( isset( $_POST['cart_item_key'] ) ) {
        $cart_item_key = sanitize_key( $_POST['cart_item_key'] );
        
        // Remove cart item
        WC()->cart->remove_cart_item( $cart_item_key );
    }
    
    // Alway at the end (to avoid server error 500)
    die();
}
add_action( 'wp_ajax_woo_product_remove', 'woo_product_remove' );
add_action( 'wp_ajax_nopriv_woo_product_remove', 'woo_product_remove' );
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Remove #product #WooCommerce #checkout #page #AJAX
ADD COMMENT
Topic
Name
7+3 =