Php 在woocommerce归档页面中显示自定义产品价格,并用于追加销售/相关产品
是否有办法在WordPress仪表板中启用后端字段,以便在归档页面中显示每个产品的自定义价格,以及追加销售/相关产品的自定义价格,但将价格保留在产品摘要中 示例:产品A的价格为10欧元,但我想展示的是6欧元/千克 最简单的方法是使用一个自定义字段,它覆盖了woocommerce\u模板\u循环\u价格,但这段代码不起作用,但我不明白为什么Php 在woocommerce归档页面中显示自定义产品价格,并用于追加销售/相关产品,php,wordpress,woocommerce,product,custom-fields,Php,Wordpress,Woocommerce,Product,Custom Fields,是否有办法在WordPress仪表板中启用后端字段,以便在归档页面中显示每个产品的自定义价格,以及追加销售/相关产品的自定义价格,但将价格保留在产品摘要中 示例:产品A的价格为10欧元,但我想展示的是6欧元/千克 最简单的方法是使用一个自定义字段,它覆盖了woocommerce\u模板\u循环\u价格,但这段代码不起作用,但我不明白为什么 add_action('woocommerce_template_loop_price', 'shopprice_change', 10, 2); funct
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 );
问题:实际上,单产品页面上的追加销售和相关产品也应该更改。但如果是产品返回$price\u html;这也排除了他们。帮助解决这件事的人会得到赏金。谢谢大家! 这个插件可能会有所帮助,Woocommerce额外价格字段来源: woocommerce\u模板\u循环\u价格不是一个动作,而是一个函数 使用下面的代码
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 );
但如果是产品返回$price\u html;它把他们排除在外
嗯
用它代替is_产品对我来说很好:
is_single( $product->get_id() )
您不应该直接调用$product->id。相反,使用$product->get\u 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 );
谢谢你的帮助,但是这个代码不起作用。控制台中没有错误您正在将帖子元保存为shoppricechange,并希望替换为price,因此,如果您的产品价格和shoppricechange不为空,则此代码完全有效。这对我来说不起作用。但考虑到您的代码,我找到了一种更接近解决方案的方法,请参见更新的问题。谢谢你!也许你知道如何调整它?因为你已经更新了问题,我也在更新答案,请检查它是否有效。这太棘手了…让我们看看它是否适用于你