Обновление 2
Функция get_product()
устарела и заменена на wc_get_product()
, и в вашем коде много ошибок и ошибок, начиная с Woocommerce 3. Вместо этого попробуйте следующее:
<?php
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) {
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = wc_get_product( get_the_id() );
$price = $product->get_price_html();
$sales_price_from = $product->get_date_on_sale_from();
$sales_price_to = $product->get_date_on_sale_to();
if( ! empty($sales_price_from) || ! empty($sales_price_to) ){
$sales_price_date_to = $sales_price_from->date( "j.m.Y");
$sales_price_date_from = $sales_price_to->date( "j.m.Y");
$sales_date = '<p class="offer_date">Angebot vom '.$sales_price_date_from.' bis '.$sales_price_date_to.'</p>';
} else {
$sales_date = $sales_price_from = $sales_price_to = '';
}
?>
<div class="wochenangebot">
<h2><?php _e("Wochenangebot"); ?></h2>
<div class="wochenangebot_content">
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
<?php echo $sales_date; ?>
<?php var_dump($sales_price_to) ?>
<?php the_content(); ?>
</a>
<span class="price"><?php echo $price; ?></span>
<div class="legal-price-info">
<p class="wc-gzd-additional-info">
<span class="wc-gzd-additional-info tax-info variation_modified" style="display: inline;">inkl. MwSt.</span>
<span class="wc-gzd-additional-info shipping-costs-info variation_modified" style="display: inline;">zzgl. <a href="https://nussundgenuss.de/unser-service/" target="_blank">Versandkosten</a></span>
</p>
</div>
<a class="button" href="<?php the_permalink(); ?>">Hol es dir</a>
</div>
<aside>
<?php
if ( has_post_thumbnail( get_the_id() ) )
echo get_the_post_thumbnail( get_the_id(), 'shop_catalog' );
else
echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" >';
?>
</aside>
</div>
<?php endwhile;
}
?>
Протестировано и работает с Woocommerce 3 +
Связанный код в вашем вопросе также устарел и должен иметь вид:
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ) {
if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {
$sales_price_from = $product->get_date_on_sale_from();
$sales_price_to = $product->get_date_on_sale_to();
if( ! empty($sales_price_from) || ! empty($sales_price_to) ){
$sales_price_date_to = $sales_price_from->date( "j.m.Y");
$sales_price_date_from = $sales_price_to->date( "j.m.Y");
$sales_date = '<p class="offer_date">Angebot vom '.$sales_price_date_from.' bis '.$sales_price_date_to.'</p>';
} else {
$sales_date = $sales_price_from = $sales_price_to = '';
}
$price = str_replace( '</ins>', ' </ins> <b>(Offer from ' . $sales_price_date_from . ' till ' . $sales_price_date_to . ')</b>', $price );
}
return $price;
}
Код входит вФайл function.php вашей активной дочерней темы (или активной темы).