简体   繁体   English

在woocommerce存档页面和加售/相关产品中显示自定义产品价格

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

Is there a way to enable a backend field in the WordPress dashboard to show a custom price per product in the archive page and for upsells/related products but leave the price in product-summary? 有没有一种方法可以使WordPress仪表板中的后端字段在存档页面和加售/相关产品中显示每个产品的自定义价格,但将价格保留在产品摘要中?

Example: Product A has a price of 10€ but I would like to show "from 6€/kg" instead. 示例:产品A的价格为10欧元,但我想显示“起价为6欧元/千克”。

The easiest way would be to use a custom field which is overriding the woocommerce_template_loop_price but this code doesn't work but I don't understand why. 最简单的方法是使用覆盖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 UPDATE

I've found a solution for changing the price in archive page without changing on single product page: 我找到了一种解决方案,可以在不更改单个产品页面的情况下更改存档页面中的价格:

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 );

Problem: Actually the upsells and related products should change as well on the single product page. 问题:实际上,加售商品和相关产品也应该在单个产品页面上进行更改。 But with if (is_product()) return $price_html; 但是if (is_product()) return $price_html; its excluding them as well. 也将它们排除在外。 Who helps solving this will get the bounty. 谁能解决这个问题将得到赏金。 Thank you! 谢谢!

But with if (is_product()) return $price_html; 但是if (is_product()) return $price_html; its excluding them as well. 也将它们排除在外。

Using this in place of the is_product() works well for me: 用它代替is_product()对我来说很好:

is_single( $product->get_id() )

And you shouldn't call $product->id directly. 而且您不应该直接调用$product->id Instead, use $product->get_id() . 而是使用$product->get_id()

Here's the full code I used: 这是我使用的完整代码:

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 is not a action it's a function woocommerce_template_loop_price不是动作,而是函数

  2. Use the code below 使用下面的代码

     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.

相关问题 WooCommerce:仅在单个产品页面上显示价格后缀 - 不适用于相关产品 - WooCommerce: Show price suffix only on single product Page - Not for related products 在WooCommerce加售之前显示自定义属性(链接的产品) - Display custom attributes before WooCommerce upsells ( linked products) 在 WooCommerce 上显示自定义价格范围 可变产品的产品循环 - Display a custom price range on WooCommerce Product loops for variable products 显示Woocommerce中按产品属性过滤的相关产品 - Display related products filtered by product attribute in Woocommerce 在单个产品页面上显示相关产品中的自定义属性 - Display custom attributes in related products on single product page 在单个产品页面上增加相关产品(woocommerce) - Increase related products on single product page (woocommerce) Woocommerce中产品类别归档页面和相关单品的条件逻辑 - Conditional logic for product category archive pages and related single products in Woocommerce 在 WooCommerce 折扣产品上显示自定义价格后缀 - Display custom price suffix on WooCommerce discounted products 在Woocommerce 3中用特定产品类别的产品替换加售商品 - Replace Upsells with products from a specific product category in Woocommerce 3 在存档页面上显示 Woocommerce 产品属性 - Display Woocommerce product attribute on archive page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM