Last updated on October 18, 2023

WooCommerce product pricing strategies

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Strategize product pricing in WooCommerce for higher conversions.

One common pricing strategy in WooCommerce is to offer discounts based on the quantity of products purchased. This can be achieved by using a custom PHP function that hooks into the woocommerce_product_get_price filter. Here’s an example code snippet that demonstrates how to implement this strategy:

function wpsnippets_quantity_based_pricing( $price, $product ) {
    // Get the quantity of the product being purchased
    $quantity = WC()->cart->get_cart_contents_count();

    // Define the pricing tiers and corresponding discounts
    $pricing_tiers = array(
        20 => 0.7   // 30% discount for 20 or more products
        10 => 0.8,  // 20% discount for 10 or more products
        5 => 0.9,   // 10% discount for 5 or more products
    );

    // Apply the discount based on the quantity
    foreach ( $pricing_tiers as $tier_quantity => $discount ) {
        if ( $quantity >= $tier_quantity ) {
            $price = $price * $discount;
            break;
        }
    }

    return $price;
}
add_filter( 'woocommerce_product_get_price', 'wpsnippets_quantity_based_pricing', 10, 2 );

In this code snippet, we define a function wpsnippets_quantity_based_pricing that takes the original price and the product object as parameters. We then retrieve the quantity of the product being purchased from the WooCommerce cart using WC()->cart->get_cart_contents_count().

Next, we define an array $pricing_tiers that represents the pricing tiers and their corresponding discounts. Each key-value pair in the array represents a tier where the key is the minimum quantity required and the value is the discount to be applied.

We then loop through the pricing tiers and check if the quantity is greater than or equal to the tier quantity. If it is, we apply the corresponding discount to the price by multiplying it with the discount value. We break out of the loop after applying the first applicable discount.

Finally, we return the modified price. The add_filter function is used to hook our custom function into the woocommerce_product_get_price filter, which allows us to modify the product price based on the quantity.

This code snippet can be useful for e-commerce websites using WooCommerce that want to implement quantity-based pricing strategies to incentivize customers to purchase larger quantities of products.

Examples

Example 1: Dynamic Pricing based on User Role

This use case demonstrates how to implement dynamic pricing for WooCommerce products based on the user role. The code example below shows how to modify the product price based on the user’s role using the woocommerce_product_get_price filter hook.

function wpsnippets_dynamic_pricing_based_on_user_role( $price, $product ) {
    // Check if user is logged in
    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
        $user_role = $user->roles[0];

        // Apply different pricing based on user role
        switch ( $user_role ) {
            case 'customer':
                $price *= 1.2; // Increase price by 20% for customers
                break;
            case 'wholesale_customer':
                $price *= 0.8; // Decrease price by 20% for wholesale customers
                break;
        }
    }

    return $price;
}
add_filter( 'woocommerce_product_get_price', 'wpsnippets_dynamic_pricing_based_on_user_role', 10, 2 );

In this code example, we define a custom function wpsnippets_dynamic_pricing_based_on_user_role that hooks into the woocommerce_product_get_price filter. It checks if the user is logged in and retrieves their role. Based on the user role, the product price is adjusted accordingly. For example, customers see a 20% price increase, while wholesale customers see a 20% price decrease.

Example 2: Tiered Pricing based on Quantity

This use case demonstrates how to implement tiered pricing for WooCommerce products based on the quantity purchased. The code example below shows how to modify the product price based on the quantity using the woocommerce_product_get_price filter hook.

function wpsnippets_tiered_pricing_based_on_quantity( $price, $product ) {
    $quantity = $product->get_quantity(); // Get the product quantity

    // Apply tiered pricing based on quantity
    if ( $quantity >= 10 ) {
        $price *= 0.9; // 10% discount for quantity 10 or more
    } elseif ( $quantity >= 5 ) {
        $price *= 0.95; // 5% discount for quantity 5 or more
    }

    return $price;
}
add_filter( 'woocommerce_product_get_price', 'wpsnippets_tiered_pricing_based_on_quantity', 10, 2 );

In this code example, we define a custom function wpsnippets_tiered_pricing_based_on_quantity that hooks into the woocommerce_product_get_price filter. It retrieves the product quantity and applies tiered pricing based on the quantity. For example, if the quantity is 10 or more, a 10% discount is applied, and if the quantity is 5 or more, a 5% discount is applied.

Example 3: Dynamic Pricing based on Product Category

This use case demonstrates how to implement dynamic pricing for WooCommerce products based on the product category. The code example below shows how to modify the product price based on the category using the woocommerce_product_get_price filter hook.

function wpsnippets_dynamic_pricing_based_on_category( $price, $product ) {
    $categories = $product->get_category_ids(); // Get the product categories

    // Apply different pricing based on product category
    if ( in_array( 'sale', $categories ) ) {
        $price *= 0.8; // 20% discount for products in the "Sale" category
    } elseif ( in_array( 'new', $categories ) ) {
        $price *= 1.1; // 10% increase for products in the "New" category
    }

    return $price;
}
add_filter( 'woocommerce_product_get_price', 'wpsnippets_dynamic_pricing_based_on_category', 10, 2 );

In this code example, we define a custom function wpsnippets_dynamic_pricing_based_on_category that hooks into the woocommerce_product_get_price filter. It retrieves the product categories and applies different pricing based on the category. For example, if the product belongs to the “Sale” category, a 20% discount is applied, and if it belongs to the “New” category, a 10% increase is applied.

Last updated on October 18, 2023. Originally posted on October 24, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *