【发布时间】:2019-10-31 22:36:05
【问题描述】:
我正在尝试将可变产品添加到 WordPress 插件 WooCommerce 的购物车中。
到目前为止,我已经能够添加单一/简单的产品:
$woocommerce->cart->add_to_cart( [product_id], [quantity] );
但是,查看 WC_Class 中的函数签名:
function add_to_cart( $product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array() ) {
我们可以清楚地看到该函数允许输入variation_id。
我已经尝试过各种空值和整数的组合:
$woocommerce->cart->add_to_cart( 24, 1, 28, null, null );
等等都无济于事。
我还尝试了我自己的 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>
谁能建议我哪里出错了?谢谢。
【问题讨论】:
-
嗨。我正在尝试构建一个像上面一样的 ajax 变体添加到购物车。我检查了 woocommerce 代码进出,但我想更好地了解 url var 结构/可能性/选项。我的意思是:
?product=tee1&add-to-cart=variation&product_id> 你是否有我可以传递到那里的所有变量和值类型的“映射”?