Denne shortcode kan bruges til at skjule en “Passer til produktet” sektion hvis det nuværende produkt ingen mersalgs produkter har tilknyttet sig.
Du skal bruges pluginnet “Dynamic Conditions”: https://wordpress.org/plugins/dynamicconditions/
Brug “Dynamic Conditions” på det element som skal skjule. Indsæt shortcoden som Dynamic Tag vælg enten “Show” eller “Hide” sæt Condition til “Is equal to” eller “Is not equal til” sæt Compare Type til “Text” og sæt “0” eller “1” i Conditional value alt efter hvad der skal ske.
0 = Produktet har ingen mersalgsprodukter tilknyttet.
1 = Produktet har mersalgsprodukter tilknyttet.
function auxo_has_mersalg_shortcode( $atts ) {
// WooCommerce not active
if ( ! function_exists( 'wc_get_product' ) ) {
return '0';
}
// Get current product ID in a safe way
$product_id = 0;
if ( function_exists( 'is_product' ) && is_product() ) {
$product_id = get_the_ID();
}
if ( ! $product_id ) {
$product_id = get_queried_object_id();
}
if ( ! $product_id ) {
return '0';
}
// Get product object
$product = wc_get_product( $product_id );
if ( ! $product || ! is_a( $product, 'WC_Product' ) ) {
return '0';
}
// "Mersalgs produkter" assumed = upsells
$upsell_ids = $product->get_upsell_ids();
if ( empty( $upsell_ids ) ) {
return '0';
}
return '1';
}
add_shortcode( 'auxo_product_has_upsales', 'auxo_has_mersalg_shortcode' );