Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeff2Ma/fa7018ce7b965cdf9919 to your computer and use it in GitHub Desktop.
Save Jeff2Ma/fa7018ce7b965cdf9919 to your computer and use it in GitHub Desktop.
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment