Skip to content
Advertisement

How to upsell product in woocoomerce based on last product’s variation

I hope someone with more coding knowledge nouse will know more on how to tackle this little upsell issue I have in WordPress.

The idea is in a side cart, based on the last product’s variation, it will show an upsell product in the side cart. Currently I am using a Woo side cart plugin that displays a random upsell in the side cart but I want to overwrite this via code.

So in my php snippets, I have written the code where it takes the last product’s variation as so:

JavaScript

So now the idea is that based on that variation, display any product that is higher in price with the same variation to upsell. If these isn’t one then display a product with same variation but lower price. If there isn’t one then display a random random upsell of any product.

So example: I add to cart a coffee bag which is “250g and wholebean” for £29.99. For an upsell, I hope it displays a product which is the same “250g and wholebean” and the price is £39.99.

So I have two issues.

1: How to code the upsell to match the above scenario 2: I have a side cart that contains a random upsell. I need to overwrite this random upsell with this code but now sure how to do that?

Please view the site here so you can see: https://shop.balancecoffee.co.uk/

Thank you

Advertisement

Answer

All products in WooCommerce have a configurable upsell in the CMS – this can be accessed on $product->get_upsell_ids(). This should be used to generate upsell products based on your product in the carts.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement