A
A
Alexander Bondarenko2020-02-21 19:21:50
WordPress
Alexander Bondarenko, 2020-02-21 19:21:50

How to remove re-adding items to the cart?

How can I add a product to the cart on a custom page? Shortcodes don't work, my code works but it adds multiple products at once and on reload it also adds products to the cart, how can I fix this?

<?php
 global $post, $woocommerce;
?>

    <div class="products-main">
        <?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="<?php $woocommerce->cart->add_to_cart( $thumb_id-1,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