This WordPress code snippet is tailored for WooCommerce-powered online stores. It strategically places upsell products just below the add-to-cart button on single product pages. The upsell products are displayed using WooCommerce’s built-in
woocommerce_upsell_display function, ensuring seamless integration with your store’s design and layout
// Add upsells below the add-to-cart button add_action( 'woocommerce_single_product_summary', 'woocommerce_upsell_display', 40 );
How This Snippet Works:
- Hooking into WooCommerce: The
add_actionfunction connects to the
woocommerce_single_product_summaryhook, which activates on single product pages in WooCommerce.
- Positioning the Upsell Display: By setting the priority to 40 in the
add_actionfunction, the upsell display is placed right below the add-to-cart button, which typically has a priority of 30.
- Displaying Upsell Products: The
woocommerce_upsell_displayfunction is used to show upsell products, automatically fetching the upsells defined in your WooCommerce product settings.