Tracking event error in Algolia
- salmanParticipant8 months, 2 weeks ago #45810
Let’s talk about the adding_to_cart event. We have an ajax-add-to-cart.js file in which a jQuery function and logic are performed on our add-to-cart button, with a post-call to add the product to the cart.
Please provide the solution accordingly so the object ID can also be sent to the add-to-cart event.
wpsolrKeymaster8 months, 2 weeks ago #45812Working now on our JS scripts to trigger an event that lets you extract the object ID from the cart data, according to your theme’s data structures.
The script will enable anyone to adapt the default add-to-cart behaviour to their own environment (plugin or theme).
Testing it right now.
wpsolrKeymaster8 months, 2 weeks ago #45813Here is the new release: https://www.dropbox.com/s/evviw9zo2ygpn4c/wpsolr-enterprise.23.9-new-custom-add-to-cart-js-events.zip?dl=1
As shown in the js code below, it now can call your own code that will unpack the objectid and quantity stored in the parameter “data” produced by your theme into the expected parameter object “event_data”. You can start by using console.log(data) to understand how this parameter is made.
You’ll just need to enqueue your JS code file in your child theme functions.php.jQuery(document).ready(function ($) { /* * Custom js events that can perform some transformations on event_data, * if cart data structure was modified by plugin or theme. * Must update event_data like {product_id: '123', quantity: 1} */ // Add to cart button with Ajax on shop or product search page $(document).on('wpsolr_on_adding_to_cart_event', function (e, data, event_data) { event_data.product_id = (extract from parameter "data"); event_data.quantity = (extract from parameter "data"); }); // Add to cart button on product page with form submit and page reload $(document).on('wpsolr_on_added_to_cart_event', function (e, data, event_data) { event_data.product_id = (extract from parameter "data"); event_data.quantity = (extract from parameter "data"); }); });
salmanParticipant8 months, 2 weeks ago #45816I tried it out, but we don’t have a child theme. I placed it in our main functions.php file, but I didn’t see any console in either of these functions.
I added your code to the custom.js file and used it like this in the main funtions.php file, which is placed in the root folder directory.
function enqueue_custom_script() {
wp_enqueue_script( ‘custom-js’, get_template_directory_uri() . ‘/js/custom.js’, array(‘jquery’), null, true );
}
add_action( ‘wp_enqueue_scripts’, ‘enqueue_custom_script’ );
You must be logged in to reply to this topic.