首页 > 解决方案 > WooCommerce:如果购物车中有特定的产品 ID,则显示自定义结帐字段

问题描述

我正在经营一家卖烤架的在线商店。只有烤架通过需要额外信息的特定承运人运输。

当购物车中有某个 product_ID 时,我设法显示了一个下拉列表。选择特定值时,将显示文本区域。

这应该发生在大约 10 种产品上,而不仅仅是一种。

在阅读了许多主题并在网上搜索后,我不知道如何添加多个产品 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;
}

标签: phpwordpresswoocommercecartcheckout

解决方案


要使您的条件函数适用于许多产品 ID,请使用以下内容:

// 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>';
    }
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。

注意: $global $woocommerce; +$woocommerce->cart已被替换为WC()->cart


推荐阅读