Created
July 4, 2019 07:04
-
-
Save ibndawood/ec7bfaa61dd5f98ef4398e9c46f7deec to your computer and use it in GitHub Desktop.
Electro v2 - Do not show savings if the product is not on sale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function electro_onsale_product( $args = array() ) { | |
if ( is_woocommerce_activated() ) { | |
$defaults = array( | |
'section_title' => wp_kses_post( __( '<span class="highlight">Special</span> Offer', 'electro' ) ), | |
'section_class' => '', | |
'show_savings' => true, | |
'savings_in' => 'amount', | |
'savings_text' => esc_html__( 'Save', 'electro' ), | |
); | |
if ( isset( $args['product_choice'] ) ) { | |
switch( $args['product_choice'] ) { | |
case 'random': | |
$args['orderby'] = 'rand'; | |
break; | |
case 'recent': | |
$args['orderby'] = 'date'; | |
$args['order'] = 'DESC'; | |
break; | |
case 'specific': | |
$args['orderby'] = 'post__in'; | |
$args['ids'] = $args['product_id']; | |
$args['post__in'] = array_map( 'trim', explode( ',', $args['product_id'] ) ); | |
break; | |
} | |
} | |
$args = wp_parse_args( array( 'per_page' => 1 ), $args ); | |
$args = wp_parse_args( $args, $defaults ); | |
$products = Electro_Products::sale_products( $args ); | |
extract( $args ); | |
if ( $products->have_posts() ) { | |
while ( $products->have_posts() ) : $products->the_post(); | |
?> | |
<section class="section-onsale-product <?php echo esc_attr( $section_class ); ?>"> | |
<?php if ( ! empty ( $section_title ) || $show_savings ) : ?> | |
<header> | |
<?php if ( ! empty ( $section_title ) ) : ?> | |
<h2 class="h1"><?php echo wp_kses_post( $section_title ); ?></h2> | |
<?php endif ; ?> | |
<?php global $product; if ( $product->is_on_sale() && $show_savings ) : ?> | |
<div class="savings"> | |
<span class="savings-text"> | |
<?php | |
echo sprintf( '%s %s', $savings_text, Electro_WC_Helper::get_savings_on_sale( $product, $savings_in ) ); | |
?> | |
</span> | |
</div> | |
<?php endif; ?> | |
</header> | |
<?php endif; ?> | |
<div class="onsale-products"> | |
<?php wc_get_template_part( 'templates/contents/content', 'onsale-product' ); ?> | |
</div> | |
</section> | |
<?php | |
endwhile; | |
woocommerce_reset_loop(); | |
wp_reset_postdata(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This functions doesn't work. (