I'm trying to modify related.php in order to randomly show any 3 products from the shop that has a price in the range of plus/minus 100.
I'm using ACF fields and Woocommerce 3.2. The problem is that although the products are selected correctly, their price is not displayed. Instead, the price of the reference product is displayed for all 3 products.
Here is the code (price_obj is the ACF field for price):
global $product, $woocommerce_loop;
$product = new WC_Product(get_the_ID());
$price_product = get_field('price_obj',get_the_ID());
$args1=array(
'post_type' => 'product',
'posts_per_page' => -1,
'post__not_in' => array( $product->get_id() )
);
$products_in_range = array();
$my_query = new wp_query($args1);
if( $my_query->have_posts() ) {
$val = count($my_query->get_posts());
while ($my_query->have_posts()) {
$my_query->the_post();
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
$price = get_field('price_obj');
$id = get_the_ID();
if ((($price_product-100) <= $price) && ($price <= ($price_product+100))){
array_push($products_in_range,$id);
}
}
}
wp_reset_query();
$rand_products = array_rand($products_in_range, 3);
?>
<?php if ($rand_products){ ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<ul class="products">
<?php
foreach ($rand_products as $prod){
$title = get_the_title($products_in_range[$prod]);
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($products_in_range[$prod]), 'large');
$link = get_permalink($products_in_range[$prod]);
$product_prod = new WC_Product($products_in_range[$prod]);
$price = wc_price($product->get_price());
?>
<li class="product type-product status-publish has-post-thumbnail first instock shipping-taxable purchasable product-type-simple">
<a href="<?php echo $link; ?>" class="woocommerce-LoopProduct-link">
<span class="et_shop_image">
<img width="400" height="400"
src="<?php echo $featured_image[0]; ?>"
class="attachment-shop_catalog size-shop_catalog wp-post-image"
alt=""
title="">
<span class="et_overlay"></span>
</span>
<h3><?php echo $title; ?></h3>
<span class="price">
<span class="woocommerce-Price-amount amount">
<?php echo $price; ?>
</span>
</span>
</a>
</li>
<?php } ?>
</ul>
</div>
Many thanks for any help!
get_field()
can take three parameters. The first one is mandatory but the last 2 are optional.
get_field($selector, [$post_id], [$format_value]);
Where the $selector
is the name of the field.The $post_id
is self explanatory, but is defaulted to the current post and the $format_value
decides whether you want to apply formatting logic.
Because you are calling the function via get_field('price_obj')
and omitting the ID of the post you want it is defaulting to the current post in this case the post of the main item.
This is wrong, because you were inside the WordPress loop the right object was being saved into $price
.
As you mentioned below, when you went to access the object via
$price = wc_price($product->get_price());
You were accessing the $product
object, which is the main item. But your sub-product were stored in $product_prod, so to access it's price you had to change your code to
$price = wc_price($product_prod->get_price());