WooCommerce is a powerful eCommerce platform that allows you to customize almost every aspect of your store. One common customization is dynamically changing product prices and titles in the cart based on user input, special campaigns, or other conditions. In this tutorial, we will walk you through how to achieve this functionality using hooks in WooCommerce.
Why Dynamically Change Product Prices and Titles?
There are various reasons why you might want to change a product’s price or name dynamically:
- Running promotional campaigns with different pricing.
- Adjusting the price based on custom options selected by the user.
- Displaying personalized product names based on a user’s action.
- Implementing special pricing rules for bulk orders or membership discounts.
Now, let’s dive into the step-by-step implementation.
Step 1: Hook into woocommerce_before_calculate_totals
WooCommerce provides the woocommerce_before_calculate_totals
hook, which allows us to modify cart item details before the final total is calculated. Add the following code to your theme’s functions.php
file or a custom plugin:
add_action('woocommerce_before_calculate_totals', 'customize_cart_item_details', 20, 1);
function customize_cart_item_details($cart) {
// Ensure we are not in the admin area unless it's an AJAX request
if (is_admin() && !defined('DOING_AJAX')) return;
// Avoid duplicate execution
if (did_action('woocommerce_before_calculate_totals') >= 2 || !is_user_logged_in()) return;
// Loop through cart items
foreach ($cart->get_cart() as $cart_item) {
// Check if the item matches a specific product ID or variation ID
if (isset($cart_item['custom_title']) && isset($cart_item['total_cost'])) {
$cart_item['data']->set_price($cart_item['total_cost']);
$cart_item['data']->set_name($cart_item['custom_title']);
}
}
}
Explanation:
- The function first checks whether we are in the admin area and ensures that the hook doesn’t execute multiple times.
- It then loops through the cart items and modifies the price and product name based on custom data stored in the cart item.
Step 2: Add Custom Data When Adding a Product to the Cart
To ensure that our cart items contain the necessary dynamic values, we need to add this data when adding the product to the cart. You can do this with the WC()->cart->add_to_cart()
function:
$cart_item_data = [
'custom_title' => "Product Title",
'total_cost' => 100
];
WC()->cart->add_to_cart($product_id, 1, 0, [], $cart_item_data);
Explanation:
campaign_id
: This is a unique identifier for the campaign.custom_title
: A custom product name, which will be displayed in the cart.total_cost
: The dynamically calculated price of the product.
Step 3: Verify Dynamic Changes in the Cart
Once the above code is implemented, add a product to the cart and check if the price and name have changed accordingly. You can also print the cart item details for debugging:
echo '<pre>';
print_r(WC()->cart->get_cart());
die();
Conclusion
By using woocommerce_before_calculate_totals
and modifying cart items dynamically, you can create a flexible pricing system in WooCommerce. Whether it’s a special campaign, user-specific pricing, or personalized product names, this approach gives you full control over how products appear in the cart.
If you have any questions or need further customizations, feel free to leave a comment below!
Bonus Tip: Use a Custom Plugin
Instead of adding code to functions.php
, consider creating a simple plugin for better maintainability. Create a new folder in wp-content/plugins/
, add a PHP file, and paste the code inside a plugin structure.
Need Help with WooCommerce Customization? Our team specializes in WooCommerce development. Contact us today for custom solutions tailored to your business!