D
D
dinodp2020-08-09 20:28:08
WordPress
dinodp, 2020-08-09 20:28:08

How to display the number of woocommerce products with the ability to edit?

How to display the number of goods in the cart, so that this number is in parentheses (4) and so that you can catch on to all these things and set your own styles in css?
And so that the inscription "Basket" and the icon of the basket itself are not lost.
I display the number of products via <?php echo WC()->cart->get_cart_contents_count();?> but I can't get hold of the styles.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dinodp, 2020-08-09
@dinodp

It's funny to talk about it.
I found a solution and will answer myself in my own post (maybe it will help someone).
The solution at the link
https://inprocess.by/blog/kak-vyvesti-korzinu-wooc...
For some reason does not follow the link, I will duplicate the solution.
in header.php in the right place to stick this

<div class="s-header__basket-wr woocommerce">
    <?php
    global $woocommerce; ?>
    <a href="<?php echo $woocommerce->cart->get_cart_url() ?>" class="basket-btn basket-btn_fixed-xs">
        <span class="basket-btn__label">Корзина</span>
        <span class="basket-btn__counter">(<?php echo sprintf($woocommerce->cart->cart_contents_count); ?>)</span>
    </a>
</div>

and in functions.php this piece
add_filter('woocommerce_add_to_cart_fragments', 'header_add_to_cart_fragment');

function header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>
    <span class="basket-btn__counter">(<?php echo sprintf($woocommerce->cart->cart_contents_count); ?>)</span>
    <?php
    $fragments['.basket-btn__counter'] = ob_get_clean();
    return $fragments;
}

After that, you can already hook and manipulate as you want.
Thanks to myself :D

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question