簡體   English   中英

在woocommerce存檔頁面和加售/相關產品中顯示自定義產品價格

[英]Display a custom product price in woocommerce archive page and for upsells/related products

有沒有一種方法可以使WordPress儀表板中的后端字段在存檔頁面和加售/相關產品中顯示每個產品的自定義價格,但將價格保留在產品摘要中?

示例:產品A的價格為10歐元,但我想顯示“起價為6歐元/千克”。

最簡單的方法是使用覆蓋woocommerce_template_loop_price的自定義字段,但是此代碼不起作用,但我不明白為什么。

add_action('woocommerce_template_loop_price', 'shopprice_change', 10, 2);
function shopprice_change ($price, $product) {
    global $post, $blog_id;
    $post_id = $post->ID;
    $price = get_post_meta($post_id, 'shoppricechange', true);
    return $price;
    wp_reset_query();
}

UPDATE

我找到了一種解決方案,可以在不更改單個產品頁面的情況下更改存檔頁面中的價格:

function cw_change_product_html( $price_html, $product ) {
    $unit_price = get_post_meta( $product->id, 'shoppricechange', true );
    if (is_product()) return $price_html;
    if ( ! empty( $unit_price ) ) {
        $price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>';  
    }
    return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );

問題:實際上,加售商品和相關產品也應該在單個產品頁面上進行更改。 但是if (is_product()) return $price_html; 也將它們排除在外。 誰能解決這個問題將得到賞金。 謝謝!

但是if (is_product()) return $price_html; 也將它們排除在外。

用它代替is_product()對我來說很好:

is_single( $product->get_id() )

而且您不應該直接調用$product->id 而是使用$product->get_id()

這是我使用的完整代碼:

function cw_change_product_html( $price_html, $product ) {
    if ( is_single( $product->get_id() ) )
        return $price_html;

    $unit_price = get_post_meta( $product->get_id(), 'shoppricechange', true );
    if ( ! empty( $unit_price ) ) {
        $price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>';
    }

    return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
  1. woocommerce_template_loop_price不是動作,而是函數

  2. 使用下面的代碼

     function cw_change_product_html( $price_html, $product ) { $unit_price = get_post_meta( $product->id, 'shoppricechange', true ); $returnedSignleProdctP =false; $trace = debug_backtrace(); $callstack = (array) $trace; foreach ($callstack as $key => $value) { if(isset($value['function']) && $value['function'] == 'woocommerce_output_related_products' ){ if ( ! empty( $unit_price ) ) { $price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>'; } } if(isset($value['function']) && $value['function'] == 'woocommerce_template_single_price' ){ $price_html = $price_html; } if(isset($value['function']) && $value['function'] == 'woocommerce_template_loop_price' ){ if ( ! empty( $unit_price ) ) { $price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>'; } } } return $price_html; } add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 ); 

Woocommerce Extra Price Fields (也許是這個插件可以提供幫助)(來源: https//wordpress.org/plugins/woocommerce-extra-price-fields/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM