Ich versuche related.php zu modifizieren, um zufällig alle drei Produkte aus dem Shop zu zeigen, die einen Preis im Bereich von plus hat/minus 100WooCommerce Verwandte Produkte - Produkte anzeigen Preisintervall
I benutze ACF Felder und Woocommerce 3.2. Das Problem ist, dass obwohl die Produkte korrekt ausgewählt sind, ihr Preis nicht angezeigt wird. Stattdessen wird der Preis des Referenzprodukts für alle 3 Produkte angezeigt. Hier
ist der Code (price_obj ist das ACF-Feld für Preis):
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>
Vielen Dank für jede Hilfe!
ändern. Get_filed() kann drei Parameter verwenden. die ID des Feldes, die Post-ID und den Format-Wert, fügen Sie einfach die Post-ID zum Aufruf hinzu, wie z. B. $ price = get_field ('price_obj', get_the_ID()); oder wenn Sie die ID zuerst bekommen, dann können Sie einfach get_field ('price_obj', $ id) verwenden; –