Ask your WordPress questions! Pay money and get answers fast! (more info)

Multiply meta value, then ADD value based on IF statement WordPress

  • SOLVED

Sorry for this very long explanation. I'll try my best to be clear and brief as possible.

Product information of my shop is pulled from these database fields: id, item_number, name, price, option_2. To save time by not entering all the details manually, it automatically populate the corresponding database fields by the following function in my functions.php file:



function do_my_stuff($post_ID) {
global $post,$wpdb;
$tablename="wp_cart66_products";
if($post->post_type == "post" && strlen( get_post_meta($post_ID, 'price', true))>0 ){
$id = $wpdb->get_var("SELECT id FROM ".$tablename." WHERE id=".$post_ID);
$data=array(
'id'=>$post_ID,
'item_number'=>get_post_meta($post->ID, 'scode', true),
'name'=>$post->post_title,
'price'=>get_post_meta($post->ID, 'price', true),
'options_2'=>get_post_meta($post->ID, 'variations', true),
'shipped'=>'1',
);
$where = array("id" => $post_ID);
// Possible format values: %s as string; %d as decimal number; and %f as float.
$format=array( '%d', '%s', '%s', '%s', '%s', '%d');
$where_format = array( '%d' );
if($id>0){
// update
$wpdb->update( $tablename,$data, $where, $format, $where_format);
}else{
// insert
$wpdb->insert( $tablename,$data,$format);
}
}
return $post_ID;
}
add_action('publish_post', 'do_my_stuff');


I give my products a price by entering a value in the custom meta field "price" in my Post editor screen.

Everything works great, but we are now working with 4 currencies and its becoming more time consuming to calculate the exchange rates manually each time and enter it in all 3 sites. We want it to be done automatically. And also have a conditional IF statement.

We have 3 sites, euro, gbp, usd.
For example the EURO site, at the moment we take a price in CNY (Chinese yuan), we go to google and do 10 CNY to EUR = 1.24, and we plus 3 euro for shipping and then we put 4.24 in the 'price' field, and publish the product. We repeat this process with USD, and GBP. By ourselves, we just add shipping depending on the CNY price as below:

<strong>3 Conditional statements</strong>
If the price is under 50 CNY, we only add 3 euro shipping.
If the price is between 50 and 200 CNY, we add 4 euro shipping.
If the price is 200+ CNY, we add 5 euro shipping.

So the main request is to modify our function to do this automatically, directly from the CNY price, without going to google etc.. The CNY price we put in the 'price' meta field (which is very quick to do) , instead of the calculated final EURO, USD, GBP price. How i would like it to be is as follows:

<strong>Step 1</strong>
For example, we take the CNY price 100, and we enter it in the 'price' field.
We publish the post as normal.

<strong>Step 2</strong>

That function takes the value of 'price', multiply by exchange rate + shipping amount bases on the conditional IF statement
(see IF it is under 50, between 50 and 200, or over 200)

So in case of EURO, 3 examples depending on the price:
49 * 8 + 3
100 * 8 + 4
250 * + 5

* Since we have 3 sites, we'll use the same function on all 3, but we just update the exchange rate depending on which site.

<strong>In short:</strong> We want to put the CNY price at the Post editor, click publish, and then have it replaced by the EURO/GBP/USD price.

<strong>Alternative route</strong>

I guess an alternative could be to keep the above function as it is, and write a new function that only updates the 'price' field based on the above rules BEFORE my function fires so the current function just adds that value to the db as a normal final price.

Answers (1)

2012-11-22

Arnav Joy answers:

<?php

function do_my_stuff($post_ID) {

global $post,$wpdb;

$tablename="wp_cart66_products";

if($post->post_type == "post" && strlen( get_post_meta($post_ID, 'price', true))>0 ){

$id = $wpdb->get_var("SELECT id FROM ".$tablename." WHERE id=".$post_ID);

$cny = get_post_meta($post->ID, 'price', true);

/*Shipping rate */

if( $cny < 50 )
$shipping = 3;
else if( $cny >= 50 && $cny < 200 )
$shipping = 4;
else if( $cny >= 200 )
$shipping = 5;

/*Exchange rate CNY to EURO */

$cny_to_euro = 0.124;

$euro = $cny * $cny_to_euro ;

$price = $euro + $shipping;

$price = number_format($price,2);

$data=array(

'id'=>$post_ID,

'item_number'=>get_post_meta($post->ID, 'scode', true),

'name'=>$post->post_title,

'price'=>$price,

'options_2'=>get_post_meta($post->ID, 'variations', true),

'shipped'=>'1',

);

$where = array("id" => $post_ID);

// Possible format values: %s as string; %d as decimal number; and %f as float.

$format=array( '%d', '%s', '%s', '%s', '%s', '%d');

$where_format = array( '%d' );

if($id>0){

// update

$wpdb->update( $tablename,$data, $where, $format, $where_format);

}else{

// insert

$wpdb->insert( $tablename,$data,$format);

}

}

return $post_ID;

}

add_action('publish_post', 'do_my_stuff');

?>


oomskaap comments:

The above code works perfectly! Thanks a lot.
I would like the custom meta 'price' to also reflect the new price once i Published, or Updated the post.


Arnav Joy comments:

try this

<?php



function do_my_stuff($post_ID) {



global $post,$wpdb;



$tablename="wp_cart66_products";



if($post->post_type == "post" && strlen( get_post_meta($post_ID, 'price', true))>0 ){



$id = $wpdb->get_var("SELECT id FROM ".$tablename." WHERE id=".$post_ID);



$cny = get_post_meta($post->ID, 'price', true);



/*Shipping rate */



if( $cny < 50 )

$shipping = 3;

else if( $cny >= 50 && $cny < 200 )

$shipping = 4;

else if( $cny >= 200 )

$shipping = 5;



/*Exchange rate CNY to EURO */



$cny_to_euro = 0.124;



$euro = $cny * $cny_to_euro ;



$price = $euro + $shipping;



$price = number_format($price,2);

update_post_meta($post->ID, 'price', $price);



$data=array(



'id'=>$post_ID,



'item_number'=>get_post_meta($post->ID, 'scode', true),



'name'=>$post->post_title,



'price'=>$price,



'options_2'=>get_post_meta($post->ID, 'variations', true),



'shipped'=>'1',



);



$where = array("id" => $post_ID);



// Possible format values: %s as string; %d as decimal number; and %f as float.



$format=array( '%d', '%s', '%s', '%s', '%s', '%d');



$where_format = array( '%d' );



if($id>0){



// update



$wpdb->update( $tablename,$data, $where, $format, $where_format);



}else{



// insert



$wpdb->insert( $tablename,$data,$format);



}



}



return $post_ID;



}



add_action('publish_post', 'do_my_stuff');



?>