/**
* Helper: get all producers / subsites that are currently in the cart.
* Returns:
* [ blog_id => [ 'term_name' => string ] ]
*/
function ms_get_cart_producers_for_notice() {
if ( ! WC()->cart ) {
return array();
}
$producers = array();
$current_blog_id = get_current_blog_id();
$has_producers = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = ! empty( $cart_item['variation_id'] )
? $cart_item['variation_id']
: $cart_item['product_id'];
// Get producer term
$terms = wp_get_post_terms( $product_id, 'producer' );
if ( is_wp_error( $terms ) || empty( $terms ) ) {
continue;
}
$term = $terms[0];
// Blog ID is stored as term meta, same as in your shipping function
$producer_blog_id = (int) get_term_meta( $term->term_id, 'blog_id', true );
if ( ! $producer_blog_id ) {
continue;
}
$has_producers = true;
if ( ! isset( $producers[ $producer_blog_id ] ) ) {
$producers[ $producer_blog_id ] = array(
'term_name' => $term->name,
);
}
}
// Fallback if we are on a subsite and no "producer" taxonomy was used
if ( ! $has_producers && (int) $current_blog_id !== 1 ) {
$producers[ $current_blog_id ] = array(
'term_name' => get_option( 'blogname' ),
);
}
return $producers;
}
/**
* 3) Show per-producer shipping countries ("_shopinfo_versandlaender")
* as a WooCommerce info box on cart & checkout.
*
* Example:
* Producer 1 versendet nach Österreich
* Producer 2 versendet nach Österreich und Deutschland
*/
add_action( 'woocommerce_before_cart', 'ms_cart_producer_shipping_countries_notice', 5 );
add_action( 'woocommerce_before_checkout_form', 'ms_cart_producer_shipping_countries_notice', 5 );
function ms_cart_producer_shipping_countries_notice() {
// Frontend only
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
if ( ! WC()->cart || WC()->cart->is_empty() ) {
return;
}
$producers = ms_get_cart_producers_for_notice();
if ( empty( $producers ) ) {
return;
}
// All WooCommerce countries (code => label)
$countries_obj = WC()->countries;
$all_countries = $countries_obj->get_countries();
$lines = array();
foreach ( $producers as $blog_id => $data ) {
$label = ! empty( $data['term_name'] ) ? $data['term_name'] : get_blog_option( $blog_id, 'blogname' );
// Read "_shopinfo_versandlaender" from the producer's subsite
switch_to_blog( $blog_id );
$shipping_countries = get_option( 'options_shopinfo_versandlaender', array() );
restore_current_blog();
if ( empty( $shipping_countries ) ) {
continue;
}
// Normalize: can be array or comma-separated string
if ( ! is_array( $shipping_countries ) ) {
$shipping_countries = array_filter(
array_map( 'trim', explode( ',', (string) $shipping_countries ) )
);
}
if ( empty( $shipping_countries ) ) {
continue;
}
$country_names = array();
foreach ( $shipping_countries as $entry ) {
$code = '';
$label_from_acf = '';
// ACF return format could be "value", "label" or "array"
if ( is_array( $entry ) ) {
if ( ! empty( $entry['value'] ) ) {
$code = $entry['value'];
}
if ( ! empty( $entry['label'] ) ) {
$label_from_acf = $entry['label'];
}
} else {
$code = (string) $entry;
}
if ( $code && isset( $all_countries[ $code ] ) ) {
$country_names[] = $all_countries[ $code ];
} elseif ( $label_from_acf ) {
$country_names[] = $label_from_acf;
} elseif ( $code ) {
// Fallback: use raw value if it's not a known code
$country_names[] = $code;
}
}
if ( empty( $country_names ) ) {
continue;
}
// Turn array into "Österreich", or "Österreich und Deutschland", or
// "Österreich, Deutschland und Italien"
$countries_string = '';
$count = count( $country_names );
if ( 1 === $count ) {
$countries_string = $country_names[0];
} else {
$last = array_pop( $country_names );
$countries_string = implode( ', ', $country_names ) . ' und ' . $last;
}
$label_safe = esc_html( $label );
$countries_string_safe = esc_html( $countries_string );
$lines[] = $label_safe . ' versendet nach ' . $countries_string_safe;
}
if ( empty( $lines ) ) {
return;
}
// Build WooCommerce info box content
$message = '' . esc_html__( 'Versandländer der Produzenten in deinem Warenkorb', 'your-textdomain' ) . ':
';
$message .= implode( '
', $lines );
// Show as WooCommerce "info" box (notice)
wc_print_notice( $message, 'notice' );
}