Skip to content

Instantly share code, notes, and snippets.

@thenbrent
Last active September 17, 2018 12:08
Show Gist options
  • Save thenbrent/7254185 to your computer and use it in GitHub Desktop.
Save thenbrent/7254185 to your computer and use it in GitHub Desktop.
Display a notice on checkout when a product was removed from the cart after a subscription was added.
<?php
/**
* Plugin Name: WooCommerce Subscriptions Product Removed Message
* Description: Display a notice on checkout when a product was removed from the cart after a subscription was added.
* Author: Gerhard Potgieter & Brent Shepherd
* Author URI:
* Version: 1.1
* License: GPL v2
*/
/**
* Set a flag to indicate that the error message needs to be displayed. We can add the message to the $woocommerce global
* yet because it will be removed by Subscriptions later on the 'add_to_cart_redirect' hook.
*/
function eg_set_product_removed_message( $valid, $product_id, $quantity ) {
global $woocommerce, $eg_set_product_removed_message;
if ( $woocommerce->cart->get_cart_contents_count() > 0 && WC_Subscriptions_Product::is_subscription( $product_id ) ) {
$eg_set_product_removed_message = $woocommerce->cart->get_cart_contents_count();
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'eg_set_product_removed_message', 9, 3 );
/**
* If the product removed flag is set, now we can add the message.
*/
function eg_show_product_removed_message( $url ) {
global $woocommerce, $eg_set_product_removed_message;
if ( isset( $eg_set_product_removed_message ) && is_numeric( $eg_set_product_removed_message ) ) {
wc_add_notice( sprintf( _n( '%s product has been removed from your cart. Products and subscriptions can not be purchased at the same time.', '%s products have been removed from your cart. Products and subscriptions can not be purchased at the same time.', $eg_set_product_removed_message, 'wcsprm' ), $eg_set_product_removed_message ), 'error' );
}
return $url;
}
add_filter( 'add_to_cart_redirect', 'eg_show_product_removed_message', 11, 1 );
@lukecav
Copy link

lukecav commented Oct 20, 2017

As it is, since there is a plugin header in the code, you can install this as a plugin on your store. But if you just wanted to add the code snippet then you can copy from
https://gist.github.com/thenbrent/7254185#file-wcs-product-removed-error-message-php-L15-L38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment