我使用以下函数为每个产品变体添加一个名为“price\\u 1\\u 50”的自定义字段:
add_action( \'woocommerce_variation_options_pricing\', \'bbloomer_add_custom_field_to_variations\', 10, 3 );
function bbloomer_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
\'id\' => \'price_1_50[\' . $loop . \']\',
\'class\' => \'short\',
\'label\' => __( \'Price 1-50:\', \'woocommerce\' ),
\'value\' => get_post_meta( $variation->ID, \'price_1_50\', true )
));
}
add_action( \'woocommerce_save_product_variation\', \'bbloomer_save_custom_field_variations\', 10, 2 );
function bbloomer_save_custom_field_variations( $variation_id, $i ) {
$price_1_50 = $_POST[\'price_1_50\'][$i];
if ( isset( $price_1_50 ) ) update_post_meta( $variation_id, \'price_1_50\', esc_attr( $price_1_50 ) );
}
add_filter( \'woocommerce_available_variation\', \'bbloomer_add_custom_field_variation_data\' );
function bbloomer_add_custom_field_variation_data( $variations ) {
$variations[\'price_1_50\'] = \'<div class="woocommerce_custom_field">Price 1-50:
<span>\'get_post_meta( $variations[ \'variation_id\' ], \'price_1_50\', true ) . \'</span></div>\';
return $variations;
}
在函数中。php我编写了一个函数,用自定义字段“price\\u 1\\u 50”替换产品价格,但我想我无法获取变体ID以在前端显示自定义字段。
我试过了
var price = <?php echo get_post_meta( $variation_id, \'price_1_50\', true ); ?>;
var price = <?php echo get_post_meta( get_the_ID(), \'price_1_50\', true ); ?>;
$arrvariations = $variation->get_children (); $price_1_50 = get_post_meta( $arrvariations, \'price_1_50\', true ); echo $price_1_50;
在单产品页面的前端获得结果的唯一方法是,如果我写入变体ID。
var price = <?php echo get_post_meta( 10652, \'price_1_50\', true ); ?>;
最合适的回答,由SO网友:Billy 整理而成
好的,我已经设法解决了这个问题。这就是我最终创建变体自定义字段的方式:
add_action( \'woocommerce_product_after_variable_attributes\', \'variation_settings_fields\', 10, 3 );
add_action( \'woocommerce_save_product_variation\', \'save_variation_settings_fields\', 10, 2 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
\'id\' => \'price_1_50[\' . $variation->ID . \']\',
\'class\' => \'price_1_50\',
\'type\' => \'text\',
\'label\' => __( \'Price | Product amount: 1-49\', \'woocommerce\' ),
\'placeholder\' => \'\',
\'desc_tip\' => \'true\',
\'description\' => __( \'Enter price for product amount 1-49 (if available)\', \'woocommerce\' ),
\'value\' => get_post_meta( $variation->ID, \'price_1_50\', true )
)
);
}
function save_variation_settings_fields( $post_id ) {
$text_field = $_POST[\'_price_1_50\'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, \'price_1_50\', esc_attr( $text_field ) );
}
}
这就是我如何在前端获得所需结果的原因(基于:
https://stackoverflow.com/questions/47646939/how-to-get-woocommerce-variation-id)
$args = array(
\'post_type\' => \'product_variation\',
\'post_status\' => array( \'private\', \'publish\' ),
\'numberposts\' => -1,
\'orderby\' => \'menu_order\',
\'order\' => \'asc\',
\'post_parent\' => get_the_ID() // get parent post-ID
);
$variations = get_posts( $args );
foreach ( $variations as $variation ) {
$variation_ID = $variation->ID;
$product_variation = new WC_Product_Variation( $variation_ID );
get_post_meta( $variation_ID , \'price_1_50\', true );
}