数量字段在WooCommerce中无效

时间:2015-12-03 15:11:30

标签: php wordpress woocommerce

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

在右上角,您可以看到购物车图标。如果您将鼠标悬停,则会看到“添加到购物车”'按钮,以及数量表。

要检查它是否有效,请添加3个项目,然后按按钮添加它们。

如果您这样做,按钮和数量表单将会消失并且结帐'按钮将出现。

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

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

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

到目前为止我尝试了什么:

我想出了两种方法来解决这个问题,但两种方式都存在问题。

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

可湿性粉剂内容/主题/斯德哥尔摩/ woocommerce /购物车/小型cart.php

在其中,我只是这样称呼:

<?php echo do_shortcode('[add_to_cart id="246"]'); ?>

246是产品的ID,因为整个表单出现在一个简单的页面而不是产品页面内,我不得不手动调用“添加到购物车”按钮。

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

可湿性粉剂内容/主题/斯德哥尔摩/ woocommerce /循环/添加到cart.php

这样我就可以将数量表格包含在该区域内。默认情况下,当您调用“添加到购物车”按钮时,WC不包括数量表单。

您可以在此处查看add-to-cart.php的完整代码: http://pastebin.com/f7mNF6Cq

1 WAY的问题

这种方式就是我现在的生活方式。正如我所说,表格正确显示,但没有按预期工作。

2 WAY

我在add-to-cart.php中找到了另一段代码:

http://pastebin.com/agxL8ica

2方式问题

当我将此代码添加到add-to-cart.php时,虽然它显示正确,但当我按数量插入3个项目并按下添加到购物车按钮时,会出现一个白页,其中包含一些代码。

http://pastebin.com/BCqRHie9

此外,网址也会更改为:

http://artware.gr/decouni/home/?wc-ajax=get_refreshed_fragments&add-to-cart=246

现在是棘手的部分!

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

方式1当我点击添加到购物车按钮时,它没有显示错误:) 方法1不会将数量值传递给购物车:(

方式2当我点击添加到购物车按钮时显示错误:( 方法2将数量值传递给购物车:)

---目前在实际网站上,您可以看到正在应用的方式1。

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

1 个答案:

答案 0 :(得分:0)

<?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>