WooCommerce add upsell product to the cart when product is added
woocommerce, wordpress
Solution
So finally, I found the solution to my question.
The problem was happening when the cart was loaded from the session via the function `get_cart_from_session()` from `class-wc-cart.php`.
When extra key are added to a cart item, we need to inject the key via the `woocommerce_get_cart_item_from_session` filter.
The user can now select an upsell, this upsell is automatically added to the cart at the same time than a a product is added. I also have a function which allow me to delete an upsell if the main product is deleted.
Here is my final code I added into my functions.php:
/**
* Add upsells as extra data for added product
*/
function add_upsells_to_cart( $cart_item_key ) {
global $woocommerce;
if ( empty( $_REQUEST['upsells'] ) || ! is_array( $_REQUEST['upsells'] ) )
return;
// Prevent loop
$upsells = $_REQUEST['upsells'];
unset( $_REQUEST['upsells'] );
// Append each upsells to product in cart
foreach( $upsells as $upsell_id ) {
$upsell_id = absint( $upsell_id );
// Add upsell into cart and set upsell_of as extra key with parent product item id
$woocommerce->cart->add_to_cart( $upsell_id, 1, '', '', array( 'upsell_of' => $cart_item_key ) );
}
}
add_action( 'woocommerce_add_to_cart', 'add_upsells_to_cart', 1, 6 );
/**
* Inject upsell_of extra key cart item key when cart is loaded from session
*/
function get_cart_items_from_session( $item, $values, $key ) {
if ( array_key_exists( 'upsell_of', $values ) )
$item[ 'upsell_of' ] = $values['upsell_of'];
return $item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3 );
/**
* Remove associated upsells if product removed from cart
*/
function remove_upsells_from_cart( $cart_item_key ) {
global $woocommerce;
// Get cart
$cart = $woocommerce->cart->get_cart();
// For each item in cart, if item is upsell of deleted product, delete it
foreach( $cart as $upsell_key => $upsell )
if ( $upsell['upsell_of'] == $cart_item_key )
unset( $woocommerce->cart->cart_contents[ $upsell_key ] );
}
add_action( 'woocommerce_before_cart_item_quantity_zero', 'remove_upsells_from_cart' );
Problem
I'm trying to implement a custom upsell function into my WooCommerce Theme. When an user add a product, I need to add extra products (upsell) to its cart. That mean I have to add an extra parameter to the product which allow me to get the relation product/upsells and delete associated upsells if the product is deleted from the cart. The user can choose these upsell products via a list of checkbox. Here is an overview of my "content-single-product.php" WooCommerce template: ``` <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype="multipart/form-data"> <?php // Get Upsells Product ID: $upsells = $product->get_upsells(); if ( sizeof( $upsells ) == 0 ) return; $meta_query = $woocommerce->query->get_meta_query(); // Get Upsells: $args = array( 'post_type' => 'product', 'ignore_sticky_posts' => 1, 'no_found_rows' => 1, 'posts_per_page' => $posts_per_page, 'orderby' => $orderby, 'post__in' => $upsells, 'post__not_in' => array( $product->id ) ); $query = new WP_Query( $args ); if ( $query->have_posts() ): ?> <ul> <?php while ( $query->have_posts() ): $query->the_post(); ?> <label for="upsell_<?php the_ID(); ?>"><input type="checkbox" name="upsells[]" id="upsell_<?php the_ID(); ?>" value="<?php the_ID(); ?>" /> <?php the_title(); ?></label> <?php endwhile; ?> </ul> <?php endif; ?> <?php wp_reset_postdata(); ?> <button type="submit">Add to cart</button> </form> ``` The code above has been simplified.. Then, here is the code I added into functions.php which allow me to add upsells to the cart and add an extra row to the associated product: ``` /** * Add upsells as extra data for added product */ function add_upsells_to_cart( $cart_item_key ) { global $woocommerce; if ( empty( $_REQUEST['upsells'] ) || ! is_array( $_REQUEST['upsells'] ) ) return; // Prevent loop $upsells = $_REQUEST['upsells']; unset( $_REQUEST['upsells'] ); // Add upsell_parent row (if not already existing) for the associated product if ( ! $cart_item_data['upsells'] || ! is_array( $cart_item_data['upsells'] ) ) $cart_item_data['upsells'] = array(); // Append each upsells to product in cart foreach( $upsells as $upsell_id ) { $upsell_id = absint( $upsell_id ); // Add extra parameter to the product which contain the upsells $woocommerce->cart->cart_contents[ $cart_item_key ]['upsells'] = $upsell_id; // Add upsell to cart $woocommerce->cart->add_to_cart( $upsell_id ); } } add_action( 'woocommerce_add_to_cart', 'add_upsells_to_cart' ); ``` Upsells are added to the cart as expected, but the extra row seems to be dropped by something in WC. When I do a `print_r( $woocommerce->cart->cart_contents[ $cart_item_key ] );` at the end of the previous function, the extra upsells parameter is visible, but when I display the cart from the cart page, the upsells parameter is dropped from the cart. Any idea? Thanks, E.