0

I am building a website with whole bunch of custom fields and need to implement several custom fields that will auto calculate values by using values from other custom fields. All files which I need to use in these calculation are in loop, so I would love to use hooks in functions.php.

Can somebody give me few examples on how to wrote filters with some simple calculations so I could continue writing filters for different problems I need to solve, like:


Example 1: Sum 2 Numbers

Custom Field 1: product_price
Custom Field 2: addon_price
Custom Field 3: price_total

Formula:

price_total = product_price + addon_price

Example 2: Add commision on price

Custom Field 1: product_price
Custom Field 2: tax_commision - [%]
Custom Field 3: price_total

Formula:

price_total = product_price + ( tax_commision / 100 ) * product_price
2
  • Please show us your research and what you've already tried.
    – kaiser
    Sep 2, 2013 at 11:40
  • @kaiser, no I dont have anything jet, but I think that solution from joostvanhoof will work fine. Thank you.
    – pendjer
    Sep 2, 2013 at 13:03

2 Answers 2

3

Define your own template tag

One idea is to use the template tag:

<?php the_price();?>

or

<?php the_price( '%.1f' );?>

to change the format to your needs.

Then you can add filters to it as you like.

In your functions.php file you could add

// setup the price filters:
add_filter( 'the_price' , 'xyz_add_product_price',      1, 1 );
add_filter( 'the_price' , 'xyz_add_commission',         2, 1 );
add_filter( 'the_price' , 'xyz_add_shipping',           3, 1 );

where you use the priority to control the order of the calculations.

The xyz_ is just a prefix that you can change.

The definition of the template tag could look like this:

/**
* Template tag to display the price
* @param  string $d Output format
* @return void
*/
function the_price( $d = '' ){
    echo get_the_price( $d );
}

where:

/**
* Template tag to return the price
* @param  string $d     Output format
* @return number $price Price
*/
function get_the_price( $d = '' ){

        $price = apply_filters( 'the_price', 0 );

        if( empty( $d ) )
                $d =  '%.2f'; // with 2 decimals

        return sprintf( $d, $price );
}

It might be a good idea to check if these template tags already exists with function_exists() or use an additional custom prefix.

Add the price filters

Your first price filter might be to add the product price:

/**
* Price filter #1
* Add product price 
* @param number $price
* @return number $price
*/
function xyz_add_product_price( $price ) {

        $product_price  = get_post_meta( get_the_ID(), 'product_price', TRUE );

        if( ! empty(  $product_price ) ){

                // convert the values from string to int/float:
                $product_price = 1 * $product_price;

                // add product price:
                return ( $price + $product_price );
        }else{
                return $price;
        }
}

and the second one for the tax commmision:

/**
* Price filter #2
* Add tax commission to the product price
* @param number $price
* @return number $price
*/
function xyz_add_commission( $price = 0 ) {

    $tax_commision  = get_post_meta( get_the_ID(), 'tax_commision', TRUE );

    if( ! empty(  $tax_commision ) ){
            // convert the values from string to int/float:
            $tax_commision = 1 * $tax_commision;

            // calculate the commision and add to the price:
            return ( $price  + ( $tax_commision / 100 ) * $price ) ;
    }else{
            return $price;
    }
}

and finally the third one for the shipping fee:

/**
* Price filter #3
* Add shipping fee to the product price
* @param number $price
* @return number $price
*/

function xyz_add_shipping( $price ) {
       $shipping_fee = 10;
       return ( $price + $shipping_fee );
}

... and you can add more later on.

2
  • Thank you @birgire. I learn a lot from your code. I test your code and then I wrote my own functions. Thank you one more time. Now I am able to contiue on my own :)
    – pendjer
    Sep 4, 2013 at 22:14
  • Great to hear that it helped you along the way, good luck with your project - cheers
    – birgire
    Sep 5, 2013 at 3:33
0

I don't really understand why you have a custom field with the total price (do you fill in these by hand?). Would you like to calculate the total price in the admin area? I don't know how to do that, but I assume you would like to display the total price on the front-end.

If so, then I'd write a function which has 2 input arguments, which can be $product and $addon (for example 2 it's $product and $tax). Then you can use the function to calculate the total price and display it on the front-end.

You can write a function in functions.php which would look something like this:

Example 1

function sum_2_numbers($product, $addon){
    $price_total = $product + $addon;
    return $price_total;
}

On the page where you would like to display the total price:

<?php echo sum_2_numbers(get_post_custom('product_price'), get_post_custom('addon_price')); ?>

Example 2

function commission($product, $tax){
    $price_total = $product + ($tax / 100) * $product;
    return $price_total;
}

On the page where you would like to display the total price:

<?php echo commission(get_post_custom('product_price'), get_post_custom('tax_commission')); ?>
4
  • Thank you @joostvanhoof and birgire. I will try both solutions. joostvanhoof you are right. I don't need custom field for total price. I just want to display total price on front end. One more time, thank you guys for your efforts.
    – pendjer
    Sep 2, 2013 at 13:01
  • No problem, let me know if it works! If so you can accept the answer.
    – eskimo
    Sep 2, 2013 at 13:04
  • Hi joostvanhoof. I try your code but I fail 3 times. Then I try PRICE FILTER function from birgire and I made it. This is how I solve it: (code.google.com/p/my-simple-calculations)
    – pendjer
    Sep 5, 2013 at 17:53
  • Ah yes I see. My code wasn't quite right after all. Thanks for the notice (and the final code)!
    – eskimo
    Sep 6, 2013 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.