簡體   English   中英

WooCommerce Ajax 添加到購物車數量不起作用

[英]WooCommerce Ajax add to cart quantity doesn't work

一直在尋找解決方案,希望有人可以提供幫助。

問題:

我正在嘗試在 WP Bakery 塊中制作自定義產品循環(到目前為止一切都很好)。 產品循環將被添加到首頁。 我設法包含了一個運行良好的數量字段,直到我想讓它在 AJAX 添加到購物車上運行。 一旦我做了 AJAX 它只會將 1 個產品添加到購物車(好像它沒有從數量中讀取輸入)。 在產品頁面上似乎一切正常,所以也許必須在 WP_Query 中定義一些東西?

要在首頁上顯示的循環:

<?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 ?>

您對$loop->post->ID進行了一些無效使用,當您聲明the_post()時,它在循環內將只是$post->ID

此外,如果您使用的是最新的 WC 版本, $product->id應該是$product->get_ID()

這在我的測試中有效。

// 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM