Woocommerce Show default variation price

非 Y 不嫁゛ 提交于 2019-12-05 18:30:48

I did not find answer for this question (how to display price from default product variation instead of price range?) so i created the code:

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);

    }

This plugin show price from default variation, of course if you set default values before. Tested on woocommerce version 2.6.2.

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!