do_shortcode 不考虑 woocommerce 本机产品短代码的价格过滤器

问题描述 投票:0回答:1

我的插件中有一些这样的过滤器

add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_sale_price', 'custom_price', 99, 2 );
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_sale_price', 'custom_variable_price', 99, 3 );
... and so on
function custom_variable_price( $price, $variation, $product ) {
    
    if((!is_admin() && !wp_doing_ajax()) && (is_shop() || is_product_category() || is_product_tag() || is_product() || is_front_page())) {

        return get_discounted_price($product, $price);

    }

    return $price;

}

这可以在任何地方动态地改变我的价格,即使使用 woocommerce 本机产品短代码(通过页面编辑器),除非在函数中的 do_shortcode 中调用它。并且无法弄清楚为什么。

这是这些过滤器不起作用的示例

add_action('woocommerce_after_cart', 'offer_additional_products_cart');
function offer_additional_products_cart() {
    
    if(is_cart()) {
        
        echo '
        <div class="company_home_title">'.__('Do not miss', 'company_translations').'</div>
        ';
        
        echo do_shortcode('[products limit="4" orderby="popularity" class="quick-sale" on_sale="true"]');
        
    }
    
} 
php wordpress woocommerce conditional-statements shortcode
1个回答
0
投票

这是完全正常的,因为您在购物车页面

中使用
do_shortcode()并且您的自定义价格不允许在购物车中显示(请参阅
IF
声明中的限制)

要在购物车页面中允许自定义产品价格,您应该在

is_cart()
声明中添加
IF

© www.soinside.com 2019 - 2024. All rights reserved.