Woocommerce自定义添加到购物车ajaxify按钮

时间:2019-10-31 05:54:46

标签: php wordpress loops woocommerce hook-woocommerce


我正在尝试使 Woocommerce自定义产品循环。但是,我仍然坚持如何在自定义产品循环中添加add to cart button ajax类型按钮。我的网站上有商店页面,有产品,并且这些产品有add to cart button with ajax action,例如当我单击add to cart button然后禁用了按钮,并且在按钮(内部按钮)的右侧时,出现加载程序图标,它旋转,在此过程的1秒钟后,出现我的购物车。我想要相同的内容,因为这是我的自定义产品循环中的ajax操作“添加到购物车”按钮。我进行了很多搜索,找到了答案,这是参考文献:https://stackoverflow.com/a/53015757/12199128。只能工作50%,单击后不会禁用按钮,单击按钮时也不会在按钮中显示加载器图标
以下是我的商店页面的屏幕截图(非自定义):
enter image description here
enter image description here

以下是我的自定义产品循环的屏幕截图:
enter image description here

这是我的代码:

<?php

$args = array(
        'post_type' => 'product',
        'showposts' => all,
        'orderby' => 'date',
        'order' => 'DESC',
);
$loop = new WP_Query( $args );

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

<div class="col-md-3 mb-3">
    <div class="card">
        <a href="" class="custom_image">
            <?php the_post_thumbnail( ); ?>
        </a>
        <?php woocommerce_show_product_sale_flash( $post, $product ); ?>
        <div class="card-body">
            <div>
                <a class="font-weight-bold" href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                    <?php the_title(); ?>
                </a>
            </div>
            <span class="price pl-0"><?php echo $product->get_price_html(); ?></span>
            <div class="add-to-cart ">
                <?php 
                    echo sprintf( '<a href="%s" data-quantity="1" class="btn btn-md btn-primary btn-block mt-3 %s" %s>%s</a>',
                    esc_url( $product->add_to_cart_url() ),
                    esc_attr( implode( ' ', array_filter( array(
                        'button', 'product_type_' . $product->get_type(),
                        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                        $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
                    ) ) ) ),
                    wc_implode_html_attributes( array(
                        'data-product_id'  => $product->get_id(),
                        'data-product_sku' => $product->get_sku(),
                        'aria-label'       => $product->add_to_cart_description(),
                        'rel'              => 'nofollow',
                    ) ),
                    esc_html( $product->add_to_cart_text() )
                );
                ?>
            </div>
        </div>
    </div>
</div>

<?php endwhile; wp_reset_query(); ?>

还有一件事,它没有显示所有产品,'showposts' => 12 <-可以使用,但是'showposts' => all <-无效。 Please help me

0 个答案:

没有答案