B
B
Boris Belov2017-08-03 22:38:37
WooCommerce
Boris Belov, 2017-08-03 22:38:37

How to get count values ​​in yith-compare?

Good evening.
There is a plugin YITH-Compare
And with it, as you know, you can compare products.
But it doesn't have a built-in counter function.
For example, the wishlist has.

<?php $wishlist_count = YITH_WCWL()->count_products();
echo $wishlist_count; ?>

But if we change it in our way, then nothing comes of it.
<?php $compare_count = YITH_Woocompare()->count_products();
echo $compare_count; ?>

Here I thought. That the plugin has a class.yith-woocompare-widget.php widget. Is it possible to get a value from a widget?
---
I just tried to take and withdraw
<?php global $yith_woocompare;
echo $yith_woocompare->obj->list_products_html(); ?>

Got a list of products that were compared
---
Found in the file class.yith-woocompare-frontend.php
foreach ( $this->products_list as $product_id ) {
                $product = $this->wc_get_product( $product_id );
                if ( ! $product )
                  continue;
                ?>

                    <a class="title" href="<?php echo get_permalink( $product_id ) ?>"><?php echo $product->get_title() ?></a>
                    <a href="<?php echo $this->remove_product_url( $product_id ) ?>" data-product_id="<?php echo $product_id; ?>" class="remove" title="<?php _e( 'Remove', 'yith-woocommerce-compare' ) ?>">x</a>

You should instead print a number that represents the number of items. Essentially this is how to display the number of records that are currently displayed
---
by doing <?php var_dump($product_id); ?>
got 4 values ​​(which just hang in comparison)
int(38) int(134) int(136) int(135)
now how can I make only count from this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Belov, 2017-08-04
@iborisbelov

Happened! Who cares how to make a comparison counter in Woocommerce?
We need the YITH WooCommerce Compare
plugin 1. Open the class.yith-woocompare-frontend.php file (path: plugins/yith-woocommerce-compare-premium/includes)
2. Edit line 701. Delete everything from 701 to 711.

foreach ( $this->products_list as $product_id ) {
                $product = $this->wc_get_product( $product_id );
                if ( ! $product )
                  continue;
                ?>
                <li>
                    <a class="title" href="<?php echo get_permalink( $product_id ) ?>"><?php echo $product->get_title() ?></a>
                    <a href="<?php echo $this->remove_product_url( $product_id ) ?>" data-product_id="<?php echo $product_id; ?>" class="remove" title="<?php _e( 'Remove', 'yith-woocommerce-compare' ) ?>">x</a>
                </li>
            <?php
            }

And replace with
$count = 0;
            foreach ( $this->products_list as $product_id ) {
                $product = $this->wc_get_product( $product_id );
             $count++;
            }
            echo $count;
            return ob_get_clean();
        }

3. Next, we paste the following in the place where the "comparison counter" should be.
<?php global $yith_woocompare;
echo $yith_woocompare->obj->list_products_html(); ?>

Example:
<li class="user-btn__item">
<a class="user-btn__link" href="/compare/">
<span class="hidden-md-down hidden-sm-down user-btn__name">Сравнение</span>
<span class="user-btn__icon fa fa-balance-scale user-btn__icon--compare">
<span class="user-btn__number"><?php global $yith_woocompare;
echo $yith_woocompare->obj->list_products_html(); ?></span> <!-- тут выводим значение --> 
</span>
</a>
</li>

Thus, we removed what should have been used in the widget. This solution is suitable for those who can sacrifice the widget, but it will have a comparison counter at the top edge.

W
WordPress WooCommerce, 2017-08-04
@maxxannik

At the same time, you need to disable the annoying update notification system. Light a candle for the rest of the site in the church. And add "Professional Site Fighter" in your signature.
The first rule of the WordPress world is don't change the core and third party code. Read, touch, but don't change.
You can just read, copy, make a neat shortcode and stick it where you need it. Nothing will break. And everything will be according to the code.
Otherwise, those who, after such maneuvers, accept sites for maintenance, run into such planted bombings, then shed crocodile tears and cover the authors of these maneuvers with a good obscenity. And in general for karma it is bad.
* As a morning humor :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question