Reputation: 168
I'm aiming to create a variable in the following code, so that every time a purchase is made, it keeps adding a certain value to the given variable. Here's the code:
<?php
$osszegyultpenz=0;
$i=0;
$args = array(
'numberposts' => -1,
'post_status' => array_keys(wc_get_order_statuses()), 'post_status' => array('Processing,Completed')
);
$orders = wc_get_orders( $args );
foreach ( $orders as $order ) {
$items = $order->get_items();
foreach ( $items as $item ) {
if ($item["pa_tamogatott-partner"]=="palackaradat"){
if ((int)$item["subtotal"]% 3810==0){
$i = (int)$item["subtotal"]/3810;
$i=$i*1905;
$osszegyultpenz=$osszegyultpenz+$i;
}
if ((int)$item["subtotal"]% 5080==0){
$i = (int)$item["subtotal"]/5080;
$i=$i*2540;
$osszegyultpenz=$osszegyultpenz+$i;
}
if ((int)$item["subtotal"]% 6350==0){
$i = (int)$item["subtotal"]/6350;
$i=$i*3175;
$osszegyultpenz=$osszegyultpenz+$i;
}
}
}
}
echo "<p class=osszeszazalek>" . $osszegyultpenz/10000 . '%' . " </p>";
Thing is, the code works, but if it's set up this way, the value is always reset because of $osszegyultpenz and $i. How would you change this so that $osszegyultpenz is only initially 0, and after this script is ran, it saves it's value at the end of the script, and adds it to it the next time a purchase is made?
Any help is appreciated!
Upvotes: 0
Views: 42
Reputation: 386
What you can do is you can set an option using add_option
which will store a value to the database which can be fetched using get_option
and updated using update_option
so the problem with variables getting reset will be solved and you can make the flow like
$my_custom_varibale=get_option('any_name_you_want');
$changed_value=$my_custom_variable+$change_the_value;
update_option('any_name_you_want',$changed_value);
Upvotes: 3