Getting cart fees in WooCommerce not working in my header.php

48 views Asked by At

I have some code in my Wordpress header.php to create a side cart.

But the line $fees = WC()->cart->get_fees(); returns an empty array while there are fees applied because if I look in the checkout there is a fee. How is this possible?


echo '<div id="side-cart">';
    echo '<div class="cart-header">';
        echo '<h4>Winkelmand</h4>';
        echo '<button class="close-cart" aria-label="close shopping cart"><i class="bi bi-x-circle"></i></button>';
    echo '</div>';
        echo '<div class="cart-items">';
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product = $cart_item['data'];
            $product_id = $cart_item['product_id'];
            $productType = wc_get_product( $cart_item['product_id'] );
            $productType = $productType->get_type();
            $giftcard = get_field('giftcard', $product_id);
            $title = $product->name;
            $quantity = $cart_item['quantity'];
            //$price = WC()->cart->get_product_price( $product );
            $price = $exVat ? wc_price(wc_get_price_excluding_tax( $product )) : WC()->cart->get_product_price($product);
            //$image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'thumbnail' )[0];
            if ($productType === 'variable' && !empty($cart_item['variation'])) {
                $variation_id = $cart_item['variation_id'];
                $variation_image_id = get_post_thumbnail_id($variation_id);
                $image = !empty($variation_image_id) ? wp_get_attachment_image_src($variation_image_id, 'thumbnail')[0] : '';
            } else {
                $image = wp_get_attachment_image_src(get_post_thumbnail_id($product_id), 'thumbnail')[0];
            }
            if (!$image) {
                $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'thumbnail' )[0];
            }
            $link = get_the_permalink($product_id);
            echo '<div class="cart-item">';
            echo '<button class="remove" key="'.$cart_item_key.'"><i class="bi bi-trash-fill"></i></button>';
                echo '<div class="image"><a href="'.$link.'"><img src="'.$image.'" /></a></div>';
                echo '<div class="cart-item-content">';
                    echo '<p><a href="'.$link.'">'.$title.'</a></p>';
                    echo '<span class="price">'.$price.'</span>';
                    echo '<div class="quantity"><button id="min">-</button><input type="number" value="'.$quantity.'" key="'.$cart_item_key.'" readonly><button id="plus">+</button></div>';
                echo '</div>';
            echo '</div>';
        }
        echo '</div>';
        $total = WC()->cart->get_subtotal();
      
        $fees = WC()->cart->get_fees();

        $htmlTotals = '';
        $htmlTotals .= '<div class="totals-item">';
            $htmlTotals .= '<span class="label">Totaal</span>';
            $htmlTotals .= '<span class="total-price">' . wc_price($total) . '</span>';
        $htmlTotals .= '</div>';
      
        echo '<div class="cart-totals">';
            echo $htmlTotals;
        echo '</div>';
    echo '<div class="cart-actions">';
        echo '<button class="cart button beige" id="continue-shopping">Verder winkelen</button>';
    echo '</div>';
    echo '<div class="cart-footer">';
        echo '<div class="cart-footer-content">';
            echo '<a href="'.wc_get_checkout_url().'" class="checkout button icon"><i class="bi bi-basket3-fill"></i><span>Afrekenen</span></a>';
        echo '</div>';
    echo '</div>';
echo '</div>';
0

There are 0 answers