2

这就是我为产品变体添加管理自定义字段的方法:

add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
     
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
    woocommerce_wp_text_input( array(
        'id' => 'custom_field[' . $loop . ']',
        'class' => 'short',
        'label' => __( 'Custom Field', 'woocommerce' ),
        'value' => get_post_meta( $variation->ID, 'custom_field', true )
    ) );
}

保存产品变化的自定义字段保存:

add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i ) {
    $custom_field = $_POST['custom_field'][$i];
    if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}

将自定义字段值存储到变体数据中

add_filter( 'woocommerce_available_variation', 'add_custom_field_variation_data' );
function add_custom_field_variation_data( $variations ) {
    $variations ['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
    return $variations;
}

该值正在保存并显示在单个产品页面中,但以下函数未返回该值

尝试在函数中检索自定义字段的值:

add_filter('woocommerce_variation_prices_price', 'get_custom_variable_price' )
function get_custom_variable_price() {

    global $post;
    $custom_value = get_post_meta( $variations[ 'variation_id' ], 'custom_field', true );
    $custom_value = (float)$custom_value;
    return $custom_value;   
}

我只需要获取值(即浮点数)

4

1 回答 1

1

您的 las 函数中有一些错误和遗漏的东西(见下文)

现在有两种方法可以在该挂钩函数中获取自定义字段值:

1)。使用WC_Data get_meta()方法(自 WooCommerce 3 起)

add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
    $value = floatval( $variation->get_meta( 'custom_field' ) );
    if( $value > 0 ) {
        $price = $value; 
    }
    return $price;
}

2)。或使用 WordPressget_post_meta()功能(旧方式)

add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
    $value = floatval( floatval( get_post_meta( $variation->get_id(), 'custom_field', true ) );
    if( $value > 0  ) {
        $price = $value; 
    }
    return $price;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中。经过测试,两者都适用。

重要提示:您可能看不到前端的结果,因为可变的产品价格范围被缓存在 WooCommerce 中以提高性能(请参阅下面的链接线程以获得更好的理解)

相关主题:通过 WooCommerce 3+ 中的挂钩更改产品价格


与您的其他功能相关的添加

从 WooCommerce 3 开始,您可以按如下方式更新您的第二个和第三个函数:

add_action( 'woocommerce_admin_process_variation_object', 'save_custom_field_variation_value', 10, 2 );
function save_custom_field_variation_value( $variation, $i ) {
    if( isset($_POST['custom_field'][$i]) ) {
        $variation->update_meta_data( 'custom_field', floatval( sanitize_text_field($_POST['custom_field'][$i]) ) );
    }
}

和第三个功能

add_filter( 'woocommerce_available_variation', 'add_variation_custom_field_value_to_variation_data', 10, 3 );
function add_variation_custom_field_value_to_variation_data( $variation_data, $product, $variation ) {
    if ( $value = $variation->get_meta( 'custom_field' ) ) {
        $variation_data['custom_field'] = '<div class="woocommerce_custom_field">' .__("Custom Field") . ': <span>' . $value . '</span></div>';
    }
    return $variation_data;
}
于 2021-03-13T12:36:35.407 回答