【问题标题】:Customize the selected product variation prices in WooCommerce在 WooCommerce 中自定义选定的产品变体价格
【发布时间】:2018-01-11 20:25:14
【问题描述】:

基本上我想弄清楚当用户做出选择时,变异数据是在哪里计算并显示到 html 中的。这是my shop link

我指的具体价格:

基本上我想知道这些数据插入到哪里以及我可以在哪里修改它。

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

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

我的最终目标是简单地修改销售和价格的显示方式,可以在它们前面加上 Price:Sale: 之类的前缀。

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

【问题讨论】:

    标签: php wordpress woocommerce price variations


    【解决方案1】:

    使用以下钩子函数,您将能够在所有产品类型的产品销售时为每个价格添加前缀:

    // 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 文件中。

    经过测试并且有效。

    你会得到类似的东西:

    基于这个类似的答案:
    Display discount percentage after the selected variation sale price in WooCommerce

    【讨论】:

    • @gregbast1994 很高兴它符合您的需要:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2019-08-28
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    相关资源
    最近更新 更多