首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WooCommerce中不工作的数量字段

在WooCommerce中不工作的数量字段
EN

Stack Overflow用户
提问于 2015-12-03 15:11:29
回答 1查看 3.2K关注 0票数 0

我有一个网站:http://artware.gr/decouni/home/

在右上角,您可以看到一个购物车图标。如果您悬停它,您将看到‘添加到购物车’按钮,以及数量形式。

若要检查它是否有效,请添加3项并按下按钮添加它们。

如果您这样做,按钮和数量表单将消失,“结帐”按钮将出现。

但我们需要确认购物车总共有3件物品。

要查看购物车,只需单击购物车图标即可。

不幸的是,数量将是1而不是3。

到目前为止,我尝试过什么:

我想出了两种方法来解决这个问题,但这两种方法都有各自的问题。

1 WAY我创建了一个固定区域,按钮弹出。它只是WooCommerce的迷你购物车小部件。

wp-content/themes/stockholm/woocommerce/cart/mini-cart.php

在里面,我只是叫这个:

代码语言:javascript
复制
<?php echo do_shortcode('[add_to_cart id="246"]'); ?>

246个是产品的id,因为整个表单出现在一个简单的页面上,而不是在产品页面中,所以我不得不手动调用add按钮。

因此,在表单出现后,我编辑:

wp-content/themes/stockholm/woocommerce/loop/add-to-cart.php

这样我就可以把数量表包括到这个区域了。默认情况下,在调用add按钮时,WC不包括quantity表单。

您可以在这里看到add-to-cart.php的全部代码:http://pastebin.com/f7mNF6Cq

1路问题

这就是我现在生活的方式。正如我所说的,表单看起来是正确的,但没有按预期工作。

2路

我找到了另一段代码,可以插入到add-to-cart.php中:

http://pastebin.com/agxL8ica

2路问题

当我将此代码添加到add-to-cart.php中时,虽然它显示正确,但当我在数量上插入3项并按add to cart按钮时,就会出现一个带有一些代码的白色页面。

http://pastebin.com/BCqRHie9

此外,URL更改为:

代码语言:javascript
复制
http://artware.gr/decouni/home/?wc-ajax=get_refreshed_fragments&add-to-cart=246

现在,棘手的部分!

如果我重新打开主页并点击购物车图标来查看购物车,我可以看到数量是3!所以上面的代码,即使它显示了一个白色的页面,它也在数量上起作用。

方法1当我单击add to cart按钮时,它没有显示错误:) WAY 1将不会将数量值传递给购物车:(

方法2当我单击add to cart按钮时会显示错误:(方法2将数量值传递给cart :)

-你可以看到正在应用的方式1。

我认为WAY 2的代码可能是WC的旧版本,这就是为什么它不能正常工作的原因。

EN

回答 1

Stack Overflow用户

发布于 2015-12-04 10:09:20

代码语言:javascript
复制
<?php 

global $product; 


if (!in_array($product->product_type, array('variable', 'grouped', 'external'))) {
    // only if can be purchased
    if ($product->is_purchasable()) {
        // show qty +/- with button
        ob_start();
        woocommerce_simple_add_to_cart();
        $button = ob_get_clean();

        // modify button so that AJAX add-to-cart script finds it
        $replacement = sprintf('data-product_id="%d" data-quantity="1" $1 add_to_cart_button product_type_simple ', $product->id);
        $button = preg_replace('/(class="single_add_to_cart_button)/', $replacement, $button);
    }
}
// output the button
    echo $button;
?>

<script>
jQuery(function($) {

<?php /* when product quantity changes, update quantity attribute on add-to-cart button */ ?>
$("form.cart").on("change", "input.qty", function() {
    $(this.form).find("button[data-quantity]").attr("data-quantity", this.value);
});

<?php /* remove old "view cart" text, only need latest one thanks! */ ?>
$(document.body).on("adding_to_cart", function() {
    $("a.added_to_cart").remove();
});

});
</script>             
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34069433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档