Forked from prasidhda/remove-cross-sell-products.php
Last active
November 27, 2019 07:06
-
-
Save techies23/bf8013da817c6b6660a4156cb0d36acd to your computer and use it in GitHub Desktop.
Remove Cross sell or up sell products from cart alongside main WooCommerce product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_cart_item_removed', 'my_plugin_remove_upsell_crossell', 10, 2); | |
function my_plugin_remove_upsell_crossell( $cart_item, $cart ) { | |
//GETTING SESSION ID PAIRS | |
$product_id_cart_item_pairs = WC()->session->get( 'product_id_cart_item_pairs', array() ); | |
$cart_item_product_pairs = WC()->session->get( 'cart_item_product_pairs', array() ); | |
// Get the product ID using session store | |
$product_id = isset( $product_id_cart_item_pairs[ $cart_item ] ) ? $product_id_cart_item_pairs[ $cart_item ] : false; | |
if ( ! $product_id ) { | |
return; | |
} | |
//get product cross sell products | |
$product = wc_get_product( $product_id ); | |
//If this is variation product | |
if ( $product->get_type() == 'variation' ) { | |
$main_product = wc_get_product( $product->get_parent_id() ); | |
} else { | |
$main_product = $product; | |
} | |
// ONLY REMOVE THE CODE YOU BASICALLY NEED TO REMOVE | |
// RELATES TO CROSSELL OR UPSELL | |
//Upsell Products | |
$upsell_products = $main_product->get_upsell_ids(); | |
if ( ! empty( $upsell_products ) ) { | |
foreach ( $upsell_products as $upsell_products ) { | |
//Get cart item Key | |
$upsell_product_cart_item_key = $cart_item_product_pairs[ $upsell_products ]; | |
if ( WC()->cart->find_product_in_cart( $upsell_product_cart_item_key ) ) { | |
WC()->cart->remove_cart_item( $upsell_product_cart_item_key ); | |
} | |
} | |
} | |
//Cross sell products | |
$cross_sell_products = $main_product->get_cross_sell_ids(); | |
if ( ! empty( $cross_sell_products ) ) { | |
foreach ( $cross_sell_products as $cross_sell_product_id ) { | |
//Get cart item Key | |
$cross_sell_product_cart_item_key = $cart_item_product_pairs[ $cross_sell_product_id ]; | |
if ( WC()->cart->find_product_in_cart( $cross_sell_product_cart_item_key ) ) { | |
WC()->cart->remove_cart_item( $cross_sell_product_cart_item_key ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment