A
A
Alexander Bondarenko2020-02-22 18:29:46
WordPress
Alexander Bondarenko, 2020-02-22 18:29:46

How to add a product to the cart without a shortcode?

How can I add a product via code to cart on woocommerce? I have a cycle in which I display all the products, but the add to cart button does not work correctly, several products are added to the cart at once, how can I fix this, please tell me how to fix it. Already looked all over, but found nothing.

<?php  $loop = new WP_Query( array(
            'post_type' => 'product',  // указываем, что выводить нужно именно товары
            'posts_per_page' => 12, // количество товаров для отображения
            'orderby' => 'date', // тип сортировки (в данном случае по дате)
            'product_cat' => $sort,
            )); 

        

            while ( $loop->have_posts() ): $loop->the_post(); ?>
            <div class="product-main">
                <div class="product-img"> <a href="<?php echo get_permalink(); ?>" class="avatar"><img class="responsive-img" src="<?php
                $thumb_id = get_post_thumbnail_id();
                $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
                echo $thumb_url[0];
                ?>" alt=""></a>
                <div class="product-title"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></div>
                <div class="product-size">Размер: <?php echo $product->get_attribute('size'); ?> см.</div>
                <div class="product-price__box">
                    <div class="product-price">Цена: <?php echo $product->get_price_html(); ?> грн.</div>
                    <div class="info-cart"><a href="" onclick="<?php $woocommerce->cart->add_to_cart( $thumb_id-1 ); ?>"><img src="/wp-content/themes/dropship/assets/img/cart.png"></a></div>
                </div>
                <div class="product-button">
                    <button class="fast-buy">Быстрая покупка</button>
                </div>
                </div>
            </div>
            <?php endwhile; ?>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question