Code Snippets

CBR PRO provides filter hooks to customize its behavior with a few lines of PHP. Add the snippets below to your child theme’s functions.php file, or with a code-snippets plugin. (Don’t edit the parent theme — changes are lost on theme updates.) Snippets verified against CBR PRO 4.4.x.

INFO
Most customizations no longer need code: redirecting the 404 page, the cart restriction message, bypassing restrictions for specific users, and message positions are all built-in settings — see Setup & Configuration. Use a snippet only for what the settings can’t do. Where a filter and a setting affect the same text, the filter runs last and wins.

The country a visitor selects is stored in the country cookie for 30 days by default (see How CBR PRO Detects the Customer’s Country). To change the lifetime, return the number of days from the set_cookie_expiration filter:

add_filter( 'set_cookie_expiration', function( $days ) {
	return 7; // Cookie lifetime in days.
} );

Customize the Cart Removal Message in Code

The Cart restriction message setting in Setup & Configuration covers most cases. Use the cbr_cart_item_removed_message filter when you need logic the setting can’t express — for example, a different message per product. The filter receives the message and the product, and its return value overrides the setting:

add_filter( 'cbr_cart_item_removed_message', function( $message, $product ) {
	return sprintf(
		'%s is not available in your country and was removed from your cart.',
		$product->get_name()
	);
}, 10, 2 );

Restrict a Product Programmatically

The cbr_is_restricted filter lets you mark a product as restricted from code, on top of the rules configured in the admin — useful for custom conditions (stock source, customer role, third-party data). Return true to restrict the given product. Note: this filter can only add restrictions; it cannot lift a restriction set by a rule (to exempt users, use the Bypass Restriction for Specific Users setting):

add_filter( 'cbr_is_restricted', function( $restricted, $product ) {
	// Example: always restrict product 123.
	if ( 123 === $product->get_id() ) {
		return true;
	}
	return $restricted;
}, 10, 2 );

Change the Analytics Retention Period

The Restriction Analytics & Insights Dashboard keeps its event log for 90 days by default. Change it with the cbr_pro_analytics_retention_days filter:

add_filter( 'cbr_pro_analytics_retention_days', function( $days ) {
	return 180; // Keep analytics data for 180 days.
} );

Hook Reference

  • set_cookie_expiration — lifetime of the country cookie in days (default: 30).
  • cbr_is_restricted ($restricted, $product) — return true to restrict a product programmatically; applied to products and variations everywhere restriction is checked.
  • cbr_cart_item_removed_message ($message, $product) — the message shown when a restricted product is removed from the cart. (The older cbr_cart_message filter still runs for backward compatibility; cbr_cart_item_removed_message runs after it.)
  • cbr_post_per_page ($per_page) — products per page used when CBR PRO rebuilds restricted product listings (default: the WordPress posts_per_page option).
  • cbr_pro_analytics_retention_days ($days) — how long analytics events are kept (default: 90).

Next Steps