【发布时间】:2019-03-08 15:21:59
【问题描述】:
有没有办法在 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();
}
更新
我找到了一种解决方案,可以在存档页面中更改价格而不在单个产品页面上进行更改:
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; 也将它们排除在外。谁帮助解决这个问题将获得赏金。谢谢!
【问题讨论】:
标签: php wordpress woocommerce product custom-fields