簡體   English   中英

按ID顯示WooCommerce變化(常規或銷售)價格

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

我正在開發一個網上商店,客戶可以在該網上商店中自己添加和編輯產品。 他們可以通過高級自定義字段來執行此操作。

效果很好,直到我突然發現沒有顯示產品的銷售價格。 這是因為它是主要產品的變體。

我目前使用以下代碼來檢索高級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; ?>

我知道,也許這不是獲取所有信息的最佳方法,但它確實有效。 至少到現在為止。 我發現並嘗試了許多不同的教程,但其中1種效果不佳。 我最接近本教程:

的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;
}

並使用此代碼返回結果,其中“ 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;
?>

我已經准備好了$ post-> ID; 但我不知道如何獲取產品的版本ID(發布后)

我怎樣才能很好地做到這一點?

您可以使用函數get_price_html()

$product->get_price_html()

我曾經在ajax search pro中遇到過同樣的問題,我編寫了一些代碼將html分成幾部分,以便您可以根據需要對其進行樣式設置。 您可以查看是否可以使用它:-)

<?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