WooCommerce add upsell product to the cart when product is added
Date : March 29 2020, 07:55 AM
may help you . 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. /**
* 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' );
|
Woocommerce add to cart event and upsell page
Date : March 29 2020, 07:55 AM
hope this fix your issue It looks like it must be wrapped by a statement checking against page ID using get_the_ID() function to prevent "Add to cat" redirect on a custom page.
|
Woocommerce Product Attribute Values picked from Upsell and Cross Sell
Tag : php , By : Joshua Johnson
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am displaying Upsell and Cross sell products on single product page. But unfortunately The Additional information tab which contains the attribute are picking from Upsell and Cross sell. , I fixed this problem by resetting the loop query. <?php wp_reset_query(); ?>
|
Upsell on WooCommerce Thank You Page using Cross-selling products
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The following get_post_meta doesn't necessarily returns a list of products: get_post_meta( $item['product_id'], '_crosssell_ids', true )
foreach ( $items as $item ) {
$product_crosssell_ids = get_post_meta( $item['product_id'], '_crosssell_ids', true );
if (is_array($product_crosssell_ids) && !empty($product_crosssell_ids)){
$cross_ids = array_unique( array_merge( $product_crosssell_ids, $cross_ids )) ;
}
}
|
Using checkboxes for variations in WooCommerce to allow multiple choice
Date : March 29 2020, 07:55 AM
|