添加到购物车后停止刷新购物页面 - woocommerce

时间:2016-09-08 01:43:35

标签: woocommerce refresh shopping-cart

我正在尝试在Woocommerce中添加目录和购物页面中的多个产品。我有一些代码可以让我这样做:



<?php
/**
 * Custom Loop Add to Cart.
 * 
 * Template with quantity.
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
global $product;
?>

<?php if ( ! $product->is_in_stock() ) : ?>

    <a href="<?php echo apply_filters( 'out_of_stock_add_to_cart_url', get_permalink( $product->id ) ); ?>" class="button"><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?></a>

<?php else : ?>

    <?php
        $link = array(
            'url'   => '',
            'label' => '',
            'class' => ''
        );
        switch ( $product->product_type ) {
            case "variable" :
                $link['url']    = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
                $link['label']  = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) );
            break;
            case "grouped" :
                $link['url']    = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
                $link['label']  = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) );
            break;
            case "external" :
                $link['url']    = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
                $link['label']  = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
            break;
            default :
                if ( $product->is_purchasable() ) {
                    $link['url']    = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
                    $link['label']  = apply_filters( 'add_to_cart_text', __( 'Add to cart', 'woocommerce' ) );
                    $link['class']  = apply_filters( 'add_to_cart_class', 'add_to_cart_button' );
                } else {
                    $link['url']    = apply_filters( 'not_purchasable_url', get_permalink( $product->id ) );
                    $link['label']  = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) );
                }
            break;
        }
        // If there is a simple product.
        if ( $product->product_type == 'simple' ) {
            ?>
            <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype="multipart/form-data">
                <?php
                    // Displays the quantity box.
                    woocommerce_quantity_input();
                ?>
                <button type="submit" class="button alt"><?php echo $link['label']; ?></button>
            </form>
            <?php
        } else {
          echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button product_type_%s">%s</a>', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link );
        }
    ?>

<?php endif; ?>
&#13;
&#13;
&#13;

这可以显示购物和范围页面上的增量添加到购物车按钮:http://www.playimports.com.au/shop/(您无法看到按钮,因为您需要登录才能看到它们 - 让我知道你是否需要一些登录信息。)

此工作正常,但在将产品添加到购物车后页面会刷新。

我尝试将其添加到functions.php文件中:

&#13;
&#13;
<?php
function cs_wc_loop_add_to_cart_scripts() {
    if ( is_shop() || is_product_category() || is_product_tag() || is_product() ) : ?>

<script>
    jQuery( document ).ready( function( $ ) {
        $( document ).on( 'change', '.quantity .qty', function() {
            $( this ).parent( '.quantity' ).next( '.add_to_cart_button' ).attr( 'data-quantity', $( this ).val() );
        });
    });
</script>

    <?php endif;
}
add_action( 'wp_footer', 'cs_wc_loop_add_to_cart_scripts' );
&#13;
&#13;
&#13;

但是,它不起作用。该页面仍然刷新,这确实存在问题,因为我们有成千上万的产品和人们在做大订单。

或者,是否有另一种方法可以在商店页面上添加增量添加到购物车按钮?

我在Stack Overflow上找到了一个可以添加的答案:

&#13;
&#13;
add_action('woocommerce_after_shop_loop_item',
           'woocommerce_template_single_add_to_cart', 30);
&#13;
&#13;
&#13;

到商店页面循环,并将其放在functions.php文件中,但页面仍然刷新,并且它增加了所有产品的数量。

谢谢,如果你能提供帮助!

0 个答案:

没有答案
相关问题