在WooCommerce中使用复选框进行变体以允许多项选择 [英] Using checkboxes for variations in WooCommerce to allow multiple choice

查看:304
本文介绍了在WooCommerce中使用复选框进行变体以允许多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用WooCommerce已有一段时间了,但是这个问题导致我遇到问题.我要为其创建站点的客户提供培训课程和演示文稿,并且该特殊产品(或演示文稿)允许将多个不同的选项添加到购物车中,每个选项都有其自己的价格.

I've been using WooCommerce for a while now but this one issue is causing me a problem. The client I'm making a site for provides training courses and presentations, and this partiular product (or presentation) allows several different options to be added to the cart, each with its own price.

因此,基本价格为零.然后,用户可以通过其现有网站上的复选框选择8个不同的演示文稿-我不知何故需要在其新网站上使用WooCommerce复制该演示文稿,但我只能使用下拉菜单进行变体形式,据我所知,它仅允许一种选择.我看到此工作的唯一可行方法是,如果我添加8个不同的下拉菜单,并且其中包含所有8个演示文稿,然后客户选择他们想要的许多不同的下拉菜单.但是,这有点麻烦,并且可能会导致用户错误(例如,两次选择相同的演示文稿).

So, the base price is zero. Then there are 8 different presentations that the user can select via checkbox on their existing website - I somehow need to replicate that using WooCommerce on their new site but I can only use drop downs for variations, and as far as I can see it only allows one option to be chosen. The only feasible way I can see this working is if I add 8 different dropdowns each with all 8 presentations within them and then the customer selects however many different ones they want. This is a bit cumbersome though and potentially can cause user error (selecting the same presentation twice for example).

我已经附上了我在WooCommerce中理想情况下的屏幕截图,有没有办法实现这一目标?我不介意使用插件,如果这是唯一的方法.

I have attached a screenshot of what I'd ideally like it to look like within WooCommerce, is there a way this is achievable? I don't mind using plugins if it's the only way.

推荐答案

您可以这样做:

1)编辑您的content-single-product.php:

1) Edit you content-single-product.php:

2)通过$ product = wc_get_product($ productId)获取产品

2) Get product by $product = wc_get_product( $productId )

3)检查$ product-> product_type =="variable"

3) Check if $product->product_type == "variable"

4)获取当前产品的所有变体并将其列出到复选框中:

4) Get all variants of current product and list it to checkboxes:

$variations = $product->get_available_variations();

foreach ( $variations as $variation ) {

$variationId = $variation['variation_id'];

echo '<input type="checkbox" name="variations[]" value="' . $variationId . '" />

}

echo '<input type="checkbox" name="product_id" value="' . $product->ID . '" />

5)之后,您可以处理$ _POST并以编程方式向购物车添加变体:

5) After that you can process $_POST and add variations to cart programatically:

if ( !empty( $_POST['variations'] ) ) {

$productId = $_POST['product_id'];
$qty = 1;
$buyVariations = $_POST['variations'];

foreach ( $buyVariations as $variationId ) {

WC()->cart->add_to_cart( $productId, $qty, $variationId );

}

}

6)正在进行消毒,验证和状态消息传递,但是此过程应该可以进行.

6) Sanitization, validation and status messaging is on you, but this process should works.

这篇关于在WooCommerce中使用复选框进行变体以允许多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆