在Woocommerce 3中

时间:2018-07-18 10:03:27

标签: php wordpress woocommerce custom-taxonomy price

在Woocommerce中是否可以更改特定产品类别中所有产品的价格?

例如,如果我要将“衬衫”产品类别中的所有产品价格更改为50,可以吗?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

要以固定金额50更改产品价格整个产品类别 shirts

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'conditional_custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'conditional_custom_price', 99, 2 );
// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'conditional_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'conditional_custom_price', 99, 2 );
function conditional_custom_price( $price, $product ) {
    if( has_term( 'shirts', 'product_cat', $product->get_id() ) ){
        // Delete product cached price  (if needed uncomment it)
        // wc_delete_product_transients($product->get_id());

        $price = 50; // X2 for testing
    }
    return $price;
}

// Variations
add_filter('woocommerce_variation_prices_price', 'conditional_custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'conditional_custom_variation_price', 99, 3 );
function conditional_custom_variation_price( $price, $variation, $product ) {
    if( has_term( 'shirts', 'product_cat', $product->get_id() ) ){
        // Delete product cached price  (if needed uncomment below)
        // wc_delete_product_transients($variation->get_id());

        $price = 50;
    }
    return $price;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

基于以下答案:Change product prices via a hook in WooCommerce 3

它处理所有Woocommerce产品类型。有条件的Wordpress函数has_term()可以处理有关任何产品类别的术语ID 术语术语名称