• Resolved cichy

    (@cichy)


    Hi there,

    I would like to swap regular price and sale price positions so that the regular price comes first.

    I see that this function needs to be overwritten.

    function get_price_html_from_to( $from, $to ) {
    		return '<del>' . ((is_numeric($from)) ? woocommerce_price( $from ) : $from) . '</del> <ins>' . ((is_numeric($to)) ? woocommerce_price( $to ) : $to) . '</ins>';
    	}

    How could I safely do it in functions.php of my child theme so that I do not to have to edit it in woocommerce files.

    Thanks in advance for your help.

    regards,
    Cichy

    http://wordpress.org/extend/plugins/woocommerce/

Viewing 3 replies - 1 through 3 (of 3 total)
  • that’s a bit difficult, since it gets returned all in one string, but you’d do something like this:

    function extract_unit($string, $start, $end)
    {
    $pos = stripos($string, $start);
    $str = substr($string, $pos);
    $str_two = substr($str, strlen($start));
    $second_pos = stripos($str_two, $end);
    $str_three = substr($str_two, 0, $second_pos);
    $unit = trim($str_three); // remove whitespaces
    return $unit;
    }
    add_action('wp_content','my_woo_filters');
     function my_woo_filters(){
    add_filter('woocommerce_sale_price_html', 'new_display_price');
    )
    function new_display_price($price){
    $saleprice = extract_unit($price,'<ins>','</ins>');
    $regprice = extract_unit($price,'<del>','</del>');
    return '<ins>' . $saleprice . '</ins><del>' . $regprice . '</del>';
    }

    This is how it SHOULD work, but the filter isn’t firing for some reason.

    Thread Starter cichy

    (@cichy)

    Hi bheadrick,

    Thank you for your time and for the hint. I will try to work on it though I am not literate in php. If I’m lucky to move it any further I will publish the results in this thread.

    However if anyone else is able to make it work or point me to the right direction I would appreciate that.

    regards,
    cichy

    Thread Starter cichy

    (@cichy)

    Hi bheadrick,

    I appreciate your help. At first I did not know how to implement it at all. But then I gave up on using the filter and implemented the way below in the price.php file in my child theme:

    global $post, $product;
    
    $price = $product->get_price_html();
    
    function extract_unit($string, $start, $end) {
    	$pos = stripos($string, $start);
    	$str = substr($string, $pos);
    	$str_two = substr($str, strlen($start));
    	$second_pos = stripos($str_two, $end);
    	$str_three = substr($str_two, 0, $second_pos);
    	$unit = trim($str_three); // remove whitespaces
    	return $unit;
    }
    
    $saleprice = extract_unit($price,'<ins>','</ins>');
    $regprice = extract_unit($price,'<del>','</del>');
    
    ?>
    <div class="right">
    <div itemprop="offers" itemscope itemtype="http://schema.org/Offer" class="pr_price">
    
    	<p itemprop="price" class="price"><?php echo '<ins>' . $saleprice . '</ins> <del>' . $regprice . '</del>'; ?></p>
    
    	<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
    
    </div>

    And it does the job.

    Thanks again,
    cichy

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to swap regular price and sale price positions?’ is closed to new replies.