In a normal case the Woocommerce retrieves the price from the database and displays it. How can one customize Woocommerce so that when the price of the product is retrieved from the database it is then changed at the very initial stage before it is used anywhere else (for checkout, cart etc).


enter image description here


The price should not be changed in the database. PHP script should change the price and manage it.


I'm not getting any directions in this problem. I believe that I've to modify class-wc-product-simple.php at line:68

       if ( $children_by_price ) {
            foreach ( $children_by_price as $child ) {
                $child_price = get_post_meta( $child, '_price', true ); // that's line 68.
                update_post_meta( $this->get_parent(), '_price', $child_price );

so that I can modify the price as it is just retrieved from the database.I know that modifying WooCommerce core is not recommended, but that's okay for me for the time being.

  • Technically, this is not really WordPress-specific, but I have a soft spot for WooCommerce, and you put a lot of effort into your question, so I will try to point you in the right direction. See the WC_Product abstract class and specifically get_price() method and the woocommerce_get_price filter therein. Nov 18 '13 at 12:43
  • Take a look at the WP HTTP API to query that third party data. Then edit your quesiton and show us your actual code.
    – kaiser
    Nov 18 '13 at 13:03
  • @helgatheviking Thank you very much for giving directions. I'd fixed the problem and shared the answer.
    – Omar Tariq
    Nov 18 '13 at 14:42

Finally, got it working with this code:-

function return_custom_price($price, $product) {       
        global $post, $woocommerce;
        $post_id = $post->ID;
        if($post_id == '696' || $post_id == '697' || $post_id == '705' /*|| $post_id == '53'*/ || $post_id == ''){
        // cart, checkout, , order recieved, order now
                $post_id = $product->id;
        $new_price = get_post_meta($post_id, '_price', true);
        if($new_price=='') {
            $new_price = $price;
        } else {
            $api_call = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%22http%3A%2F%2Ffinance.yahoo.com%2Fd%2Fquotes.csv%3Fe%3D.csv%26f%3Dc4l1%26s%3DNZD%3DX%2CAUD%3DX%22%3B&format=json&diagnostics=true&callback=";
            $data[] = json_decode(file_get_contents($api_call));
            $nzd = (double) $data[0]->query->results->row[0]->col1;
            $aud = (double) $data[0]->query->results->row[1]->col1;
            $new_price = ( $nzd / $aud ) * $new_price;
        return $new_price;         
add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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