# Custom Woocommerce Function

**URL:** <https://www.phphelp.com/t/custom-woocommerce-function/21614>\
**Category:** Help with WordPress\
**Created:** [November 27, 2013, 4:46pm UTC](https://www.phphelp.com/t/custom-woocommerce-function/21614 "2013-11-27T16:46:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tascam424](https://www.phphelp.com/letter_avatar_proxy/v4/letter/t/bcef8e/32.png) [@tascam424](https://www.phphelp.com/u/tascam424)\
**Post date:** [November 27, 2013, 4:46pm UTC](https://www.phphelp.com/t/custom-woocommerce-function/21614/1 "2013-11-27T16:46:32Z")

</div>

I was wondering if anybody would be so kind as to help me write a woocommerce function.  
I am by no means a coder so i really appreciate any help you can offer.

What i wish to achieve is for a function to calculate the quantity of items in cart and then change the total order price based on predefined values.

So for example all items are £2.50 … what i want is for the function to count the items and if 5 items are in the cart change the cart total to £10 … if 10 items are added change the cart total to £20.00.

I am aware of some discount plugins which may achieve something similar, but i don’t actually want to apply a discount, i would really prefer to change the total automatically and not display a discount.

Once again i greatly appreciate any help.

---

<div class="post-metadata">

**Author:** ![darkfreaks](https://www.phphelp.com/letter_avatar_proxy/v4/letter/d/4bbf92/32.png) [@darkfreaks](https://www.phphelp.com/u/darkfreaks)\
**Post date:** [March 15, 2023, 11:41pm UTC](https://www.phphelp.com/t/custom-woocommerce-function/21614/2 "2023-03-15T23:41:39Z")

</div>

```auto
function calculate_cart_price(){
    global $woocommerce;
    $cart_total = 0;
    // Get cart total
    foreach ( $woocommerce->cart->get_cart() as $key => $values ) {
        $cart_total += $values['quantity'];
    }
    // Calculate the order total based on the quantity
    if ($cart_total >= 1 && $cart_total <= 10) {
        $order_total = 50;
    } elseif ($cart_total >= 11 && $cart_total <= 20) {
        $order_total = 100;
    } elseif ($cart_total >= 21 && $cart_total <= 30) {
        $order_total = 150;
    }
    // Set the order total

```
