사용자 지정 쿼리 루프 제품 woocmerce에서 작동하지 않는 AJAX add to cart 버튼
저는 우커머스를 사용하여 사용자 지정 전자 상거래 웹 사이트를 만들고 있는데 "카트에 추가 버튼"을 수정하는 데 문제가 있습니다.입력 상자/수량 상자에 여러 개의 금액을 추가할 때마다 카트에 한 개의 항목만 증가 또는 추가됩니다.이는 사용자 지정 루프를 생성할 때만 발생합니다.
매장과 단품 페이지에서는 잘 작동합니다.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 기능이 우커머스의 템플릿인 업셀 템플릿(up-sells.php)에서 작동하며 잘 작동한다는 것입니다.
<?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();
이미 이 개발자의 솔루션을 적용해 보았습니다.
그리고 이것도
하지만 여전히 같은 생산량을 만들어냅니다.왜 카트에 한 가지 항목만 증가시키는지 정말 모르겠습니다.브라우저 콘솔에 오류가 있는지 확인했으며 기능이 작동하도록 하기 위해 다른 방법이나 옵션을 시도했음을 확인하거나 알려주기 위해 코드의 일부를 주석으로 달았습니다.
다음 단계를 따릅니다.
- 언코멘트
woocommerce_quantity_input();
- 콘솔에 오류가 있는지 없는지 브라우저 콘솔에서 확인합니다.그렇다면 여기에 오류를 공유해 주시기 바랍니다.
- 오류가 없으면 교체합니다.
woocommerce_template_loop_add_to_cart( $crate_products->post, $product );
와 함께print_r(woocommerce_template_loop_add_to_cart( $crate_products->post, $product ));
데이터를 반환하는지 여부를 확인합니다.
댓글 달기도 시도해 봅니다.do_action( 'woocommerce_after_shop_loop_item' );
여기 업데이트된 버전이 있습니다.
<?php
/*
* Template Name: Home
*/
get_header(); ?>
<section class="full-width home-template">
<div class="full-width shop-section">
<div class="container">
<?php
global $product;
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
$posts = get_posts( $args );
foreach( $posts as $post ) :
setup_postdata( $post );
wc_setup_product_data( $post );
$product = wc_get_product( $post->ID ); ?>
<div id="post-<?php the_ID() ?>" class="three columns product-post">
<figure class="featured-image">
<a href="<?php the_permalink()?>" ><?php echo woocommerce_get_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(); ?>
</div>
</div>
<?php endforeach; ?>
<script type="text/javascript">
(function($){
$(document).ready(function(){
$(document).on( "keyup", "input.qty", function(){
$(this).parent().next().find("a").attr( "data-quantity", $(this).val() );
});
});
})(jQuery);
</script>
</div>
</div>
</section>
<?php get_footer(); ?>
<?php // woocommerce_quantity_input(); ?>
그래야 한다
<?php woocommerce_quantity_input(); ?>
언급URL : https://stackoverflow.com/questions/36857379/ajax-add-to-cart-button-not-working-on-custom-query-loop-product-woocommerce
'sourcecode' 카테고리의 다른 글
jQuery에서 숫자에 쉼표를 더합니다. (0) | 2023.10.30 |
---|---|
각 행의 결측/NaN 수 계산 (0) | 2023.10.30 |
플러그인 및 테마를 설치할 수 없습니다. (0) | 2023.10.30 |
click() 이벤트가 jQuery에서 두 번 호출됩니다. (0) | 2023.10.30 |
jQuery와 jQuery Mobile의 차이점은? (0) | 2023.10.30 |