首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce Ajax添加到购物车数量不起作用

问WooCommerce Ajax添加到购物车数量不起作用
EN

Stack Overflow用户
提问于 2020-07-06 09:36:34
回答 1查看 642关注 0票数 1

很长一段时间以来一直在寻找解决方案,希望有人能帮上忙。

问题:

我试图在WP块中做一个定制的产品循环(到目前为止还不错)。产品循环将添加到frontpage。我已经成功地包含了一个quantity字段,直到我想让它在上工作为止。一旦我实现了AJAX,它只会向购物车中添加一个产品(就好像它没有从数量上读取输入一样)。在产品页面上,一切似乎都很好,所以也许需要在WP_Query中定义一些东西?

将显示在frontpage上的循环:

代码语言:javascript
复制
<?php
// Setup your custom query
$args = array( 'post_type' => 'product', 'orderby' => 'date' );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

    <div class="product-content-containers">
        <a href="<?php echo get_permalink( $loop->post->ID ) ?>">
            <div id="mobclear" style="background-image: url(<?php echo get_the_post_thumbnail_url($loop->post->ID);?>);" class="product-right-content">
                </div>
        </a>
        <div id="descclear" class="product-left-content">
            <h3 class="h5">
                <a href="<?php echo get_permalink( $loop->post->ID ) ?>">
                    <?php the_title(); ?>
                </a>
            </h3>
            <p><?php echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt ) ?></p>
            <div>
            <p><span class="woocommerce-Price-amount amount customamount"><?php echo $product->get_price(); ?> <span class="woocommerce-Price-currencySymbol"><?php echo get_woocommerce_currency_symbol(); ?></span> pr. stk.</span></p>
            <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype='multipart/form-data'>

               <?php woocommerce_quantity_input(); ?>

                <button type="submit" data-quantity="1" data-product_id="<?php echo $product->id; ?>"
                class="button alt ajax_add_to_cart add_to_cart_button product_type_simple"><?php echo $label; ?></button>

            </form>
            </div>
        </div>
    </div>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-06 10:08:40

您对$loop->post->ID有一些无效的用法,当您声明the_post()时,它在循环中仅为$post->ID

而且,如果您使用的是最新的WC版本,那么$product->id应该是$product->get_ID()。

下面这是我的测试。

代码语言:javascript
复制
// Setup your custom query
$args = array( 'post_type' => 'product', 'orderby' => 'date' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; 
?>
<div class="product-content-containers">
    <a href="<?php echo get_permalink( $post->ID ) ?>">
        <div id="mobclear" style="background-image: url(<?php echo get_the_post_thumbnail_url($post->ID);?>);" class="product-right-content">
            </div>
    </a>
        <div id="descclear" class="product-left-content">
            <h3 class="h5">
                <a href="<?php echo get_permalink( $post->ID ) ?>">
                    <?php the_title(); ?>
                </a>
            </h3>
            <p><?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?></p>
            <div>
            <p><span class="woocommerce-Price-amount amount customamount"><?php echo $product->get_price(); ?> <span class="woocommerce-Price-currencySymbol"><?php echo get_woocommerce_currency_symbol(); ?></span> pr. stk.</span></p>
            <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype='multipart/form-data'>

               <?php woocommerce_quantity_input(); ?>

                <button type="submit" data-quantity="1" data-product_id="<?php echo $product->get_ID(); ?>"
                class="button alt ajax_add_to_cart add_to_cart_button product_type_simple"><?php echo $label; ?></button>

            </form>
            </div>
        </div>
    </div>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
<script type="text/javascript">
jQuery('input[name="quantity"]').change(function(){
    var q = jQuery(this).val();
    jQuery('input[name="quantity"]').parent().next().attr('data-quantity', q);
});
</script>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62753237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档