Skip to content

Instantly share code, notes, and snippets.

@girafffee
Last active August 20, 2021 07:39
Show Gist options
  • Save girafffee/e76ea58199306bdbe1884383f5a378eb to your computer and use it in GitHub Desktop.
Save girafffee/e76ea58199306bdbe1884383f5a378eb to your computer and use it in GitHub Desktop.
This is an example of working with a hook that is triggered when an item is added to the cart via the addon WooCommerce Cart & Checkout Action
<?php
/**
* This is an example of working with a hook
* that is triggered when an item is added to the cart via the addon
* @link https://jetformbuilder.com/addons/woocommerce-cart-checkout-action/
*
* Works with both JetFormBuilder and JetEngine Forms
*/
add_filter( 'jet-form-builder/action/redirect_to_woo_checkout/add-to-cart', function ( $add_to_cart_data ) {
/**
* @var array $cart_item_data
*
* $cart_item_data[ $manager->form_data_key ] -> Form Request (array)
* $cart_item_data[ $manager->form_id_key ] -> Form ID (int)
* $cart_item_data[ $manager->action_settings_key ] -> Current action settings (array)
*
* I strongly recommend not to change the $cart_item_data array.
* You can only get data from it.
*/
list( $product_id, $quantity, $variation_id, $variation, $cart_item_data ) = $add_to_cart_data;
$manager = \Jet_FB_Woo\Plugin::instance()->wc;
/**
* This is an example of how to change the quantity of an item
* based on the value from the form field.
*/
$request = $cart_item_data[ $manager->form_data_key ];
$quantity = $request['your_field_name'] ?? 1;
return array( $product_id, $quantity, $variation_id, $variation, $cart_item_data );
}, 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment