php WordPress WooCommerce - 使用 WC_Cart 类将可变产品添加到购物车
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10802172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WordPress WooCommerce - Add a variable product to cart using the WC_Cart class
提问by Gga
I am trying to add a variable product to the cart of the WordPress plugin, WooCommerce.
我正在尝试将可变产品添加到 WordPress 插件 WooCommerce 的购物车中。
So far I have been able to add single/simple products with:
到目前为止,我已经能够添加单个/简单的产品:
$woocommerce->cart->add_to_cart( [product_id], [quantity] );
However, looking in the WC_Class at the functions signature:
但是,查看 WC_Class 中的函数签名:
function add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() ) {
we can clearly see the function permits inputs of variation_id.
我们可以清楚地看到该函数允许输入variation_id。
I have tried every combination of nulls and integers along the lines of:
我已经尝试了以下行的空值和整数的每种组合:
$woocommerce->cart->add_to_cart( 24, 1, 28, null, null );
and so on to no avail.
等等都无济于事。
Ive also tried my own hacky approach that tries to recreate the post events performed by WooCommerce's own product page, again with no luck.
我也尝试过我自己的hacky方法,试图重新创建由WooCommerce自己的产品页面执行的发布事件,但同样没有运气。
<a id="buy_v" href="#">Buy Variable Product !</a>
<script>
$('#buy_v').click(function(e) {
e.preventDefault();
addToCartV(24,26,'Red',1);
return false;
});
function addToCartV(p_id, v_id, c, q) {
$.ajax({
type: 'POST',
url: '/wp/?product=tee1&add-to-cart=variation&product_id='+p_id,
data: { 'attribute_colour': c,
'variation_id': v_id,
'quantity': q,
'product_id': p_id},
success: function(response, textStatus, jqXHR){
// log a message to the console
console.log("It worked!");
}/*,
dataType: 'JSON'*/
});
}
</script>
Could anyone suggest where I might be going wrong? Thanks.
谁能建议我可能出错的地方?谢谢。
回答by Gga
Both the above example actually work fine, they just don't display correctly in WooCommerce's own cart.
上面的两个例子实际上都可以正常工作,只是在 WooCommerce 自己的购物车中没有正确显示。
To make them display correctly, pass in an array for the forth parameter which seems to represent the variation in WooCommerce's own cart:
为了使它们正确显示,请为第四个参数传入一个数组,该数组似乎代表 WooCommerce 自己的购物车中的变化:
$arr = array();
$arr['Color'] = 'Green';
$woocommerce->cart->add_to_cart( 24, 1, 28, $arr, null );
回答by jsims281
For anyone else attempting something similar, here is my approach.
对于其他尝试类似事情的人,这是我的方法。
I created a script to be called via ajax that contains the following:
我创建了一个通过 ajax 调用的脚本,其中包含以下内容:
<?php
require_once("../../../wp-blog-header.php");
header("HTTP/1.1 200 OK");
global $woocommerce;
$quantity = (isset($_REQUEST['qty'])) ? (int) $_REQUEST['qty'] : 1;
$product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_REQUEST['pid']);
$vid = (int) apply_filters('woocommerce_add_to_cart_product_id', $_REQUEST['vid']);
if ($vid > 0) $woocommerce->cart->add_to_cart( $product_id, $quantity, $vid );
else $woocommerce->cart->add_to_cart( $product_id, $quantity );
This successfully adds product variations to the cart
这成功地将产品变体添加到购物车

