I'm using a woocommerce product addons plugin where I would like to display the price of a product in my dropdown section of the addons. Currently the code I have is this
<?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'm using this echo
$product->get_price_html()
what this does though is display the $"sale price" $"price" but I just want to display just the sale price or just the product price if there is no sale price. Looking at the code below, how would I accomplish this?
Pretty simple. We'll write a custom function that will first be sure on that if the product is on sale or not. Then it'll return regular or sale price based on the sale condition it defined previously. So the function will be-
Now call this function
the_dramatist_price_show()
instead of$product->get_price_html()
. You'll get the price based on is on sale or not without currency symbol.Hope that helps.
This article worked for me. you can also try this one.
If you want to get the regular price or sale price of a woocommerce product and you are getting nothing you need to know the following:
If the product has no variations you can get the regular price and sales price just like that:
Get the price of a simple procut
If the product has variations you will get nothing if you will use the code above.
You need to get the variation product prices.
Get the price of a product with variations
That should be all.
Enjoy.
Source: http://www.w3bdeveloper.com/how-to/how-to-get-regular-price-of-a-product-in-wordpress-woocommerce/
That's what works for me with Wordpress 5.1 and WooCommerce 3.5.5 :