Skip to content

Exclude shipping method for gallery

You may want to exclude a specific shipping method when items in the cart are from specific galleries. Replace the values for the gallery IDs you want to look for and the shipping method instance ID. This code will look for any child galleries of the gallery IDs you provide.

You can also try this code snippet which allows you to include specific shipping methods.

add_filter( 'sunshine_allowed_shipping_methods', 'sunshine_exclude_shipping_method', 999 );
function sunshine_exclude_shipping_method( $shipping_methods ) {

	$cart_items                      = SPC()->cart->get_cart();
	$exclude_from_gallery_ids        = array( 3048, 9485, 2284 ); // Your gallery IDs go here.
	$shipping_instance_id_to_exclude = 'a3fad0ed8620ba36be97ade1d2af8358'; // Shipping method instance ID.

	// Get all children of the excluded galleries.
	$all_excluded_gallery_ids = $exclude_from_gallery_ids;

	foreach ( $exclude_from_gallery_ids as $parent_id ) {
		$descendants = sunshine_get_gallery_descendant_ids( $parent_id );
		if ( ! empty( $descendants ) ) {
			$all_excluded_gallery_ids = array_merge( $all_excluded_gallery_ids, $descendants );
		}
	}

	$all_excluded_gallery_ids = array_unique( $all_excluded_gallery_ids );

	// Check cart items for excluded gallery IDs
	if ( ! empty( $cart_items ) ) {
		foreach ( $cart_items as $item ) {
			if ( ! empty( $item['gallery_id'] ) ) {
				if ( in_array( (int) $item['gallery_id'], $all_excluded_gallery_ids, true ) ) {
					unset( $shipping_methods[ $shipping_instance_id_to_exclude ] );
				}
			}
		}
	}

	return $shipping_methods;
}

Learn how to add this custom code to your WordPress website

You can learn how to find the Gallery ID here.

To find the Shipping Instance ID, go to Sunshine > Settings > Delivery & Shipping > Edit your shipping method to find the Instance ID.

Screenshot showing where to find the shipping method instance ID

Still need help?

If you have not yet found your answer in the documentation articles, please contact support

Sunshine Photo Cart for WordPress