I have two ecommerce websites. They sell the same products at the same prices. One is WordPress (woocommerce). The other has a .NET backend and a MSSQL database. I need to import the stock level from the other database into the Wordpress database.

Edit: I have the beginnings of a solution. I am able to automate the creation of a CSV file that will list stock levels. Then I need to set up a WordPress Cron job to read the CSV file every hour and update the database.

So far i have written this in my child theme's functions.php file:

// Scheduled Action Hook
function updatedb( ) {
$myfile = fopen("somefilename.csv", "r") or die("Unable to open file!");
$csvArray = array_map('str_getcsv', file($myfile));
global $wpdb;

foreach($csvArray as $csvEntry) {
$value = $csvEntry[2];
$field = $csvEntry[1];
$sku = $csvEntry[0];
        array(  meta_value = $value,
        array(  meta_key = $field AND
                       post_id = (select post_id
                                              from (select
                                                             from ppwp_postmeta
                                                             where meta_key = '_sku') as sku_post
                                                             where meta_value = $sku);

// Schedule Cron Job Event
function update_stock_cron_job() {
if ( ! wp_next_scheduled( ‘updatedb’ ) ) {
wp_schedule_event( time(), ‘hourly’, ‘updatedb’ );
add_action( ‘wp’, ‘update_stock_cron_job’ );

My knowledge of both PHP and WordPress is pretty limited... I'm not sure what logic should go into the updatedb to actually read the CSV and update the database. If anyone has a rough idea, that would be really useful!

  • If they are using different platforms (which it appears they are), you can't just connect the databases to each other. Converting the queries/tables on the fly will probably be expensive or even impossible. You might want to replace one of the platforms with the other, so they both run on the same platform. – Jack Johansson Jul 27 '17 at 13:45
  • We are not in a position to simply tear down and rebuild either website. The situation is what it is. We have one database reliably reflecting stock levels and it isn't the wordpress one. I absolutely need to get that data somehow :s – o-t-w Jul 27 '17 at 22:50
  • 1
    You can hire someone to do this for you. Unfortunately, converting a database to another one is a complicated task. – Jack Johansson Jul 27 '17 at 23:13
  • You could update the wp database and then use the REST API to update the other database. – gdaniel Aug 9 '17 at 16:04

Your Answer

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

Browse other questions tagged or ask your own question.