显示变化自定义字段值

时间:2019-12-11 14:35:22

标签: php wordpress woocommerce

我正在使用以下功能向每个产品版本添加一个名为“ price_1_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;
}

在functions.php中,我编写了一个用自定义字段“ price_1_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 ); ?>;

2 个答案:

答案 0 :(得分:0)

您在functions.php中使用哪个挂钩替换产品价格?

答案 1 :(得分:0)

好,我设法解决了这个问题。这就是我最终创建变体自定义字段的方式:

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

这就是我在前端获得预期结果的方式(基于: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 );

}