Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
WooCommerce checkout message based for a variable product and a specific variable?

I have added a custom message that now appears below woocommerce_after_order_notes, But I just want it to be displayed for one of the variables of a product. The following code shows it only for the main product Please tell me how to change it for a variable.

add_action( 'woocommerce_after_order_notes', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
    // set your products IDs here:
    $product_ids = array( 91);
    $bool = false;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( in_array( $item->id, $product_ids ) )
            $bool = true;
    }
    // If the special cat is detected in one items of the cart
    // It displays the message
    if ($bool)
        echo '<div class="checkoutdisc">This is Your custom message displayed.</div>';
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can check if there is any variation_id in cart like this $cart_item['variation_id']. try the below code.

add_action( 'woocommerce_after_order_notes', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {

    // set your products IDs here:
    $product_ids = array( 82, 94 ); // add your both product and variation id.

    $parent_ids_in_cart = false;
    
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $item = $cart_item['data'];
        if ( in_array( $item->id, $product_ids ) ){
            $parent_ids_in_cart = true;
        }
    }

    $variation_ids_in_cart = false;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Collecting product variation IDs if they are in cart for this variable product
        if ( $cart_item['variation_id'] > 0 && in_array( $cart_item['variation_id'], $product_ids ) ){
            $variation_ids_in_cart = true;
        }
    }

    // It displays the message
    if ( $parent_ids_in_cart || $variation_ids_in_cart ){
        echo '<div class="checkoutdisc">This is Your custom message displayed.</div>';
    }

}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda