【问题标题】:Ajax add to cart with quantity is working only once for my woocommerce websiteAjax 添加到购物车的数量仅适用于我的 woocommerce 网站
【发布时间】:2016-02-06 09:04:18
【问题描述】:

在我的website 的商店页面上,我实现了添加到购物车的 ajax 功能以及数量输入。

我使用了来自website 的代码 sn-p。

该功能使用 ajax。因此,如果我在数量框中输入 10 并单击“添加到购物车”,则通过 ajax 将 10 个产品添加到购物车中。但问题是,如果在相同数量的框中(对于相同的产品),如果我想再添加 5 个,如果我在框中输入 5 并点击“添加到购物车”,它会再次添加 10 个项目而不是 5 个。

所以,它只保留 10 的值,并且无论我在数量框中输入什么,它都会不断添加。您可以在我的website 上查看。

所以,如果有人可以通过查看下面的代码来帮助我解决这个问题。

sn-ps的代码如下:

将 add-to-cart.php 文件替换为以下代码:

添加到购物车.php

<?php
/**
 * Custom Loop Add to Cart.
 *
 * Template with quantity and ajax.
 */

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();

                    // Display the submit button.
                    echo sprintf( '<button type="submit" data-product_id="%s" data-product_sku="%s" data-quantity="1" class="%s button product_type_simple">%s</button>', esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_html( $link['label'] ) );
                ?>
            </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; ?>

在functions.php文件中添加以下代码:

functions.php

function cs_wc_loop_add_to_cart_scripts() {
    if ( is_shop() || is_product_category() || is_product_tag() || is_product() || is_front_page() || is_home() ) : ?>

<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' );

数量输入.php

<div class="quantity"><a href="javascript:void(0)" class="minus">-</a><input type="number" step="<?php echo esc_attr( $step ); ?>" <?php if ( is_numeric( $min_value ) ) : ?>min="<?php echo esc_attr( $min_value ); ?>"<?php endif; ?> <?php if ( is_numeric( $max_value ) ) : ?>max="<?php echo esc_attr( $max_value ); ?>"<?php endif; ?> name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="input-text qty text" size="4" /><a href="javascript:void(0)" class="plus">+</a></div>

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    我认为 data-quantity 不能与 .attr() 一起正常工作。

    使用.data()..替换此代码..

    $(this).parent('.quantity').next('.add_to_cart_button').attr('data-quantity', $(this).val());
    

    试试这样的..

    $(this).closest('form').find('.add_to_cart_button').data('quantity', this.value);
    

    【讨论】:

    • 但是有一个小问题,如果我在数量框中输入值,那么它会被正确添加,但是如果我使用加号或减号按钮来添加或减少项目的数量,那么它不工作。我猜 onchange 没有检测到新值。
    • 所以,如果你能弄清楚为什么它不能与按钮一起工作,请帮助我。
    • 您应该检查+- 的工作原理.. 并在那里调用.change()....目前在您的问题中,我看不到它的代码...
    • 你能看看现场网站吗,因为我认为值在变化,但它没有被 on change 函数读取。 thejobupdates.com/pt/masonbaker/shop感谢您的宝贵时间
    • woocommerce_quantity_input();此代码输出数量框和加号和减号。所以,我认为您建议的 jquery 代码 sn-p 编辑应该适用于两者。但它不是
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    相关资源
    最近更新 更多