Horje
Trigger event script after product added to cart via Ajax - Woocommerce - Wordpress Solution
Hi Members, I am looking for a solution here. I have a woo-commerce website. I am looking to integrate webengage with it. I need to pass an event to webengage called "Added to cart" along with the product details that have been added to cart ( like product name, quantity, sku, sale price, reg price etc..) My site is using Ajax and I am not sure how to fire script after product is added to cart via ajax along with the product data as mentioned above. My site products are more of variation products, So I need to pass variation name aloing with the product title, price, sku, reg price, sale price, sku. Any help is much appreciated. Thanks!

Solution - 1

WooCommerce offers a hook that fires when an item is added to the cart. It is:

woocommerce_add_to_cart
This hook provides the following variables: $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data So you can use them in your custom function. To send data to any API from this function, you can use wp_remote_post or wp_remote_get methods. Here is a sample code that sends data after product is added to the cart:
function action_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { 

//your code here

//sample code...
$data = array( 'key1' => 'value1', 'key2' => 'value2' );
$response = wp_remote_post( 'http://httpbin.org/post', array( 'data' => $data ) );
}
add_action( 'woocommerce_add_to_cart', 'action_woocommerce_add_to_cart', 10, 6 );
Hope you will find this useful to send your data to WebEngage API.


Solution - 2

Are y ou still looking for help?





Wordpress

Related
Order posts by ACF checkbox - Wordpress Solution Order posts by ACF checkbox - Wordpress Solution
Cumulative Layout Shift - Wordpress Solution Cumulative Layout Shift - Wordpress Solution
Wordpress dompdf custom fonts in pdf - Wordpress Solution Wordpress dompdf custom fonts in pdf - Wordpress Solution
Set relation to be OR instead of AND in a custom Query - Wordpress Solution Set relation to be OR instead of AND in a custom Query - Wordpress Solution
How to show only parent terms with get_the_term_list() shortcode - Wordpress Solution How to show only parent terms with get_the_term_list() shortcode - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
34