How to show selling price or regular woocommerce price if there is no selling price

I use the addo woocommerce product plugin where I would like to display the price of a product in my drop-down add-ons section. Currently the code that I have is

    <?php
$loop = 0;
$current_value = isset( $_POST['addon-' . sanitize_title( $addon['field-name'] ) ] ) ? wc_clean( $_POST[ 'addon-' . sanitize_title( $addon['field-name'] ) ] ) : '';
global $product;
?>
<p class="form-row form-row-wide addon-wrap-<?php echo sanitize_title( $addon['field-name'] ); ?>">
    <select class="addon addon-select" name="addon-<?php echo sanitize_title( $addon['field-name'] ); ?>">

        <?php if ( ! isset( $addon['required'] ) ) : ?>
            <option value=""><?php _e('None', 'woocommerce-product-addons'); ?></option>
        <?php else : ?>
            <!--<option value=""><?php _e('Select an option...', 'woocommerce-product-addons'); ?></option>-->
        <?php endif; ?>

        <?php foreach ( $addon['options'] as $i => $option ) :
            $loop ++;
            $price = apply_filters( 'woocommerce_product_addons_option_price',
                $option['price'] > 0 ? ' + ' . wc_price( get_product_addon_price_for_display( $option['price'] ) ) . '' : '',
                $option,
                $i,
                'select'
            );
            ?>
            <option data-raw-price="<?php echo esc_attr( $option['price'] ); ?>" data-price="<?php echo get_product_addon_price_for_display( $option['price'] ); ?>" value="<?php echo sanitize_title( $option['label'] ) . '-' . $loop; ?>" <?php selected( $current_value, sanitize_title( $option['label'] ) . '-' . $loop ); ?>><?php echo wptexturize( $option['label'] . ' (' ); echo balanceTags($product->get_price_html()) . $price ?>)</option>
        <?php endforeach; ?>

    </select>
</p>

I use this echo

$product->get_price_html()

what it does, although it shows the price of $ "sale price" $ "price", but I just want to display only the sale price or only the price of the product if there is no sale price. Looking at the code below, how would I do this?

+4
source share
2 answers

. , , . , , . , -

/**
 * Returns product price based on sales.
 * 
 * @return string
 */
function the_dramatist_price_show() {
    global $product;
    if( $product->is_on_sale() ) {
        return $product->get_sale_price();
    }
    return $product->get_regular_price();
}

the_dramatist_price_show() $product->get_price_html(). , , .

, .

+13

. .

woocommerce, , :

, :

<?php
#the product must be instantiated above like $product = new WC_Product();
echo $product->regular_price;
echo $product->sale_price;
?>

, , .

.

#1 Get product variations
$product_variations = $product->get_available_variations();

#2 Get one variation id of a product
$variation_product_id = $product_variations [0]['variation_id'];

#3 Create the product object
$variation_product = new WC_Product_Variation( $variation_product_id );

#4 Use the variation product object to get the variation prices
echo $variation_product ->regular_price;
echo $variation_product ->sale_price;

.

Enjoy.

: http://www.w3bdeveloper.com/how-to/how-to-get-regular-price-of-a-product-in-wordpress-woocommerce/

0

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


All Articles