在WooCommerce中使用自定义字段值替换产品变体价格

问题描述 投票:1回答:1

我想用我的自定义字段(_wholesale_price)替换价格,但无法弄清楚如何获取所选变体的ID

function new_variable_price_format( $price, $product ) {

    echo '<pre>';
    var_dump($product);
    echo '</pre>';


    // return get_post_meta( $ID, '_wholesale_price', true);

}

add_filter( 'woocommerce_variable_sale_price_html', 'new_variable_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'new_variable_price_format', 10, 2 );
php wordpress woocommerce price variations
1个回答
2
投票

你应该试试这个:

// Min and max variable prices
add_filter( 'woocommerce_variable_sale_price_html', 'new_variable_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'new_variable_price_format', 10, 2 );
function new_variable_price_format( $formated_price, $product ) {

    $price = get_post_meta( $product->get_id(), '_wholesale_price', true);
    return wc_price($price);
}

// Selected variation prices
add_filter('woocommerce_product_variation_get_sale_price', 'custom_product_get_price', 10, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_product_get_price', 10, 2 );
function custom_product_get_price( $price, $product ){
    return get_post_meta( $product->get_id(), '_wholesale_price', true);
}

此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中。

经过测试和工作

© www.soinside.com 2019 - 2024. All rights reserved.