在 Woocommerce 中添加基于购物车重量的自定义结帐字段

Add custom Checkout field based on Cart weight in Woocommerce

在结帐页面上,如果购物车中的物品重量超过 1000 公斤,我想添加一个自定义字段。

有没有办法添加一个 if 语句(形成-checkout.php?)它获取购物车重量然后我可以添加一个 select 字段?

也先读the official related documentation to customize checkout fields or this documentation

这是一个示例,当购物车重量超过 1000 千克(1 吨)时,结帐页面将添加自定义账单 select 字段:

add_filter( 'woocommerce_checkout_fields' , 'customizing_checkout_fields', 10, 1 );
function customizing_checkout_fields( $fields ) {

    if( WC()->cart->get_cart_contents_weight() > 1000 ) {

        // Custom Select field
        $fields['billing']['billing_custom'] = array(
            'type'     => 'select',
            'label'    => __("Cart weight over 1 Ton", "woocommerce"),
            'class'    => array('form-row-wide'),
            'options'  => array(
                ''         => __("Choose an option please…", "woocommerce"),
                'option-1' => __("Option 1", "woocommerce"),
                'option-2' => __("Option 1", "woocommerce"),
                'option-3' => __("Option 1", "woocommerce"),
            ),
            'priority' => '120',
            'required' => true,
        );
    }

    return $fields;
}

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