PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 10, 2022

[FIXED] When products are being updated using a script function (by a cron), is there any way to identify or trace the function that editted the script?

 May 10, 2022     cron, cron-task, custom-wordpress-pages, product, wordpress     No comments   

Issue

the goal of this question is to find anything, by which it would be possible to identify an ID or anything, by which it would be possible to track a function (preferably by its name / slug) that was editting or updating prices for all products (lets say by a factor of 1,25), that were imported to the store automatically by cron).

Each time the fuction updates a product, it could for example store someplace a value that this given function editted the product for the last time.

So far I have searched for some options, the ones that I found were the following:

1) get_the_author_meta("display_name"); 2) get_the_modified_author();

However, none of these options are useful, or I have not managed to make them echo anything at all.

The expected results are:

1) To find a way how to track a function updating posts (products) by leaving a trace or ID, that would document that given function that performed the last edit.

2) To find a way how to identify the newly created traces per each post and be able to use it with other fuctions.

The ultimate goal is: To make sure that when a function goes through lets say 4000 products and the fuction does not finish due to some external cause - it does not make another pass from the beginning, but changes only the products it did not change yet.

If the process does not finish, the funtion is being restarted by a cron from the beginning (until it finihes) - which at the moment causes problems - because the already updated product prices by factor 1,25 are being updated over again by the same factor.

Would anyone have any ideas?

Code:

private function set_custom_price($product) {
    if (!$product->exists()) {
        return false;
    }

    $product_price = $product->get_regular_price();

    if (($product_price > 0) && ($product_price < 401)) {
        $product->set_regular_price(ceil(($product_price + ($product_price*(0.5)))/10) *10-1);
    } elseif (($product_price > 400) && ($product_price < 801)) {
        $product->set_regular_price(ceil(($product_price + 120)/10) *10-1);
    } elseif (($product_price > 800) && ($product_price < 1101)) {
        $product->set_regular_price(ceil(($product_price + 150)/10) *10-1);
    } elseif (($product_price > 1100) && ($product_price < 1601)) {
        $product->set_regular_price(ceil(($product_price + 200)/10) *10-1);
    } elseif (($product_price > 1600) && ($product_price < 2001)) {
        $product->set_regular_price(ceil(($product_price + 220)/10) *10-1);
    } elseif (($product_price > 2000) && ($product_price < 5001)) {
        $product->set_regular_price(ceil(($product_price + ($product_price*(0.15)))/10) *10-1);
    }

    $product->save();
    // HERE SOME CODE NEEDED TO REGISTER TRACE??
}

Solution

You could possibly use something like this:

$product_id = $product->ID;
$last_changed = get_post_meta( $product_id, 'custom_product_last_change', true );
$last_changed_timestamp = strtotime($last_changed);
$current_datetime = strtotime(date('m/d/Y h:i:s a', time()));

// Check if change date is in last 24 hours ( you can change that to what ever you want, last week, last month..etc )
if($last_changed_timestamp > $current_datetime - 86400){

    update_post_meta( $product_id, 'custom_product_last_change', date('m/d/Y h:i:s a', time()) );
    // Do the changes here
}


Answered By - Ali_k
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing