Woocommerce 价格循环显示在错误的项目上 [英] Woocommerce price in loop displays on wrong item
问题描述
遵循此处的指南
在使用 echo $product->regular_price;
之前尝试声明 global $product
所以:
Following the guide here http://www.w3bdeveloper.com/how-to/how-to-get-regular-price-of-a-product-in-wordpress-woocommerce/
Using the following code to call the price inside the loop, the price is displaying the wrong price per item.
The first item has no price, the second item has the first items price, the third item has the second price and so on.
<div class='price'>
<?php echo $product->regular_price; ?>
</div>
If I use <?php echo $product->get_price_html(); ?>
it displays correctly, but I'd like to be able to display the sale price and the regular price separately. Also for some reason, if I use this code before the button code, I get a fatal error.
The loop code is as follows:
<div class='post'>
<a class='oxy-post-image' href='<?php the_permalink(); ?>'>
<div class='oxy-post-image-fixed-ratio' style='background-image: url(<?php echo get_the_post_thumbnail_url(); ?>);'></div>
<div class='price-overlay'>
<?php echo $product->regular_price; ?>
</div>
</a>
<div class='post-wrapper'>
<a class='oxy-post-title' href='<?php the_permalink(); ?>'><?php the_title(); ?></a>
<div class='oxy-post-meta'>
<div class='cart-button'>
<?php global $product; echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s">%s</a>',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
$product->is_purchasable() ? 'add_to_cart_button' : '',
esc_attr( $product->get_type() ),
esc_html( $product->add_to_cart_text() ) ),$product ); ?>
</div>
</div>
</div>
</div>
A screenshot of the items in the loop:
Try to declare global $product
before using echo $product->regular_price;
so :
<div class='price-overlay'>
<?php global $product; echo $product->regular_price; ?>
</div>
这篇关于Woocommerce 价格循环显示在错误的项目上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!