Sergi Khizanishvili
Sergi Khizanishvili

Reputation: 587

Get Cart Item Remove URL in Woocommerce 3.3

I've got the below code to get elements from the woocommerce cart, but I can't figure out how to get remove link of the item as well. could you help me?

<div class="carters">
    <?php

        $subtotal = WC()->cart->get_cart_subtotal(); echo $subtotal;

        foreach( WC()->cart->get_cart() as $cart_item ){
            $product_id = $cart_item['data']->get_id();
            $qty        = $cart_item['quantity'];
            $price      = $cart_item['data']->get_price();
            $thumbnail  = get_the_post_thumbnail_url( $product_id ); 
            echo '<img width="50px" src="' . $thumbnail . '" />';
            $title      = get_the_title( $product_id ); 
            echo $title . $qty . $price; 
        }

    ?>
</div>

Upvotes: 2

Views: 7286

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253978

There is wc_get_cart_remove_url( $cart_item_key ) function that can be used for that this way:

<div class="carters">
    <?php

        $subtotal = WC()->cart->get_cart_subtotal(); echo $subtotal;

        foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
            $product_id = $cart_item['data']->get_id();
            $qty        = $cart_item['quantity'];
            $price      = $cart_item['data']->get_price();
            $thumbnail  = get_the_post_thumbnail_url( $product_id ); 
            echo '<img width="50px" src="' . $thumbnail . '" />';
            $title      = get_the_title( $product_id ); 
            echo $title . $qty . $price;

            // get the cart remove url (since WooCommerce 3.3)
            $cart_item_remove_url = wc_get_cart_remove_url( $cart_item_key );
        }

    ?>
</div>

Tested and works on Woocommerce 3.3+

It replace deprecated WC_Cart method get_remove_url( $cart_item_key )

So before woocommerce 3.3 you will use instead:

WC()->cart->get_remove_url( $cart_item_key );

Upvotes: 6

Related Questions