# Ship a Category Only to Certain Countries

> Stops checkout when the cart contains products from a restricted category, such as batteries or plants, and the shipping country is not on your allowed list.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, shipping, countries, checkout
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-category-shipping-countries/

## Code

```php
$wpide_restricted_cats      = array( 'plants' );          // Category slugs.
$wpide_restricted_countries = array( 'GB', 'IE' );        // Countries these may ship to.

add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) use ( $wpide_restricted_cats, $wpide_restricted_countries ) {
    $country = ! empty( $data['ship_to_different_address'] ) ? ( $data['shipping_country'] ?? '' ) : ( $data['billing_country'] ?? '' );

    if ( '' === $country || in_array( $country, $wpide_restricted_countries, true ) || ! WC()->cart ) {
        return;
    }

    $names = array();

    foreach ( WC()->cart->get_cart() as $item ) {
        if ( has_term( $wpide_restricted_cats, 'product_cat', $item['product_id'] ) ) {
            $names[] = $item['data']->get_name();
        }
    }

    if ( $names ) {
        $errors->add( 'shipping', sprintf( 'Sorry, we cannot ship %s to %s. Please remove it to continue.', esc_html( implode( ', ', array_unique( $names ) ) ), esc_html( WC()->countries->countries[ $country ] ?? $country ) ) );
    }
}, 10, 2 );
```

## Notes

Uses two-letter country codes, and the error message names the products that cannot be shipped.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Ship a Category Only to Certain Countries" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
