【问题标题】:WooCommerce: Display custom checkout fields if specific product IDs are in cartWooCommerce:如果特定产品 ID 在购物车中,则显示自定义结帐字段
【发布时间】:2019-05-12 19:16:34
【问题描述】:

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

当某个 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;
}

【问题讨论】:

    标签: php wordpress woocommerce cart checkout


    【解决方案1】:

    要使您的条件函数适用于许多产品 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-&gt;cart 已被WC()-&gt;cart 取代

    【讨论】:

      猜你喜欢
      • 2019-05-11
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 2018-08-28
      • 2018-07-16
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      相关资源
      最近更新 更多