通过WooCommerce 3.3中的钩子更改产品变化价格

时间:2018-04-20 13:56:48

标签: php wordpress woocommerce product price

我使用钩子来定制可变产品价格。但是,this answer似乎不适用于Woocommerce 3.3.5。我在functions.php文件中使用了以下内容(来自上一篇文章):

add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );
function custom_variation_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
wc_delete_product_transients($variation->get_id());

return $price * 3; // X2 for testing
}

在产品页面上,产品变化的价格范围显示正确的价格(原价* 3)但是当我选择选项时,显示的变化价格是未过滤的。我在这里错过了什么吗?

编辑:我对简单产品和产品差异的价格计算略有不同。我最终得到了3个功能:

// Simple
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );

// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price_2', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price_2' , 99, 2 );

// Variations (of a variable product)
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );

function custom_price( $price, $product ) {
// Delete product cached price  (if needed)
wc_delete_product_transients($product->get_id());

return $price * 3; // X3 for testing
}

function custom_price_2( $price, $product ) {
// Delete product cached price  (if needed)
wc_delete_product_transients($product->get_id());

return $price * 2; // X2 for testing
}

function custom_variation_price( $price, $variation, $product ) {
// Delete product cached price  (if needed)
wc_delete_product_transients($variation->get_id());


return $price * 2; // X2 for testing
}

1 个答案:

答案 0 :(得分:2)

linked code仍可用于Woocommerce 3.3.x,请参阅在Woocommerce 3.3.x上测试的this related recent accepted working answer ...您的代码尚未完成...

您需要使用:

// Variable
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price' , 99, 2 );

// Variations (of a variable product)
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );

function custom_price( $price, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());

    return $price * 3; // X3 for testing
}

function custom_variation_price( $price, $variation, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($variation->get_id());

    return $price * 3; // X3 for testing
}