Skip to content

Include shipping methods for gallery

In the other documentation article on excluding shipping methods for a gallery, we can also include specific shipping methods for selected 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.

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

	$cart_items = SPC()->cart->get_cart();

	// Define allowed shipping methods per gallery ID
	$allowed = array(
		'a3fad0ed8620ba36be97ade1d2af8358' => array( 3048, 9485 ), // Shipping method instance with gallery IDs
		'abc123abc123abc123abc123abc123ab' => array( 2284, 9999 ), // Shipping method instance ID with gallery IDs
	);

	// Expand allowed galleries to include children
	foreach ( $allowed as $instance_id => $gallery_ids ) {
		$all_ids = $gallery_ids;

		foreach ( $gallery_ids as $gallery_id ) {
			$descendants = sunshine_get_gallery_descendant_ids( $gallery_id );
			if ( ! empty( $descendants ) ) {
				$all_ids = array_merge( $all_excluded_gallery_ids, $descendants );
			}
		}

		$allowed[ $instance_id ] = array_unique( $all_ids );
	}

	// Collect all gallery IDs from the cart
	$cart_gallery_ids = array();

	foreach ( $cart_items as $item ) {
		if ( ! empty( $item['gallery_id'] ) ) {
			$cart_gallery_ids[] = (int) $item['gallery_id'];
		}
	}

	$cart_gallery_ids = array_unique( $cart_gallery_ids );

	// Determine which shipping methods should be allowed
	$final_allowed = array();

	foreach ( $allowed as $instance_id => $allowed_gallery_ids ) {
		foreach ( $cart_gallery_ids as $gallery_id ) {
			if ( in_array( $gallery_id, $allowed_gallery_ids, true ) ) {
				$final_allowed[] = $instance_id;
				break;
			}
		}
	}

	// If no matching gallery IDs found, return all shipping methods unfiltered
	if ( empty( $final_allowed ) ) {
		return $shipping_methods;
	}

	// Only return the shipping methods that are allowed
	foreach ( $shipping_methods as $id => $method ) {
		if ( ! in_array( $id, $final_allowed, true ) ) {
			unset( $shipping_methods[ $id ] );
		}
	}

	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