在Woocommerce中,我试图显示可变产品的销售日期。
我发现了a code,如果我将这段代码恰好发布在functions.php中,它什么也没做。我尝试对其进行修改,因为仅在开始页面上需要它,但是filtered 1,Date,desc,type,2018
filtered 2,Date,desc,type,2018
filtered 3,Date,desc,type,2018
filtered 4,Date,desc,type,2018
filtered 5,Date,desc,type,2018
返回一个空字符串。也许我必须添加一个论点?
var_dump
我们非常感谢您的帮助。
答案 0 :(得分:0)
更新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文件中。