WC_Product::is_on_sale PHP Method

is_on_sale() public method

Returns whether or not the product is on sale.
public is_on_sale ( ) : boolean
return boolean
    public function is_on_sale()
    {
        if ('' !== (string) $this->get_sale_price() && $this->get_regular_price() > $this->get_sale_price()) {
            $onsale = true;
            if ('' !== (string) $this->get_date_on_sale_from() && $this->get_date_on_sale_from() > strtotime('NOW', current_time('timestamp'))) {
                $onsale = false;
            }
            if ('' !== (string) $this->get_date_on_sale_to() && $this->get_date_on_sale_to() < strtotime('NOW', current_time('timestamp'))) {
                $onsale = false;
            }
        } else {
            $onsale = false;
        }
        return apply_filters('woocommerce_product_is_on_sale', $onsale, $this);
    }

Usage Example

/**
 * Gte woocommerce data for product
 *
 * @param $value
 * @param $data
 *
 * @return string
 */
function vc_gitem_template_attribute_woocommerce_product($value, $data)
{
    $label = '';
    /**
     * @var null|Wp_Post $post ;
     * @var string $data ;
     */
    extract(array_merge(array('post' => null, 'data' => ''), $data));
    require_once WC()->plugin_path() . '/includes/abstracts/abstract-wc-product.php';
    $product = new WC_Product($post);
    if (preg_match('/_labeled$/', $data)) {
        $data = preg_replace('/_labeled$/', '', $data);
        $label = apply_filters('vc_gitem_template_attribute_woocommerce_product_' . $data . '_label', Vc_Vendor_Woocommerce::getProductFieldLabel($data) . ': ');
    }
    $price_format = get_woocommerce_price_format();
    switch ($data) {
        case 'id':
            $value = (int) $product->is_type('variation') ? $product->get_variation_id() : $product->id;
            break;
        case 'sku':
            $value = $product->get_sku();
            break;
        case 'price':
            $value = sprintf($price_format, wc_format_decimal($product->get_price(), 2), get_woocommerce_currency());
            break;
        case 'regular_price':
            $value = sprintf($price_format, wc_format_decimal($product->get_regular_price(), 2), get_woocommerce_currency());
            break;
        case 'sale_price':
            $value = sprintf(get_woocommerce_price_format(), $product->get_sale_price() ? wc_format_decimal($product->get_sale_price(), 2) : '', get_woocommerce_currency());
            break;
        case 'price_html':
            $value = $product->get_price_html();
            break;
        case 'reviews_count':
            $value = count(get_comments(array('post_id' => $post->ID, 'approve' => 'approve')));
            break;
        case 'short_description':
            $value = apply_filters('woocommerce_short_description', $product->get_post_data()->post_excerpt);
            break;
        case 'dimensions':
            $units = get_option('woocommerce_dimension_unit');
            $value = $product->length . $units . 'x' . $product->width . $units . 'x' . $product->height . $units;
            break;
        case 'raiting_count':
            $value = $product->get_rating_count();
            break;
        case 'weight':
            $value = $product->get_weight() ? wc_format_decimal($product->get_weight(), 2) : '';
            break;
        case 'on_sale':
            $value = $product->is_on_sale() ? 'yes' : 'no';
            // @todo change
            break;
        default:
            $value = $product->{$data};
    }
    return strlen($value) > 0 ? $label . apply_filters('vc_gitem_template_attribute_woocommerce_product_' . $data . '_value', $value) : '';
}
All Usage Examples Of WC_Product::is_on_sale
WC_Product