【问题标题】:Display a custom product price in woocommerce archive page and for upsells/related products在 woocommerce 存档页面和加售/相关产品中显示自定义产品价格
【发布时间】: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


    【解决方案1】:

    但是 if (is_product()) return $price_html; 将它们排除在外 好吧。

    用它代替is_product() 对我来说效果很好:

    is_single( $product->get_id() )
    

    你不应该直接打电话给$product-&gt;id。相反,请使用$product-&gt;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 );
    

    【讨论】:

      【解决方案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 );
        

      【讨论】:

      • 感谢您的帮助,但此代码不起作用。控制台没有错误
      • 您将帖子元数据保存为 shoppricechange 并且您想用价格替换,因此如果您的产品价格和 shoppricechange 不为空,则此代码完全有效
      • 这对我不起作用。但是考虑到您的代码,我找到了一种更接近解决方案的方法(请参阅更新的问题)。感谢那!也许你知道如何适应它?
      • 因为你已经更新了问题,我也在更新答案,请检查它是否有效,它太棘手了......让我们看看它是否适合你
      【解决方案3】:

      也许这个插件可以提供帮助,Woocommerce Extra Price Fields(来源:https://wordpress.org/plugins/woocommerce-extra-price-fields/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        相关资源
        最近更新 更多