woocommerce_add_to_cart action hook breaks current add to cart functionality of website

I built a node express API and have it listening for post requests coming from the woocommerce_add_to_cart webhook, but the payload from that is essentially worthless.

body:{ action: ‘woocommerce_add_to_cart’, arg:’098uoijo098920sa489983jk’ }

Is there a way to add product data to these webhooks? If not looking to send a php curl request from a functions.php function like so

My functions.php function:

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);

This is causing the websites add to cart functionality to break so my assumption is that the theme, Basel, uses this action hook and adding it again to functions.php causes an error. Looking at the old segment plugin, they too use this event for this purpose, so it must be possible to use this hook in multiple places.

The code snippet where the error occurs:

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() {}
})

The error:

Uncaught TypeError: Cannot read property ‘indexOf’ of undefined
at Object.success (theme.min.js?ver=4.0.0:formatted:17037)
at i (jquery.js?ver=1.12.4:2)
at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
at y (jquery.js?ver=1.12.4:4)
at XMLHttpRequest.c (jquery.js?ver=1.12.4:4)

When I remove my function from functions.php the product gets added to the cart properly and there are no errors.

Do I need to add this function in a plug-in rather than in functions.php?

Read more here:: woocommerce_add_to_cart action hook breaks current add to cart functionality of website

Leave a Reply

Your email address will not be published. Required fields are marked *