【发布时间】:2016-08-19 20:04:01
【问题描述】:
我正在使用 woocommerce 构建一个自定义电子商务网站,但在修复“添加到购物车按钮”时遇到了一些问题。每当我在输入框/数量框中添加多个金额时,它只会增加或将一项添加到购物车中。这仅在我创建自定义循环时发生。
在商店和单品页面上,它运行良好。如果我添加 10 件商品并按下添加到购物车按钮。它正好将 10 件商品添加到购物车中。
这是我一直在制作的模板。
<?php
/*
* Template Name: Home
*/
get_header(); ?>
<section class="full-width home-template">
<div class="full-width shop-section">
<div class="container">
<?php
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
$crate_products = new WP_Query ( $args );
if ( $crate_products->have_posts() ) : while ( $crate_products->have_posts() ) :
$crate_products->the_post();
?>
<div id="post-<?php the_ID() ?>" class="three columns product-post">
<?php // wc_get_template_part('content', 'product'); ?>
<figure class="featured-image">
<?php
//Display Product Thumbnail
$product_thumbnail = woocommerce_get_product_thumbnail();
?>
<a href="<?php the_permalink()?>" ><?php echo $product_thumbnail ?></a>
</figure>
<h2 class="product-price"><a href="<?php the_permalink(); ?>"><?php wc_get_template( 'single-product/price.php' ); ?></a></h2>
<span class="product-name"><?php the_title(); ?></span>
<?php // woocommerce_quantity_input(); ?>
<div class="add-to-cart-btn">
<?php woocommerce_template_loop_add_to_cart( $crate_products->post, $product ); ?>
<?php // do_action( 'woocommerce_after_shop_loop_item' ); ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
</div>
</section>
<?php get_footer(); ?>
令人困惑的是,AJAX 功能适用于 upsells 模板(up-sells.php),它是 woocommerce 的模板,并且运行良好。
<?php
/**
* Single Product Up-Sells
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/up-sells.php.
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product, $woocommerce_loop;
$upsells = $product->get_upsells();
if ( sizeof( $upsells ) === 0 ) {
return;
}
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => $upsells,
'post__not_in' => array( $product->id ),
'meta_query' => $meta_query
);
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<div class="upsells products">
<div class="twelve columns">
<h2><?php // _e( 'You may also like…', 'woocommerce' ) ?></h2>
</div>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<div id="post-<?php the_ID() ?>" class="three columns product-post">
<?php wc_get_template_part('content', 'product'); ?>
</div>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
我已经尝试过应用这个开发者的解决方案
还有这个
但它仍然产生相同的输出。我真的不知道为什么它只会将一件商品添加到购物车中。我已经检查了浏览器控制台是否有任何错误,并且还注释掉了代码的某些部分,以确保或让您知道我已经尝试了不同的方法或选项来使功能正常工作
【问题讨论】:
-
我没有看到数量输入...
-
这里是数量输入
-
如果你可以追踪它,那个动作的数量总是1。
-
并且没有数量输入
-
我还是不明白你说的“不输入数量”是什么意思
标签: php ajax loops woocommerce