Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xlplugins/71addd9209c1b33689cd38ccf96f812f to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/71addd9209c1b33689cd38ccf96f812f to your computer and use it in GitHub Desktop.
FunnelKit Cart: Exclude Bundled Product Upsells
/**
* Exclude bundled product upsells from cart "Frequently Bought Together".
* Drop this in wp-content/mu-plugins/ or add to theme functions.php.
*
* Requires: WooCommerce Product Bundles + FunnelKit Cart + fkcart_upsell_ids filter in upsells.php
*/
add_filter( 'fkcart_upsell_ids', function ( $upsell_ids ) {
if ( ! WC()->cart || empty( $upsell_ids ) ) {
return $upsell_ids;
}
// Product IDs that are in cart as standalone (not only bundled).
$standalone_product_ids = [];
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
if ( ! empty( $cart_item['bundled_by'] ) ) {
continue;
}
$product = $cart_item['data'] ?? null;
if ( $product instanceof WC_Product ) {
$id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();
$standalone_product_ids[ $id ] = true;
}
}
$bundled_upsell_ids = [];
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
if ( empty( $cart_item['bundled_by'] ) ) {
continue;
}
$product = $cart_item['data'] ?? null;
if ( ! $product instanceof WC_Product ) {
continue;
}
$product_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();
// Skip: product is also in cart as standalone, so its upsells should show.
if ( ! empty( $standalone_product_ids[ $product_id ] ) ) {
continue;
}
$p = wc_get_product( $product_id );
if ( $p instanceof WC_Product ) {
$bundled_upsell_ids = array_merge(
$bundled_upsell_ids,
$p->get_upsell_ids(),
$p->get_cross_sell_ids()
);
}
}
$bundled_upsell_ids = array_unique( array_map( 'absint', $bundled_upsell_ids ) );
return array_values( array_diff( $upsell_ids, $bundled_upsell_ids ) );
}, 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment