add_shortcode( 'ccom_order_bump', function( $atts ) {
// WooCommerce Or Bail
if( ! class_exists( 'WooCommerce' ) ) {
return;
}
// Front End Or Bail
if( is_admin() ) {
return;
}
// Bail On Null Or Empty Cart
if( is_null( WC()->cart ) || WC()->cart->is_empty() ) {
return;
}
// Get And Loop Block Patterns
$output = '';
$args = [
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'wp_block',
'posts_per_page' => -1,
'status' => 'publish',
];
foreach( get_posts( $args ) as $block ) {
// Only With -for- Within Post Slug
if( strstr( $block->post_name, '-for-' ) === false ) {
continue;
}
// Get Upsell And Trigger Products
list( $upsell_product_names, $trigger_product_name )
= explode( ' -for- ', $block->post_title, 2 );
$upsell_product_names = explode( ' -and- ', $upsell_product_names );
// Upsell Products Must Not Be On Cart
foreach( $upsell_product_names as $upsell_product_name ) {
foreach( WC()->cart->get_cart() as $cart_item ) {
$cart_item_product = wc_get_product( $cart_item['data']->get_id() );
if( $cart_item_product->is_type( 'variation' ) ) {
$cart_item_product = wc_get_product( $cart_item_product->get_parent_id() );
}
if(
$cart_item_product->get_title() === $upsell_product_name
|| $cart_item_product->get_slug() === $upsell_product_name
) {
continue 3;
}
}
}
// Match Trigger Product
$trigger_product = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$cart_item_product = wc_get_product( $cart_item['data']->get_id() );
if( $cart_item_product->is_type( 'variation' ) ) {
$cart_item_product = wc_get_product( $cart_item_product->get_parent_id() );
}
if(
$cart_item_product->get_title() === $trigger_product_name
|| $cart_item_product->get_slug() === $trigger_product_name
) {
$trigger_product = true;
}
}
if( $trigger_product ) {
$output .= apply_filters( 'the_content', $block->post_content );
}
}
return $output;
} );
add_action( 'woocommerce_before_cart', function() {
do_shortcode( '[ccom_order_bump]' );
} );
add_action( 'woocommerce_before_checkout_form', function() {
do_shortcode( '[ccom_order_bump]' );
} );
Cart and Checkout order bump up-sell
Places a special offer onto Cart and Checkout pages when product in offer isn’t already in the cart. Supports simple and variable product types.
This is a basic version of several official extensions you might consider: Product Recommendations, Cart Add-ons, Cart Notices, WooCommerce Checkout Add-Ons.
This is one of three useful up-sell features available in my plugin Up-Sell Trio for WooCommerce.
Instructions for Cart and Checkout order bump up-sell
- Log into a staging or locally hosted clone of your site.
- Install and activate Code Snippets plugin.
- WP Admin > Snippets > Add New
- Copy and paste the code from the section above.
- Check to ensure formatting came over properly.
- Customize the code as desired.
- Add a meaningful title.
- Select whether to run on front-end or back-end (or both).
- Click “Save and Activate”.
- Test your site to ensure it works.
- Disable if any problems, or recover.
- Repeat for live environment.
Need help modifying Cart and Checkout order bump up-sell?
Contact me. I can help with fitting projects or refer to my partner.
License
All code snippets are licensed GPLv2 (or later) matching WordPress licensing.
Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.
Disclaimer of warranty