Home > Backend Development > PHP Tutorial > How Can I Programmatically Change Product Prices in WooCommerce 3 ?

How Can I Programmatically Change Product Prices in WooCommerce 3 ?

Mary-Kate Olsen
Release: 2024-12-08 03:24:10
Original
724 people have browsed it

How Can I Programmatically Change Product Prices in WooCommerce 3 ?

Change Product Prices Via a Hook in WooCommerce 3

The WooCommerce platform provides various hooks to modify product prices. While the solution presented using add_filter('woocommerce_get_regular_price') and add_filter('woocommerce_get_price') works for simple products, it encounters limitations when it comes to variation products.

Variation Product Pricing

To adjust variation product prices, the following updated hooks are recommended:

  • Simple, Grouped, and External Products:

    • woocommerce_product_get_price
    • woocommerce_product_get_regular_price
  • Variations:

    • woocommerce_product_variation_get_regular_price
    • woocommerce_product_variation_get_price
  • Variable Product Range:

    • woocommerce_variation_prices_price
    • woocommerce_variation_prices_regular_price

Plugin Implementation

To implement these hooks within a plugin, consider the following:

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_variation_get_regular_price', 'custom_price', 99, 2);
add_filter('woocommerce_product_variation_get_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);

function custom_price($price, $product) {
    $multiplier = 2; // Adjust as needed
    return (float) $price * $multiplier;
}

function custom_variable_price($price, $variation, $product) {
    $multiplier = 2; // Adjust as needed
    return (float) $price * $multiplier;
}
Copy after login
Copy after login

Theme Implementation

If you prefer a theme-based approach, include the following code in your functions.php file:

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_variation_get_regular_price', 'custom_price', 99, 2);
add_filter('woocommerce_product_variation_get_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);

function custom_price($price, $product) {
    $multiplier = 2; // Adjust as needed
    return (float) $price * $multiplier;
}

function custom_variable_price($price, $variation, $product) {
    $multiplier = 2; // Adjust as needed
    return (float) $price * $multiplier;
}
Copy after login
Copy after login

Additional Notes and Enhancements

  • Pricing Widget: For manipulation of price filter widget minimum and maximum values, consider using the following hooks: woocommerce_price_filter_widget_min_amount and woocommerce_price_filter_widget_max_amount.
  • Product Sale Pricing: Specific hooks exist for sale prices: woocommerce_product_get_sale_price, woocommerce_variation_prices_sale_price, and woocommerce_product_variation_get_sale_price.
  • Price Caching: Cached variation prices in WooCommerce 3 must be considered. The add_filter('woocommerce_get_variation_prices_hash') filter is recommended to efficiently handle price caching.

The above is the detailed content of How Can I Programmatically Change Product Prices in WooCommerce 3 ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template