How to get woocommerce price attributes

I am trying to get the following information on a single woocommerce product page to replace the default pricing style for woocommerce.

When the product is sold, I want to be able to get the initial price and the discounted price and repeat it inside the dive for further styling. I am trying to achieve something like this.

<div id="orig-price">Original Price: <?php echo $price; ?></div>
<div id="sale-price">Sale Price: <?php echo $sale-price; ?></div>
<div id="saved">You saved: <?php $saved=$price-$saleprice; echo $saved; ?></div>

I tried opening price.php but did not find what I was looking for. that's what i get.

<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">

    <p class="price"><?php echo $product->get_price_html(); ?></p>

    <meta itemprop="price" content="<?php echo $product->get_price(); ?>" />
    <meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
    <link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
    <div><?php echo $sale_price_in_percent; ?></div>

</div>

Am I in the right place? Does anyone know how I can access these attributes?

thank

+4
source share
2 answers

- get_price_html() /woocommerce/includes/abstracts/abstract-wc-product.php, , html :

  • woocommerce_sale_price_html
  • woocommerce_price_html
  • woocommerce_empty_price_html
  • woocommerce_free_sale_price_html
  • woocommerce_free_price_html
  • woocommerce_get_price_html

, , html, woocommerce_get_price_html. HTML HTML.

add_filter( 'woocommerce_get_price_html', function( $price, $product ) 
{ 
    global $woocommerce_loop;

    // check if we are in single product page, in main section, and if product has price and is on sale
    if ( is_product() && !isset( $woocommerce_loop ) && $product->get_price() && $product->is_on_sale() ) {

        // collect prices from $price html string
        $prices = array_map( function( $item ) {        
            return array( $item, (float) preg_replace( "/[^0-9.]/", "", html_entity_decode( $item, ENT_QUOTES, 'UTF-8' ) ) );           
        }, explode( ' ', strip_tags( $price ) ) );

        $price = isset( $prices[0][0] ) ? '<span class="orig-price">Original Price: ' . $prices[0][0] . '</span>' : '';
        $price .= isset( $prices[1][0] ) ? '<span class="sale-price">Sale Price: ' . $prices[1][0] . '</span>' : '';

        if ( $product->get_regular_price() ) {
            // set saved amount with currency symbol placed as defined in options
            $price .= '<span class="saved">You saved: ' . sprintf( get_woocommerce_price_format(), get_woocommerce_currency_symbol(), $prices[0][1] - $prices[1][1] ) . '</span>';      
        }
    }

    return $price;

}, 10, 2 );

span divs, . price.php, /themes/yourtheme/woocommerce/single-product/.

+9

woocommerce. PHP, div .

WooCommerce

, .

+1

Source: https://habr.com/ru/post/1537901/


All Articles