gpt4 book ai didi

woocommerce - 如何在 woocommerce 中显示销售日期

转载 作者:行者123 更新时间:2023-12-02 21:53:07 29 4
gpt4 key购买 nike

在 Woocommerce 中,我尝试显示可变产品的销售日期。

我找到了a code ,如果我将此代码准确地发布在functions.php中,它不会执行任何操作。我尝试修改它,因为我只需要在起始页上使用它,但是 var_dump 返回一个空字符串。也许我必须添加一个参数?

<?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 = get_product( $featured_query->post->ID );
$price = $product->get_price_html();
$sales_price_from = get_post_meta( $product->id, '_sale_price_dates_from', true );
$sales_price_to = get_post_meta( $product->id, '_sale_price_dates_to', true );
$sales_price_date_from = date( "j.m.Y", $sales_price_from );
$sales_price_date_to = date( "j.m.Y", $sales_price_to );
$sales_date = '<p class="offer_date">Angebot vom '.$sales_price_date_from.' bis '.$sales_price_date_to.'</p>';
?>

<div class="wochenangebot">
<h2>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( $loop->post->ID ) )
echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' );
else
echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" >';
?>
</aside>
</div>

<?php endwhile;
}
?>

非常感谢任何帮助。

最佳答案

更新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 文件中。

关于woocommerce - 如何在 woocommerce 中显示销售日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52774900/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com