简体   繁体   English

按ID显示WooCommerce变化(常规或销售)价格

[英]Display WooCommerce variation (regular or sale) price by ID

I am developing a webshop where customers can add and edit product themselves on the homepage. 我正在开发一个网上商店,客户可以在该网上商店中自己添加和编辑产品。 They can do this through advanced custom fields. 他们可以通过高级自定义字段来执行此操作。

It worked fine until I suddenly noticed that the sale price of a product was not shown. 效果很好,直到我突然发现没有显示产品的销售价格。 This is because it is a variation of the main product. 这是因为它是主要产品的变体。

I currently use this code to retrieve the advanced custum field and the linked product: 我目前使用以下代码来检索高级custum字段和链接的产品:

<?php
$post_object = get_field('product_1'); 
if( $post_object ): 
$post = $post_object;
setup_postdata( $post );
$product_id = $post->ID;
$product = wc_get_product( $product_id );
$category = get_the_terms( $post->ID, 'product_cat' );
$category_link = get_term_link( $category[0]->term_id, 'product_cat' );
$category_id = get_term_by('id', $category[0]->term_id, 'product_cat'); 
$category_name = $category_id->name;
?>
<?php 
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
?>
<a href="<?php the_permalink(); ?>">
<div class="badge-wrapper">
<img class="sale-badge" src="/wp-content/uploads/sale-image.png" />
<?php echo $product->get_price_html(); ?>
</div>
<img src="<?php echo $image[0]; ?>" data-id="<?php echo $post->ID; ?>">
<h6><?php the_title(); ?></h6>
</a>
<a class="more-info" href="<?php echo $category_link; ?>">View all</a>
<?php wp_reset_postdata();?>
<?php endif; ?>

I know, maybe this is not the best way to get all the information, but it works. 我知道,也许这不是获取所有信息的最佳方法,但它确实有效。 At least until now. 至少到现在为止。 I have found and tried many different tutorials but not 1 works well. 我发现并尝试了许多不同的教程,但其中1种效果不佳。 I came closest with this tutorial: 我最接近本教程:

functions.php 的functions.php

function get_variation_price_by_id($product_id, $variation_id){
    $currency_symbol = get_woocommerce_currency_symbol();
    $product = new WC_Product_Variable($product_id);
    $variations = $product->get_available_variations();
    $var_data = [];
    foreach ($variations as $variation) {
        if($variation['variation_id'] == $variation_id){
            $display_regular_price = $variation['display_regular_price'].'<span class="currency">'. $currency_symbol .'</span>';
            $display_price = $variation['display_price'].'<span class="currency">'. $currency_symbol .'</span>';
        }
    }

    //Check if Regular price is equal with Sale price (Display price)
    if ($display_regular_price == $display_price){
        $display_price = false;
    }

    $priceArray = array(
        'display_regular_price' => $display_regular_price,
        'display_price' => $display_price
    );
    $priceObject = (object)$priceArray;
    return $priceObject;
}

And with this code to return the results, where "9" is the post-ID and 15 the variation ID: 并使用此代码返回结果,其中“ 9”是发布ID,而15是变化ID:

<?php 
    $variation_price = get_variation_price_by_id(9, 15);
    echo $variation_price -> display_regular_price;
    echo $variation_price -> display_price;
?>

I allready got $post->ID; 我已经准备好了$ post-> ID; but I dont know how to get the variation ID of the product (post) 但我不知道如何获取产品的版本ID(发布后)

How can I do this in a good way? 我怎样才能很好地做到这一点?

You can use the function get_price_html() 您可以使用函数get_price_html()

$product->get_price_html()

I had the same problem once in ajax search pro, i made some code that split the html to parts so you can style it as you want. 我曾经在ajax search pro中遇到过同样的问题,我编写了一些代码将html分成几部分,以便您可以根据需要对其进行样式设置。 You can see if you can use it :-) 您可以查看是否可以使用它:-)

<?php
$r_product = wc_get_product($r->id);
$price_html = strip_tags($r_product->get_price_html());
$pieces = explode(' ', $price_html);
if(count($pieces)) {
    echo'<div class="search-result-price">';
    if(count($pieces) === 3) {
        $price .= '<ins>';
        foreach($pieces as $piece) {
            $price .= $piece . ' ';
        }
        $price .= '</ins>';
        echo $price;
    } else if(count($pieces) === 2) {
        echo '<del>' . $pieces[0] . '</del>';
        echo '<ins>' . $pieces[1] . '</ins>';
    } else if(count($pieces) === 1) {
        echo '<ins>' . $pieces[0] . '</ins>';
    }
    echo'</div>';
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM