Post by itrends » Tue May 29, 2012 11:00 pm

Hi all,

anyone aware of a mod / extension / way that I could get opencart to automatically check a CSV or similar on a daily basis, check through a stock column and update opencart as a result?

The file I want to check is as follows:
File Url: http://www.domain.com/file-that-you-want-to-get.xml
Root (Products), Node (Product), Element names:
1) ProductID
(2) Name
(3) Xline "true or false, If true, will be discontinued when stocks run out"
(4) InStock "In Stock, Out of Stock or Discontinued"
(5) Stock "if Xline true will show remaining stock, otherwise will always show 99"

I use the same product ID on my 'model' in opencart as is used in the feed so I essentially want to
- grab the file
- match the IDs to my models
- check if the in stock status should be 'in stock' or 'out of stock'
- updated as needed

Any thoughts or suggestions on automating this are most certainly welcome :)
Last edited by itrends on Tue May 29, 2012 11:42 pm, edited 1 time in total.

Active Member

Posts

Joined
Tue Jul 14, 2009 7:54 pm

Post by itrends » Tue May 29, 2012 11:42 pm

For anyone that is interested, I managed to work this out. Here is my code :)

Code: Select all


$xml = file_get_contents("http://domain.com/file-that-you-want-to-get.xml"); // your file is in the string "$xml" now.
file_put_contents("/path_to_file/public_html/stock-today.xml", $xml); // now your xml file is saved.

sleep(200);

$con = mysql_connect("localhost","datebase_username","database_password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database_name", $con);


$url = "/path/to/file/public_html/stock-today.xml";
$xml = simplexml_load_file($url);

foreach($xml->Product as $Product)
{

mysql_query("UPDATE product SET quantity=$Product->Stock WHERE model=$Product->ProductID ");

}

mysql_close($con);
You'll see I put a sleep delay in there of 200 seconds to make sure the file was fully downloaded as it can be quite large.

Active Member

Posts

Joined
Tue Jul 14, 2009 7:54 pm

Post by burley » Sat Jun 02, 2012 9:24 pm

Wow!

Thats great!! Is it working om 1.5.1.3?

User avatar
Active Member

Posts

Joined
Sun Oct 09, 2011 3:30 pm

Post by itrends » Wed Jun 06, 2012 12:19 am

No reason why not - best bet is to backup your database and then test. :)

I'm not looking to suppor this or anything but it should be good for some time in the future as it is independent of any opencart files and only needs to touch one table in the database that I can't imagine would change now.

Active Member

Posts

Joined
Tue Jul 14, 2009 7:54 pm

Post by burley » Wed Jun 06, 2012 12:24 am

great! Thanks!

User avatar
Active Member

Posts

Joined
Sun Oct 09, 2011 3:30 pm

Post by itrends » Wed Jun 06, 2012 12:27 am

FYI - there may be settings with your host that might cause issues if it wont let you run the grab / download parts etc and you'll need to ensure your paths are correct etc.

Good luck :)

Active Member

Posts

Joined
Tue Jul 14, 2009 7:54 pm

Post by Helloguy » Tue Jun 12, 2012 4:52 am

Hi,
Thanks for an awesome script!

Would it be possible to also update the stock levels for product separate options? For example, there would be separate codes for Adidas black shirt and adidas white shirt (the product is the same, only the options quantity differ)
Anyone's help would be really appreaciated.

Newbie

Posts

Joined
Tue Jun 12, 2012 4:47 am

Post by burley » Fri Jun 22, 2012 10:20 pm

this would help me out as well, since that is what I really am looking for.

This is a start, but stock update within the options would be even better!

User avatar
Active Member

Posts

Joined
Sun Oct 09, 2011 3:30 pm

Post by faxe3 » Sun Sep 16, 2012 7:19 pm

Hi,

Just what I need! But what should I do about the code? Should I just save it in a PHP file, put it on my website and then run it from my browser?

Thanks,
Faxe3

Newbie

Posts

Joined
Thu Jun 07, 2012 4:09 pm

Post by robbas » Wed Oct 03, 2012 10:23 pm

Hi there,

Can someone help me with a few pointers where this should be placed and perhaps some usefell url's for understanding the ways to customize this? I like to get into this but my search keywords dont seem to give usefull results.

If there is someone with some of these tip or usefull links about these kinda imports, plz let me know!

Thx in Advance.

Online Media Development

http://www.vuuredesign.nl/


New member

Posts

Joined
Fri Jul 27, 2012 6:35 pm
Location - Amsterdam

Post by SXGuy » Thu Oct 04, 2012 5:08 am

Your code may work, but its scripted very badly, you shouldnt be added the mysql database details directly to the page, they should be pulled from pre existing variables already loaded.

Plus your databse calls should be handled seperately in a model file.

Active Member

Posts

Joined
Sun Nov 08, 2009 2:07 am

Post by eWarrior » Tue Oct 09, 2018 8:13 am

SXGuy wrote:
Thu Oct 04, 2012 5:08 am
Your code may work, but its scripted very badly, you shouldnt be added the mysql database details directly to the page, they should be pulled from pre existing variables already loaded.

Plus your databse calls should be handled seperately in a model file.
I realise this is an old post but I feel this should be addressed.

The database details are not visible to anyone, nor can they be accessed by anything other than the server. The server RUNS the PHP script to generate the desired outcome.

In regards to separating the database calls. You would like the OP to follow the full MVC architecture for a simple script?

Anyway, back on topic. I am also trying to get this to work but for some reason it just wont update the product quantity. No errors. I cleaned up the code a little to make it easier to read and is below (I changed the sleep to 10 seconds as I am currently testing with a small file). Can anyone identify the issue?

Code: Select all

<?php
 
$xml = file_get_contents("https://www.supplierwebsite.com.au/supplier-feed.xml");
file_put_contents("/home/project_folder/public_html/supplier-feed.xml", $xml);

sleep(10);

$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';
$db = 'database';

$con = mysql_connect($dbhost, $dbuser, $dbpass);

if (!$con) {
	die('Could not connect: ' . mysql_error());
}

mysql_select_db($db, $con);

$url = "https://www.mywebsite.com.au/supplier-feed.xml";
$xml = simplexml_load_file($url);

foreach($xml->product as $product) {
	mysql_query("UPDATE oc_product SET quantity = $product->PRODUCT_QUANTITY WHERE model=$product->MODEL");
}

mysql_close($con);

?>

New member

Posts

Joined
Wed Aug 29, 2012 4:27 pm

Post by straightlight » Tue Oct 09, 2018 8:58 am

A ... really untrustworthy code ... :choke:

A few example on how to gather the data in a more solid way are demonstrated in the catalog/model/shipping folder. The RFC standards has increased their security measures to capture data from XML data since then. Besides, MySQL has been deprecated since ages as the MySQLi extension takes the lead.

At this point, if you are unsure on how to proceed, the best option would be to create a new service request in the Commercial Support section of the forum to get this done as a custom job.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by eWarrior » Tue Oct 09, 2018 9:21 am

straightlight wrote:
Tue Oct 09, 2018 8:58 am
A ... really untrustworthy code ... :choke:
Thank you. I used the code in the original post and updated accordingly. Please note that I NEVER use any new code on a live site unless I am absolutely certain it is safe and error free.

On that note, it would be beneficial to elaborate on your statement so we can obtain a deeper understanding of what aspects are considered unsafe.

straightlight wrote:
Tue Oct 09, 2018 8:58 am
At this point, if you are unsure on how to proceed, the best option would be to create a new service request in the Commercial Support section of the forum to get this done as a custom job.
You are most likely correct, but I do like to gain knowledge myself so I can make minor updates if necessary.

New member

Posts

Joined
Wed Aug 29, 2012 4:27 pm
Who is online

Users browsing this forum: No registered users and 129 guests