2
votes

I'm trying to solve issue about displaying price in wordpress woocomerce. The preview site (loop) works fine also the single product page prices shows well. Only the variable price doesn't change with selection field. The main problem why I create filter for prices is displaying price with tax and without. My code look like :

add_filter('woocommerce_get_price_html', 'edit_price_display', 10, 2);

function edit_price_display() {
    global $product;
    $price = $product->get_price();
    $tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
    if (!empty($tax_rates)) {
        $tax_rate = reset($tax_rates);
    }

    if($product->is_type( 'simple' ) && is_numeric($price)) {
        $price_incl_tax = $price + round($price * ( $tax_rate['rate'] / 100 ), 2);  
        $price_incl_tax = wc_price($price_incl_tax);
        $price = wc_price($price);
        $display_price = '<span class="price">';
        $display_price .= '<span class="amount">' . $price_incl_tax .' s DPH </span>';
        $display_price .= '<br>';
        $display_price .= '<span class="amount">' . $price .' bez DPH </span>';
        $display_price .= '</span>';    
        return $display_price;  
    }

    if($product->is_type( 'variable' ) && is_numeric($price) && !is_single()) {

        $minmax = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );

        if($minmax[0] == $minmax[1]){
            $price_incl_tax = $minmax[0] + round($minmax[0] * ( $tax_rate['rate'] / 100 ), 2);  
            $price_incl_tax = wc_price($price_incl_tax);
            $price = wc_price($minmax[0]);
            $display_price = '<span class="price">';
            $display_price .= '<span class="amount">' . $price_incl_tax .' s DPH </span>';
            $display_price .= '<br>';
            $display_price .= '<span class="amount">' . $price .' bez DPH </span>';
            $display_price .= '</span>';    
        }

        if($minmax[0] != $minmax[1]){
            $price_incl_tax_min = $minmax[0] + round($minmax[0] * ( $tax_rate['rate'] / 100 ), 2);
            $price_incl_tax_min = wc_price($price_incl_tax_min);
            $price_incl_tax_max = $minmax[1] + round($minmax[1] * ( $tax_rate['rate'] / 100 ), 2);  
            $price_incl_tax_max = wc_price($price_incl_tax_max);
            $price_excl_tax_min = wc_price($minmax[0]);
            $price_excl_tax_max = wc_price($minmax[1]);
            $display_price = '<span class="price">';
            $display_price .= '<span class="amount">' .$price_incl_tax_min. ' - ' .$price_incl_tax_max. ' s DPH </span>';
            $display_price .= '<br>';
            $display_price .= '<span class="amount">' . $price_excl_tax_min .' - '.$price_excl_tax_max.' bez DPH </span>';
            $display_price .= '</span>';    
        }           
        return $display_price;      
    }

    if($product->is_type( 'variable' ) && is_numeric($price) && is_single()) {
        return $product->price; // Issue is right her !
    }

    if( $price == '' || $price == '-' ) {
        $display_price = '<span class="amount">Cena na dopyt</span>';
        return $display_price;
    }
}

I think the problem is in (multidimensional) array filling. Because the variation.php file handling the price show up for variable product. In the variation we can see something like :

<script type="text/template" id="tmpl-variation-template">
    <div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
    <div class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div>
    <div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
</script>

But I have no idea how that mentioned array looks like ?

1

1 Answers

0
votes

It is not the best solution to modify riginal loop/price.php file but it is quick. The final code looks like:

global $product;

if ( $price_html = $product->get_price_html() ) {

    $price = $product->get_price();

    $tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
    if (!empty($tax_rates)) {
        $tax_rate = reset($tax_rates);
    }

    if ( '' === $price || 0 == $price ) {
        $display_price = $price_html;
    }

    if($product->is_type( 'simple' ) && is_numeric($price)) {
        $price_incl_tax = round($price * (1 + ($tax_rate['rate'] / 100) ), 2);  
        $price_incl_tax = wc_price($price_incl_tax);
        $price = wc_price($price);
        $display_price = '<span class="price">';
        $display_price .= '<span class="amount">' . $price_incl_tax .' s DPH </span>';
        $display_price .= '<br>';
        $display_price .= '<span class="amount">' . $price .' bez DPH </span>';
        $display_price .= '</span>';    
    }

    if($product->is_type( 'variable' ) && is_numeric($price) && $price > 0) {

        $minmax = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );

        if($minmax[0] == $minmax[1]){
            $price_excl_tax = round($minmax[0] / (1 + ($tax_rate['rate'] / 100) ), 2);  
            $price_excl_tax = wc_price($price_excl_tax);
            $price = wc_price($minmax[0]);
            $display_price = '<span class="price">';
            $display_price .= '<span class="amount">' . $price .' s DPH </span>';
            $display_price .= '<br>';
            $display_price .= '<span class="amount">' . $price_excl_tax .' bez DPH </span>';
            $display_price .= '</span>';    
        }

        if($minmax[0] != $minmax[1]){
            $price_excl_tax_min = round($minmax[0] / (1 + ($tax_rate['rate'] / 100) ), 2); 
            $price_excl_tax_min = wc_price($price_excl_tax_min);
            $price_excl_tax_max = round($minmax[1] / (1 + ($tax_rate['rate'] / 100) ), 2); 
            $price_excl_tax_max = wc_price($price_excl_tax_max);

            $price_incl_tax_min = wc_price($minmax[0]);
            $price_incl_tax_max = wc_price($minmax[1]);

            $display_price = '<span class="price">';
            $display_price .= '<span class="amount">' .$price_incl_tax_min. ' - ' .$price_incl_tax_max. ' s DPH </span>';
            $display_price .= '<br>';
            $display_price .= '<span class="amount">' . $price_excl_tax_min .' - '.$price_excl_tax_max.' bez DPH </span>';
            $display_price .= '</span>';    
        }               
    }

echo $display_price;
}

And for single product page use shortcode {price_excluding_tax} as price suffix located in woocommerce tax.