简体   繁体   English

显示 Woocommerce 变量产品中后缀的选定变体价格

[英]Display selected variation price suffixed in Woocommerce variable products

I am using this function so I get the default variation price but I want to show my price suffix after the price:我正在使用这个函数,所以我得到了默认的变化价格,但我想在价格后显示我的价格后缀:

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
    foreach($product->get_available_variations() as $pav){
        $def=true;
        foreach($product->get_variation_default_attributes() as $defkey=>$defval){
            if($pav['attributes']['attribute_'.$defkey]!=$defval){
                $def=false;             
            }   
        }
        if($def){
            $price = $pav['display_price'];         
        }
    }   
    return woocommerce_price($price);
}

What I am getting out now is for example '€15,00' but what I want it to show is '€15,00 per kilo' and 'per kilo' is the price suffix例如,我现在得到的是“15,00 欧元”,但我希望它显示的是“每公斤 15,00 欧元”,“每公斤”是价格后缀

Your code is outdated since Woocommerce 3 ( get_variation_default_attributes() and woocommerce_price() are deprecated) .您的代码已过时,因为 Woocommerce 3 get_variation_default_attributes()woocommerce_price()已弃用)

The will display the selected variation price suffixed for Woocommerce 3:将显示 Woocommerce 3 后缀的所选变体价格:

add_filter('woocommerce_available_variation', 'display_variation_price_suffixed', 10, 3 );
function display_variation_price_suffixed( $variation_data, $product, $variation ) {
    $variation_data['price_html'] .= ' <span class="price-suffix">' . __("per kilo", "woocommerce") . '</span>';

    return $variation_data;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

If needed you can use $variation_data['attributes'] array to target specific product attributes term values in a foreach loop.如果需要,您可以使用$variation_data['attributes']数组在 foreach 循环中定位特定的产品属性术语值。

For information, the filter hook woocommerce_available_variation is located inside WC_Product_Variable get_available_variation() method.有关信息,过滤器钩子woocommerce_available_variation位于WC_Product_Variable get_available_variation()方法内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce Variable 产品的 jQuery 中获取选定的变化价格 - Get selected variation price in jQuery on Woocommerce Variable products 显示Woocommerce商店页面中变量产品的默认变化价格 - Display default variation price for variable products in Woocommerce shop pages 使用简码显示选定的 WooCommerce 变体格式价格 - Display the selected WooCommerce variation formatted price with shortcode 在 WooCommerce 中选择的变体销售价格后显示折扣百分比 - Display discount percentage after the selected variation sale price in WooCommerce 在 WooCommerce 变化价格上显示特定的选定产品属性 - Display specific the selected product attribute on the WooCommerce variation price Woocommerce显示可变产品的不含税价格 - Woocommerce display price without tax on variable products 隐藏产品可变价格,直到在 WooCommerce 中选择所有变体字段 - Hide product variable price until all variation fields are selected in WooCommerce 在WooCommerce可变产品单页中获取选定的产品变体ID - Get the selected product variation ID in WooCommerce variable products single pages 显示 WooCommerce 可变产品的销售变化节省金额 - Display on sale variation saving amount for WooCommerce variable products 在Woocommerce变量产品的变体描述之前自定义显示 - Custom display before variation description on Woocommerce variable products
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM