【问题标题】:Replace product variations prices with custom field value in WooCommerce在 WooCommerce 中用自定义字段值替换产品变体价格
【发布时间】:2017-12-20 12:49:03
【问题描述】:

我想用我的自定义字段 (_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】:

    你应该试试这个:

    // 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 文件中或任何插件文件中。

    经过测试和工作

    【讨论】:

    • 谢谢你的sn-p :)
    猜你喜欢
    • 2019-05-09
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    相关资源
    最近更新 更多