简体   繁体   中英

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:

<?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. I came closest with this tutorial:

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:

<?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; but I dont know how to get the variation ID of the product (post)

How can I do this in a good way?

You can use the function 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. 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>';
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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