WooCommerce:在挂钩函数中获取产品变体自定义字段值



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

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

我只需要得到值(这是浮点数(:

在las函数中存在一些错误和遗漏(请参阅下文(。

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

1( 。使用WC_Dataget_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;
}

最新更新