在Woocommerce中用自定义字段替换变动价格范围

时间:2020-03-27 07:39:10

标签: php wordpress woocommerce

我正在使用以下功能将自定义字段添加并保存到我的变量产品中:

add_action( 'woocommerce_product_options_inventory_product_data', 'wc_add_custom_fields' );

function wc_add_custom_fields() {

    woocommerce_wp_text_input(
         array(
             'id'                => '_custom_product_pricekg_field',
             'label'             => __( 'Price per kg', 'woocommerce' ),
             'placeholder'       => '',
            'desc_tip'            => false,
             'description'       => __( "Here's some really helpful text that appears next to the field.", 'woocommerce' ),
             'type'              => 'number',
             'custom_attributes' => array(
                     'step'     => 'any',
                     'min'    => '0'
                 )
         )
     );    

}

add_action( 'woocommerce_process_product_meta', 'wc_custom_fields_save' );

function wc_custom_fields_save($post_id)
{

    $woocommerce_custom_product_pricekg_field = $_POST['_custom_product_pricekg_field'];
    if (!empty($woocommerce_custom_product_pricekg_field))
        update_post_meta($post_id, '_custom_product_pricekg_field', esc_attr( $_POST['_custom_product_pricekg_field'] ) );
}

我想用此自定义字段替换所有档案和产品页面上的可变价格范围($ XX-XX)。我不知道该怎么做。下面的代码片段将可变价格范围替换为默认变量,也许可以对其进行修改以显示我的自定义字段值?

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);

function custom_variation_price( $price, $product ) {

    foreach($product->get_available_variations() as $pav){
        $def=true;
        foreach($product->get_variation_default_attributes() as $defkey=>$defval){
            if($pav['attributes']['attribute_'.$defkey]!=$defval){
                $def=false;             
            }   
        }
        if($def){
            $price = $pav['display_price'];         
        }
    }   
    return woocommerce_price($price);
}

1 个答案:

答案 0 :(得分:1)

以下代码将价格范围替换为自定义字段中的值,如果您还有其他问题,欢迎您。

// Add new field
function wc_add_custom_fields() {
    woocommerce_wp_text_input( 
        array(
            'id'                => '_custom_product_pricekg_field',
            'label'             => __( 'Price per kg', 'woocommerce' ),
            'placeholder'       => '',
            'desc_tip'            => false,
            'description'       => __( "Here's some really helpful text that appears next to the field.", 'woocommerce' ),
            'type'              => 'number',
            'custom_attributes' => array(
                'step'     => 'any',
                'min'    => '0'
            )
        )
     );
}
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_add_custom_fields', 10, 0 );

// Save
function wc_custom_fields_save($post_id) {
    $woocommerce_custom_product_pricekg_field = $_POST['_custom_product_pricekg_field'];

    if ( !empty( $woocommerce_custom_product_pricekg_field ) ) {
        update_post_meta($post_id, '_custom_product_pricekg_field', esc_attr( $_POST['_custom_product_pricekg_field'] ) );
    }
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_fields_save', 10, 1 );

// Variation price
function custom_variation_price( $price, $product ) {
    // Get product id
    $product_id = $product->get_id();

    // Get custom value
    $new_price = get_post_meta( $product_id, '_custom_product_pricekg_field', true);

    // NOT empty
    if ( ! empty( $new_price ) ) {
        $price = $new_price;
    }

    return $price;
}
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
相关问题