簡體   English   中英

在WooCommerce中用自定義字段值替換產品變化價格

[英]Replace product variations prices with custom field value in WooCommerce

我想用自定義字段(_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 );

您應該嘗試這樣:

// 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文件中,也可能會出現在任何插件文件中。

經過測試和工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM