如何在自定义页面上将产品添加到购物车?简码不起作用,我的代码也不起作用?我知道这需要通过 Ajax 请求来完成,但我不太明白如何,请告诉我
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 echo esc_html( $product->single_add_to_cart_text() ); ?>"><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; ?>
我稍微修改了我的代码,现在货物被添加了但是是几件,如何解决这个问题?
<?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; ?>
您可以通过链接添加产品?add-to-cart=product_id