Tuesday, December 27, 2022
HomeWordPress Developmentphp - Take away Woocommerce product from cart with ajax/fetch

php – Take away Woocommerce product from cart with ajax/fetch


I’ve an issue with eradicating merchandise from the cart in Woocommerce and I feel it has to do with WC_Cart::remove_cart_item. I solely get these error messages:

POST http://localhost:3000/esport/wp-admin/admin-ajax.php
[HTTP/1.1 500 Internal Server Error 3046ms]

SyntaxError: JSON.parse: surprising finish of knowledge at line 1 column 1 of the JSON information

The merchandise are looped over from my latte (php) file and including a data-key attribute to every <li> component.

{var $cart_items = WC()->cart->get_cart()}
<part class="sidebar-checkout">
        <div class="sidebar-checkout__header">
            <h3 n:ifcontent>Varukorg</h3>
            <button class="cart-checkout-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="w-8 mr-4 icon-close"><path class="secondary" fill-rule="evenodd" d="M15.78 14.36a1 1 0 0 1-1.42 1.42l-2.82-2.83-2.83 2.83a1 1 0 1 1-1.42-1.42l2.83-2.82L7.3 8.7a1 1 0 0 1 1.42-1.42l2.83 2.83 2.82-2.83a1 1 0 0 1 1.42 1.42l-2.83 2.83 2.83 2.82z"></path></svg></button>
        </div>
        <ul class="sidebar-cart" n:if="$cart_items">
            <li n:foreach="$cart_items as $cart_item_key => $cart_item" class="sidebar-cart__item" data-key="{$cart_item_key}">
                {var $product = $cart_item['data']}
                {var $img_src = wp_get_attachment_image_url($product->get_image_id(), 'thumbnail')}
                {var $image_alt = get_post_meta($attachment_id, "_wp_attachment_image_alt", TRUE)}
                <img src="{$img_src}" alt="{$image_alt}" decoding="async" loading="lazy">
                <a href="{get_permalink($product->get_id())}">{$product->get_name()}</a>
                <button class="remove_from_cart_button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="w-8 mr-4 icon-trash"><path class="major" d="M5 5h14l-.89 15.12a2 2 0 0 1-2 1.88H7.9a2 2 0 0 1-2-1.88L5 5zm5 5a1 1 0 0 0-1 1v6a1 1 0 0 0 2 0v-6a1 1 0 0 0-1-1zm4 0a1 1 0 0 0-1 1v6a1 1 0 0 0 2 0v-6a1 1 0 0 0-1-1z"></path><path class="secondary" d="M8.59 4l1.7-1.7A1 1 0 0 1 11 2h2a1 1 0 0 1 .7.3L15.42 4H19a1 1 0 0 1 0 2H5a1 1 0 1 1 0-2h3.59z"></path></svg></button>
            </li>
            <div class="widget_shopping_cart_content"></div>
        </ul>
</part>

The important thing attributes are picked up by my javascript file after which despatched by the fetch perform and obtained by the wp_ajax_ customized perform.

const remove_from_cart_button = doc.querySelectorAll('.remove_from_cart_button');
remove_from_cart_button.forEach(e => {
    e.addEventListener('click on', async e => {
        const key = e.goal.closest('.sidebar-cart__item').dataset.key;
        attempt {
            const response = await fetch('/esport/wp-admin/admin-ajax.php', {
                methodology: 'POST',
                headers: {
                    'Content material-Kind': 'utility/x-www-form-urlencoded'
                },
                physique: 'motion=my_custom_action&cart_item_key=' + key
                }
            );
            const information = await response.json();
            console.log('information', information);
        } catch (error) {
            console.error(error)
        }
    }
)})

And my last ajax perform:

add_action('wp_ajax_my_custom_action', 'my_custom_action_callback');
add_action('wp_ajax_nopriv_my_custom_action', 'my_custom_action_callback');

perform my_custom_action_callback() {
    $cart_item_key = $_POST['cart_item_key'];
    foreach(WC()->cart->get_cart() as $key => $merchandise) {
        if ($key == $cart_item_key) {
            WC()->cart->remove_cart_item($key);
            wp_send_json_success(array('message' => 'Cart merchandise eliminated efficiently'));
        }
    }
  
}

If I take away WC()->cart->remove_cart_item($key); It sends again the json message with out drawback.
I’ve tried utilizing woocommerce_remove_cart_item immediately within the motion fetch however I additionally get an error there. The error log doesnt show any errors.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments