You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让WooCommerce的delivery_time字段仅显示于租赁产品而非押金产品

解决Woocommerce中仅在租赁产品显示delivery_time字段的问题

问题分析

现有代码存在两个核心问题导致delivery_time字段出现在押金产品上:

  1. 未明确排除自动添加的押金产品(ID:240),即使押金产品不属于租赁分类,也可能因全局产品对象的逻辑触发字段渲染
  2. delivery_time_save_field_value_to_cart_data函数中存在语法错误:使用赋值运算符=而非比较运算符==,导致字段逻辑异常

修改后的完整代码

// 仅在租赁产品(非押金产品)显示delivery_time字段
function delivery_time_before_add_to_cart_field() {
    global $product;

    // 排除押金产品,同时只在rent分类产品显示字段
    $deposit_product_id = 240;
    if ( $product->get_id() == $deposit_product_id || !has_term( 'rent', 'product_cat', $product->get_id() ) ) {
        return;
    }

    echo '<div class="delivery_time-before-add-to-cart-field">
        <label for="delivery_time"><h4>期望配送时间:</h4></label>
        <input class="delivery_class" type="time" id="delivery_time" name="delivery_time" placeholder="12:00" maxlength="15">
    </div>';
}

// 仅验证租赁产品的delivery_time字段
add_action( 'woocommerce_add_to_cart_validation', 'delivery_field_validation', 10, 3 );
function delivery_field_validation( $result, $product_id, $quantity ) {
    $deposit_product_id = 240;
    // 排除押金产品,仅对rent分类产品做字段验证
    if ( $product_id != $deposit_product_id && has_term( 'rent', 'product_cat', $product_id ) && empty( $_REQUEST[ 'delivery_time' ] ) ) {
        wc_add_notice( '请填写期望配送时间!', 'error' );
        return false;
    }

    return $result;
}

// 仅将delivery_time字段值保存到租赁产品的购物车数据中
add_filter( 'woocommerce_add_cart_item_data', 'delivery_time_save_field_value_to_cart_data', 10, 3 );
function delivery_time_save_field_value_to_cart_data( $cart_item_data, $product_id, $variation_id ) {
    $deposit_product_id = 240;
    // 修正赋值错误为比较,同时排除押金产品
    if( !empty( $_POST[ 'delivery_time' ] ) && $product_id == 149 && $product_id != $deposit_product_id ) {
        $cart_item_data[ 'delivery_time' ] = sanitize_text_field( $_POST[ 'delivery_time' ] );
    }

    return $cart_item_data;
}

// 在购物车中显示delivery_time字段(仅针对有该数据的商品)
add_filter( 'woocommerce_get_item_data', 'delivery_time_display_field', 10, 2 );
function delivery_time_display_field( $item_data, $cart_item ) {
    if( !empty( $cart_item[ 'delivery_time' ] ) ) {
        $item_data[] = array(
            'key'     => '期望配送时间',
            'value'   => $cart_item[ 'delivery_time' ],
            'display' => '',
        );
    }

    return $item_data;
}

// 将delivery_time字段添加到订单行项目中
add_action( 'woocommerce_checkout_create_order_line_item', 'delivery_time_add_order_item_meta', 10, 4 );
function delivery_time_add_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( !empty( $values[ 'delivery_time' ] ) ) {
        $item->add_meta_data( '期望配送时间', $values[ 'delivery_time' ] );
    }
}

/*
* 自动将押金产品添加到购物车
*/
function deposit_add_product_to_cart( $item_key, $product_id ) {
    $product_category_id = 23; // 需要添加额外商品的分类ID
    $free_product_id = 240; // 自动添加的押金产品ID

    $product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );

    if ( !is_admin() && in_array( $product_category_id, $product_cats_ids ) ) {
        $found = false;

        // 检查押金产品是否已在购物车中
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->get_id() == $free_product_id )
                    $found = true;
            }
            // 如果未找到则添加
            if ( !$found )
                WC()->cart->add_to_cart( $free_product_id );
        } else {
            // 购物车为空时直接添加
            WC()->cart->add_to_cart( $free_product_id );
        }        
    }    
}
add_action( 'woocommerce_add_to_cart', 'deposit_add_product_to_cart', 10, 2 );

关键修改说明

  • 全局排除押金产品:在字段显示、验证、数据保存的三个核心函数中,新增对押金产品ID(240)的判断,确保该产品完全不触发delivery_time相关逻辑
  • 修正语法错误:将delivery_time_save_field_value_to_cart_data函数中的$product_id = 149改为$product_id == 149,恢复正确的产品判断逻辑
  • 本地化字段文本:将英文提示改为中文,提升用户体验

内容的提问来源于stack exchange,提问作者Ville

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:02:25