Skip to content

Instantly share code, notes, and snippets.

@ovonel86
Created July 16, 2013 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ovonel86/766d674a03c68959bdfd to your computer and use it in GitHub Desktop.
Save ovonel86/766d674a03c68959bdfd to your computer and use it in GitHub Desktop.
Woocommerce Stock updater
<?php
if(isset($_GET['stanje']))
{
/****************************************
WP config path
*****************************************/
include '../wp-config.php';
/****************************************
*****************************************/
/*************************************************
DB connection
**************************************************/
$jp_connect = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$jp_mysql = @mysql_select_db(DB_NAME);
/*************************************************
**************************************************/
if(!$jp_connect or !$jp_mysql)
{
die("ERROR!");
}
else
{
$jp_url = "http://www.shoppingira.com/feature/export/stock.php"; // Link od eksternog fajla sa zalihama
$jp_user = "user"; // Username za ovjeru
$jp_pass = "pass"; // Password za ovjeru
$curl = curl_init($jp_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, $jp_user.":".$jp_pass);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Sample Code');
$jp_response = curl_exec($curl);
$jp_resultStatus = curl_getinfo($curl);
if($jp_resultStatus['http_code'] == 200) {
require_once 'inc/simple_html_dom.php';
$jp_html = str_get_html($jp_response);
// Get ID and stock
foreach($jp_html->find('izdelek') as $jp_element)
{
$jp_product['id'] = $jp_element->find('izdelekid', 0)->plaintext; // product id
$jp_product['stock'] = $jp_element->find('dobava', 0)->plaintext; // stock
$id = $jp_product['id'];
$stock = $jp_product['stock'];
$sql = "SELECT * FROM ".$table_prefix."postmeta WHERE meta_value = '$id'";
$que = mysql_query($sql);
while($fet=@mysql_fetch_array($que))
{
$izdelekID = $fet['meta_value'];
// Show us post_id if meta_value == $izdelekID
$sql = "SELECT post_id FROM ".$table_prefix."postmeta WHERE meta_key = 'izdelekID' AND meta_value = '$izdelekID'";
$que = mysql_query($sql);
$fet = mysql_fetch_array($que);
$post_id = $fet['post_id'];
// Find products and update stock
if($izdelekID == $id)
{
switch ($stock) {
case $stock=="na zalogi":
$stock = "instock";
break;
case $stock=="iz zaloge":
$stock = "outofstock";
break;
}
$sql = "UPDATE ".$table_prefix."postmeta SET meta_value = '$stock' WHERE meta_key = '_stock_status' AND post_id = '$post_id'";
$que = mysql_query ($sql);
if(!$que)
{
die(mysql_error());
}
}
}
}
} else {
echo 'Call Failed '.print_r($jp_resultStatus);
}
}
}
else
{
header("location: index.php");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment