Woocommerce显示默认变化价格 [英] Woocommerce Show default variation price

查看:211
本文介绍了Woocommerce显示默认变化价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Woocommerce和产品变体,所有变体都定义了默认变体.我想知道如何找到默认变体并显示其价格.

I'm using Woocommerce and product variations and all my variations have a default variation defined. I would like to know, how I can find the default variation and display it's price.

这是我到目前为止获得的代码,但是它显示了最便宜的变动价格,而我正在寻找默认的变动产品价格.

This is the code I got so far, but it displays the cheapest variation price, and I'm looking for my default variation product price instead.

// Use WC 2.0 variable price format, now include sale price strikeout
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'HERE YOUR LANGUAGE: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'HERE YOUR LANGUAGE: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

if ( $price !== $saleprice ) {
    $price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;

}

我在 Woocommerce变化产品价格以显示默认代码的地方找到了代码示例

推荐答案

这是我自己的解决方案,认为可能会更好,但是我不是php开发人员,请随时添加改进.

This is my own solution, think it could be better, but I'm not a php developer, so feel free to add improvements.

function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$available_variations = $product->get_available_variations();
$selectedPrice = '';

foreach ( $available_variations as $variation )
{
    $tmp = implode($variation['attributes']);

    if (strpos($tmp,'standard') !== false) {
        $selectedproduct = wc_get_product($variation['variation_id']);
        $price =  $selectedproduct->get_price();
        $selectedPrice = wc_price($price);
    }       
}

return $selectedPrice;
}

这篇关于Woocommerce显示默认变化价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆