Javascript 添加操作添加到购物车功能
我构建了一个node express API,并让它监听来自woocommerce\u add\u to\u cart webhook的post请求,但是来自它的有效负载基本上毫无价值Javascript 添加操作添加到购物车功能,javascript,php,ajax,woocommerce,webhooks,Javascript,Php,Ajax,Woocommerce,Webhooks,我构建了一个node express API,并让它监听来自woocommerce\u add\u to\u cart webhook的post请求,但是来自它的有效负载基本上毫无价值 body:{ action: 'woocommerce_add_to_cart', arg:'098uoijo098920sa489983jk' } 有没有办法将产品数据添加到这些Webhook中?如果不希望从functions.php函数发送php curl请求,则 My functions.php函数: f
body:{ action: 'woocommerce_add_to_cart', arg:'098uoijo098920sa489983jk' }
有没有办法将产品数据添加到这些Webhook中?如果不希望从functions.php函数发送php curl请求,则
My functions.php函数:
function custom_add_to_cart($cart_item_key, $product_id, $quantity,
$variation_id, $variation, $cart_item_data){
//added this just to see if my event function even gets called, doesn't appear to
echo'<pre>';var_dump($product_id);echo'</pre>';die();
$product_info = json_encode(array($product_id => wc_get_product($product_id)));
$request_args = array(
'httpversion' => '1.1',
'headers' => 'Content-Type:application/json',
'body' => $product_info,
);
$response = wp_safe_remote_post("myapp.com/endpoint", $request_args);
}
add_action('woocommerce_add_to_cart', 'custom_add_to_cart', 10, 6);
错误:
//line where error gets reported
if(b.notices){
b.notices.indexOf("error") > 0 ? (a(".woocommerce-error").length > 0 && a(".woocommerce-error").remove(),
a(".single-product-content").prepend(b.notices),
d.addClass("not-added")) : ("widget" == basel_settings.add_to_cart_action && a.magnificPopup.close(),
d.addClass("added"),
a(document.body).trigger("added_to_cart", [e, f, d]))
}
当我从functions.php中删除我的函数时,产品被正确地添加到购物车中,并且没有错误
我是否需要在插件中而不是在functions.php中添加此函数 我想只是注意不在那里,所以你需要保护它 你可以试试看
您是否可以尝试使用
error\u log(json\u encode($product)),而不是echo
代码>和错误日志(json编码($cart\u item\u data))代码>。并在wp内容中检查文件debug.log以获取输出。注意事项是什么?是否尝试删除echo';变量转储($product\U id);回声';模具()代码>完全地,ajax调用通过回显它得到意外响应的任何内容来期待响应。没有骰子。错误可能发生在链的上游。我只是想知道通过添加add_to_cart操作,是什么导致add to cart中断。也许我可以想出一个不同的策略。
a.ajax({
url: basel_settings.ajaxurl,
data: e,
method: "POST",
success: function(b) {
if (b) {
var c = window.location.toString();
if (c = c.replace("add-to-cart", "added-to-cart"),
b.error && b.product_url)
return void (window.location = b.product_url);
if ("yes" === wc_add_to_cart_params.cart_redirect_after_add)
return void (window.location = wc_add_to_cart_params.cart_url);
d.removeClass("loading");
var e = b.fragments
, f = b.cart_hash;
e && a.each(e, function(b) {
a(b).addClass("updating")
}),
e && a.each(e, function(b, c) {
a(b).replaceWith(c)
}),
//line where error gets reported
b.notices.indexOf("error") > 0 ? (a(".woocommerce-error").length > 0 && a(".woocommerce-error").remove(),
a(".single-product-content").prepend(b.notices),
d.addClass("not-added")) : ("widget" == basel_settings.add_to_cart_action && a.magnificPopup.close(),
d.addClass("added"),
a(document.body).trigger("added_to_cart", [e, f, d]))
}
},
error: function() {
console.log("ajax adding to cart error")
},
complete: function() {}
})
//line where error gets reported
if(b.notices){
b.notices.indexOf("error") > 0 ? (a(".woocommerce-error").length > 0 && a(".woocommerce-error").remove(),
a(".single-product-content").prepend(b.notices),
d.addClass("not-added")) : ("widget" == basel_settings.add_to_cart_action && a.magnificPopup.close(),
d.addClass("added"),
a(document.body).trigger("added_to_cart", [e, f, d]))
}