2
votes

I am running an online-store selling grills. Only the grills are shipped via a specific carrier where additional informations are needed.

I managed to display a dropdown when a certain product_ID is in the cart. When a specific value is selected a text-area is displayed.

This should happen for about 10 products, not just one.

After reading a number of threads and searching the web, i cant figure out, how to add more than one product_ID.

add_action( 'woocommerce_after_order_notes', 'grills_versandauswahl_checkout_field' );
function grills_versandauswahl_checkout_field( $checkout ) {

    $grill_in_cart = grills_is_conditional_product_in_cart ( 125 );

    if ( $grill_in_cart === true ) {
        echo '<div id="my_custom_checkout_field"><h3>' . __(     'Versandoptionen' ) . '</h3><p style="margin: 0 0 8px;">text</p>';

        woocommerce_form_field( 'versandoption', array(
            'type'          => 'select',
            'class'         => array( 'wps-drop' ),
            'label'         => __( 'Versandoptionen' ),
            'required'      => true,
            'options'       => array(
                'blank'     => __( 'Ausw&auml;hlen', 'wps' ),
                'fixtermin' => __( 'Fixtermin', 'wps' ),
                'avis'      => __( 'Telefonisches Avis', 'wps' ),
            )
        ), $checkout->get_value( 'versandoption' ) );

        woocommerce_form_field( 'inscription_textbox', array(
            'type'  => 'textarea',
            'class' => array( 'inscription-text form-row-wide' ),
            'label' => __( 'Wunschtermin / Ablageort' ),
        ), $checkout->get_value( 'inscription_textbox' ) );

        echo '</div>';
    }
}

function grills_is_conditional_product_in_cart( $product_id ) {
    //Check to see if user has product in cart
    global $woocommerce;

    //flag no book in cart
    $grill_in_cart = false;

    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if ( $_product->id === $product_id ) {
            //book is in cart!
            $grill_in_cart = true;
        }
    }
    return $grill_in_cart;
}
1

1 Answers

0
votes

To make your conditional function work for many product Ids use the following:

// Custom conditional function
function grills_in_cart( $product_ids ) {
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( array_intersect($product_ids, array( $item['product_id'], $item['variation_id']) ) ) {
            return true;
        }
    }
    return false;
}

// Conditionally display custom checkout fields for specific product IDS
add_action( 'woocommerce_after_order_notes', 'grills_versandauswahl_checkout_field' );
function grills_versandauswahl_checkout_field( $checkout ) {
    // Here define the targeted product IDs in this array
    $targeted_ids = array( 125, 132, 154 );
    $text_domain  = 'woocommerce';

    if ( grills_in_cart( $targeted_ids ) ) {
        echo '<div id="my_custom_checkout_field">
        <h3>' . __( "Versandoptionen", $text_domain ) . '</h3>
        <p style="margin: 0 0 8px;">' . __( "text", $text_domain ) . '</p>';

        woocommerce_form_field( 'versandoption', array(
            'type'     => 'select',
            'class'    => array('wps-drop'),
            'label'    => __( "Versandoptionen", $text_domain ),
            'required' => true,
            'options'  => array(
                'blank'     => __( "Ausw&auml;hlen", $text_domain ),
                'fixtermin' => __( "Fixtermin", $text_domain ),
                'avis'      => __( "Telefonisches Avis", $text_domain ),
            ),
        ), $checkout->get_value( 'versandoption' ) );

        woocommerce_form_field( 'inscription_textbox', array(
            'type'  => 'textarea',
            'class' => array( 'inscription-text form-row-wide' ),
            'label' => __( 'Wunschtermin / Ablageort', $text_domain ),
        ), $checkout->get_value( 'inscription_textbox' ) );

        echo '</div>';
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Note: $global $woocommerce; + $woocommerce->cart has been replaced by WC()->cart