Skip to content

Instantly share code, notes, and snippets.

@etiennetremel
Last active June 27, 2023 00:43
Show Gist options
  • Save etiennetremel/6288904 to your computer and use it in GitHub Desktop.
Save etiennetremel/6288904 to your computer and use it in GitHub Desktop.
WooCommerce Upsells Upsells as checkbox. When product is deleted from the cart, upsells are also deleted.
<?php
/**
* The template for displaying product content in the single-product.php template
*
* Override this template by copying it to yourtheme/woocommerce/content-single-product.php
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>
<?php
/**
* woocommerce_before_single_product hook
*
* @hooked woocommerce_show_messages - 10
*/
do_action( 'woocommerce_before_single_product' );
?>
<div itemscope itemtype="http://schema.org/Product" id="product-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
/**
* woocommerce_show_product_images hook
*
* @hooked woocommerce_show_product_sale_flash - 10
* @hooked woocommerce_show_product_images - 20
*/
do_action( 'woocommerce_before_single_product_summary' );
?>
<div class="summary entry-summary">
<?php
/**
* woocommerce_single_product_summary hook
*
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce_template_single_excerpt - 20
* @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50
*/
do_action( 'woocommerce_single_product_summary' );
?>
<?php global $product, $woocommerce, $woocommerce_loop; ?>
<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype="multipart/form-data">
<?php do_action('woocommerce_before_add_to_cart_button'); ?>
<?php
if ( ! $product->is_sold_individually() )
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )
) );
?>
<hr class="clear" />
<?php
$upsells = $product->get_upsells();
if ( sizeof( $upsells ) == 0 ) return;
$meta_query = $woocommerce->query->get_meta_query();
$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 ),
'meta_query' => $meta_query
);
$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" class="single_add_to_cart_button button alt"><?php echo apply_filters('single_add_to_cart_text', __( 'Add to cart', 'woocommerce' ), $product->product_type); ?></button>
</form>
</div><!-- .summary -->
<?php
/**
* woocommerce_after_single_product_summary hook
*
* @hooked woocommerce_output_product_data_tabs - 10
* @hooked woocommerce_output_related_products - 20
*/
do_action( 'woocommerce_after_single_product_summary' );
?>
</div><!-- #product-<?php the_ID(); ?> -->
<?php do_action( 'woocommerce_after_single_product' ); ?>
<?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' );
@MrJoshFisher
Copy link

Got an issue with the upsells not working well with variable products wth options,

Simple product works fine but when using in conjunction with a variable product with an option it doesn't seem to be adding it to the basket on add to cart click. Just sits there doing nothing.

@tradeseeker88
Copy link

Cause of variations aren't located in content-single-product.php
just take a look at /single-product/add-to-cart/ variable.php

@mg33dev
Copy link

mg33dev commented Sep 8, 2017

Good work, but any chance of updating to woocommerce 3x? Lots of errors due to depreciation. Mainly the way ID's being called.

"Notice: WC_Product::get_upsells is deprecated since version 3.0! Use WC_Product::get_upsell_ids instead."

@swoboda
Copy link

swoboda commented Nov 7, 2017

Thank you for this! This saved my life.

However I had problems with the last function - removing upsells from the cart. I changed the hook to woocommerce_remove_cart_item and it works now:

add_action( 'woocommerce_remove_cart_item', 'remove_upsells_from_cart' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment