Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lucasstark/43fde31cc78a3d6e1997d8b36f69ff6f to your computer and use it in GitHub Desktop.
Save lucasstark/43fde31cc78a3d6e1997d8b36f69ff6f to your computer and use it in GitHub Desktop.
Set Gravity Forms Product Addons to always edit a product in the cart, regardless of where that item was navigated from.
/**
* Attempts to retrieve the cart item key for a product that is enabled for cart editing.
*
* @param int $product_id The ID of the product to check.
*
* @return false|string The cart item key if found, false otherwise.
*/
function wc_gfpa_get_cart_edit_key( $product_id ) {
if ( is_admin() || ! WC()->cart ) {
return false;
}
// Loop through each item in the cart.
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if the item contains Gravity Forms data and a lead entry.
if ( isset( $cart_item['_gravity_form_data'] ) && isset( $cart_item['_gravity_form_lead'] ) ) {
// Determine if the product is a variation and get the parent or the product itself.
if ( $cart_item['data']->get_type() == 'variation' ) {
$p = wc_get_product( $cart_item['data']->get_parent_id() );
} else {
$p = $cart_item['data'];
}
// If the product ID does not match, skip to the next item.
if ( $p->get_id() != $product_id ) {
continue;
}
// Retrieve Gravity Forms data associated with the product.
$gravity_form_data = wc_gfpa()->get_gravity_form_data( $p->get_id() );
// Check if cart editing is enabled for this product.
if ( isset( $gravity_form_data['enable_cart_edit'] ) && $gravity_form_data['enable_cart_edit'] !== 'no' ) {
// Return the cart item key if cart editing is enabled.
return $cart_item_key;
}
}
}
// If no cart item key is found, return false.
return false;
}
// Filter to override the add to cart URL for a product if cart editing is enabled.
add_filter( 'woocommerce_product_add_to_cart_url', function ( $url, $product ) {
// Get the cart edit key for the product.
$cart_edit_key = wc_gfpa_get_cart_edit_key( $product->get_id() );
if ( $cart_edit_key ) {
// Modify the product URL to include the cart edit query parameter.
$url = add_query_arg( [ 'wc_gforms_cart_item_key' => $cart_edit_key ], $url );
}
return $url; // Return the potentially modified URL.
}, 10, 2 );
// Filter to override the product permalink if cart editing is enabled.
add_filter( 'post_type_link', function ( $url, $post ) {
// Check if the post is a product and get the cart edit key.
$product = wc_get_product( $post->ID );
$cart_edit_key = wc_gfpa_get_cart_edit_key( $product->get_id() );
if ( $cart_edit_key ) {
// Modify the permalink to include the cart edit query parameter.
$url = add_query_arg( [ 'wc_gforms_cart_item_key' => $cart_edit_key ], $url );
}
return $url; // Return the potentially modified permalink.
}, 10, 2 );
// Redirect to the product with the cart edit query parameter if it is set.
add_action( 'template_redirect', function () {
// Make sure we are on a single product page.
// Then check if the cart edit query parameter is set.
// If so, redirect to the product page with the cart edit query parameter, if it is not already set and the product is in the cart.
if ( is_product() ) {
if ( ! isset( $_GET['wc_gforms_cart_item_key'] ) ) {
$cart_item_key = wc_gfpa_get_cart_edit_key( get_the_ID() );
// Get the cart item from the cart.
$cart_item = WC()->cart->get_cart_item( $cart_item_key );
// If the cart item is found, redirect to the product page with the cart edit query parameter.
if ( ! empty( $cart_item ) ) {
wp_safe_redirect( add_query_arg( [ 'wc_gforms_cart_item_key' => $cart_item_key ], get_permalink() ) );
exit;
}
}
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment