我在我的插件里有一些这样的过滤器
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"]');
}
}