【问题标题】:Quantity field not working in WooCommerce数量字段在 WooCommerce 中不起作用
【发布时间】:2015-12-03 15:11:29
【问题描述】:

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

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

要检查它是否正常工作,请添加 3 个项目,然后按按钮添加它们。

如果你这样做,按钮和数量表格将消失,'结帐'按钮将出现。

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

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

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


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

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

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

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

在里面,我只是这样称呼:

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

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

所以,表格出现后,我编辑:

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

以便我可以将数量表格包含在该区域中。默认情况下,调用添加到购物车按钮时,WC 不包含数量表单。

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

1 WAY 的问题

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

两种方式

我发现另一段代码插入到 add-to-cart.php 中:

http://pastebin.com/agxL8ica

2 WAY 的问题

当我将此代码添加到 add-to-cart.php 时,虽然它显示正确,但当我插入 3 个数量的商品并按下“添加到购物车”按钮时,会出现一个白色页面,上面有一些代码。

http://pastebin.com/BCqRHie9

URL 也更改为:

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

现在是棘手的部分!

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


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

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

--- 目前在现场,您可以看到正在应用的 WAY 1。

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

【问题讨论】:

  • 我建议您停止尝试使用该短代码,并使用自定义类编写自己的 &lt;a&gt; 链接,并使用它来触发您自己的自定义 ajax。在您的修改版本中,您可以从数量输入中获取数量,因为如果您发送一个数量参数,add_to_cart() 回调方法将接受一个数量参数。

标签: php wordpress woocommerce


【解决方案1】:
<?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>             

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    相关资源
    最近更新 更多