在 WooCommerce 中自定义选定的产品变体价格

Customize the selected product variation prices in WooCommerce

本质上,当用户做出选择时,我试图弄清楚在哪里计算变化数据并将其显示到 html 中。这是 my shop link.

我所指的具体价格:

基本上我想知道这个数据是从哪里插入的,我在哪里可以修改它。

模板中的一些JS导入数据single-product/add-to-cart/variation.php:

<div class="woocommerce-variation-price">
    {{{ data.variation.price_html }}}
</div>

我的最终目标是简单地修改销售和价格的显示方式,也许可以在它们前面加上 Price:[=13 这样的前缀=].

WP 版本:4.9.1
WC 版本:3.2.6
主题:Flatsome3 v3.3.9

使用以下钩子函数,您将无法在所有产品类型的产品打折时为每个价格添加前缀:

// Prefix the selected variation prices When discounted for variable products (on sale)
add_filter( 'woocommerce_format_sale_price', 'prefix_variations_selected_prices', 10, 3 );
function prefix_variations_selected_prices( $price, $regular_price, $sale_price ) {
    global $product;

    // Just for variable products on single product pages
    if( $product->is_type('variable') && is_product() ) {

        // Custom texts
        $price_txt =  __( 'Price', 'woocommerce' ) . ': ';
        $sale_txt = __(' Sale', 'woocommerce' ).': ';

        return '<del>' . $price_txt . wc_price( $regular_price ) . '</del> <ins>' . $sale_txt . wc_price( $sale_price ) . '</ins>';
    }
    return $price;
}

代码进入活动子主题(或活动主题)的 function.php 文件。

已测试并有效。

你会得到类似的东西:

基于这个类似的答案: