首页 文章

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

提问于
浏览
2

我使用钩子来定制可变产品价格 . 但是,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),但是当我选择期权时,显示的变化价格是未过滤的 . 我在这里错过了什么吗?

EDIT: 我对简单产品和产品变化的价格计算略有不同 . 我最终得到了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 回答

  • 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
    }
    

相关问题