Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- session_start();
- require_once 'vendor/autoload.php';
- require_once 'utils.php';
- $clientBuilder = new ClientBuilder($params);
- $client = $clientBuilder->build();
- $_SESSION["php-oauth-client"] = array();
- // OwnServerDB MySQL Connection
- require('mysql_login.php');
- require('mysql_connection.php');
- require_once 'ownServerGetId.php';
- // WooCommerce Connection
- require 'wooApi/vendor/autoload.php';
- use Automattic\WooCommerce\Client;
- use Automattic\WooCommerce\HttpClient\HttpClientException;
- // Connection to WooCommerce
- $woocommerce = new Client(
- 'https://4-gamers-shop.com',
- [
- 'wp_api' => true,
- 'version' => 'wc/v3',
- 'query_string_auth' => true // Force Basic Authentication as query string true and using under HTTPS
- ]
- );
- // Get Products from LIEFERANT
- try {
- $getProductId = "";
- $countIndex = 0;
- $products = $client->getProducts(); // All Products as an object
- foreach ($products as $product) {
- echo "IndexIndex : ".$countIndex."\n";
- $getProductId = $product->getProductId();
- $sql = "SELECT productId FROM `product` WHERE productId = '".$getProductId."'";
- $result = $conn->query($sql);
- echo $getOfficialTitle = addslashes($product->getName());
- echo "\n";
- echo $getProductId."\n";
- if ($result->num_rows == 0) {
- echo "CREATE_ NEW PRODUCT IN TABLE > P R O D U C T < ======";
- // Date and Time
- $dateToday = date("d.m.Y");
- $timeNow = date("H:i:s");
- // Source : $product
- // $getOfficialTitle = addslashes($product->getName());
- $getStockQ = getStockQ($product);
- $getDefaultPrice = getDefaultPrice($product);
- $getLanguages = getLanguages($product); // ARR
- $getRegion = getRegion($product); // ARR
- $getPrice = getPrice($product); // ARR
- $getGamePlatform = getGamePlatform($product);
- $getGamePlatformId = getGamePlatformId($getGamePlatform);
- $getReleaseDate = getReleaseDate($product);
- //$getProductId = $product->getProductId();
- // Length of the ArrayOutput
- $getLanguagesLength = count($getLanguages);
- $getRegionLength = count($getRegion);
- $getPriceLength = count($getPrice);
- // INSERT PRODUCT
- $insertProduct = "INSERT INTO product (productId,officialTitle,stock,defaultPrice,developer,pegi,developerHomepage,price_1_9,price_10_99,price_100,releaseDate,platform,platformId,createDate,createTime)
- VALUES ('".$getProductId."', '".$getOfficialTitle."','".$getStockQ."','".$getDefaultPrice."',NULL,NULL,NULL,'".$getPrice[1]."','".$getPrice[0]."','".$getPrice[2]."','".$getReleaseDate."','".$getGamePlatform."','".$getGamePlatformId."','".$dateToday."','".$timeNow."')";
- if ($conn->query($insertProduct) === TRUE) {
- } else {
- echo "Error: " . $insertProduct . "<br>" . $conn->error;
- }
- sleep(2);
- //Insert Language
- for($i = 0; $i < $getLanguagesLength; $i++){
- $insertLanguage = "INSERT INTO language_product_ass (lp_id,languageId,productId) VALUES (NULL,'".getLanguageId($getLanguages[$i])."','".$getProductId."')";
- if ($conn->query($insertLanguage) === TRUE) {
- } else {
- echo "</br>Error: ". $conn->error."</br>";
- }
- }
- sleep(2);
- //Insert Region
- for($i = 0; $i < $getRegionLength; $i++){
- $insertRegion = "INSERT INTO region_product_ass (rp_id,regionId,productId) VALUES (NULL,'".getRegionId($getRegion[$i])."','".$getProductId."')";
- $getRegion[$i];
- if ($conn->query($insertRegion) === TRUE) {
- } else {
- echo "</br>Error: ". $conn->error."</br>";
- }
- }
- sleep(4);
- }
- //* After insert product to TABLE 'product'
- /* Check Shop_Product_Ass */
- $checkShopProductAss = "SELECT productId from shop_product_ass WHERE productId ='".$getProductId."'";
- $resultShopProductAss = $conn->query($checkShopProductAss);
- if ($resultShopProductAss->num_rows == 1) {
- //Already uploaded to Shop
- echo "ALREADY UPLOADED TO SHOP ! \n";
- echo $getProductId;
- }
- else{ //NOT include in Shop
- echo "NOT UPLOADED TO SHOP ! \n";
- echo $getProductId;
- // Check exists images, description
- $checkExistsImages = "SELECT * FROM images WHERE productId = '".$getProductId."'";
- $resultExistsImages = $conn->query($checkExistsImages);
- if ($resultExistsImages->num_rows == 0) { // Images not exists
- echo "Images don't exists for ProductId : ".$getProductId;
- echo "=>=>=>=>=>=>=>==>=>=>=>=>=>=>=>==>=>=>=>";
- echo "=>=>=>=>=>=>=>==>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>";
- // Open URL to import to Database
- echo "Try to open Index : ".$countIndex."\n";
- set_error_handler(
- function ($severity, $message, $file, $line) {
- throw new ErrorException($message, $severity, $severity, $file, $line);
- }
- );
- $open_url = ''; // URL aufrufen um restliche Productdetails vom Lieferanten aus der Datenbank zu holen
- $open_url = $open_url.$countIndex;
- try {
- file_get_contents($open_url);
- }
- catch (Exception $e) {
- echo $e->getMessage();
- }
- restore_error_handler();
- }
- else{ // Images exists
- }
- // Download images if in database
- $resultExistsImages = $conn->query($checkExistsImages);
- if ($resultExistsImages->num_rows > 0) {
- $selectGaleryImages = "SELECT * FROM images WHERE productId = '".$getProductId."'";
- $resultSelectGaleryImages = $conn->query($selectGaleryImages);
- if ($resultSelectGaleryImages->num_rows > 0) {
- echo "Image exists in Database => ";
- echo $row['imgUrl']."\n";
- $s1 = "../../../var/www/html/importProductsAuto/images/".$getProductId."/";
- if (!file_exists($s1)) {
- mkdir($s1, 0777, true);
- }
- $imagesCount = 0;
- while($row = $resultSelectGaleryImages->fetch_assoc()) {
- if($row['type'] == "EBAY_GALLERY" || $row['type'] == "SCREEN_SHOT_LARGE" || $row['type'] == "SCREEN_SHOT_THUMB"){
- $temp_name = "gallery";
- $imagesCount++;
- }
- else{
- $temp_name = "cover";
- $imagesCount++;
- }
- $fileName = $temp_name.$imagesCount.".jpg";
- $imageUrl = $row['imgUrl'];
- $openLink = $s1.$fileName;
- echo "Try to Download Image =>=>=>=>=>=>=>\n";
- $ch = curl_init($imageUrl);
- $fp = fopen($openLink, "wb");
- curl_setopt($ch, CURLOPT_FILE, $fp);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_exec($ch);
- curl_close($ch);
- //checkfilesize
- if(filesize($openLink) < 300){
- echo "Datei zu klein \n";
- unlink($openLink);
- echo "Datei geloescht : ".$openLink."\n";
- //Delete File
- }
- }
- // After download images
- /* COMPARE IMAGE SIZE */
- echo ">>>>>>> START COMPARE IMAGES <<<<<<";
- $filesArr = array();
- $folder = $s1;
- $fileType = '.jpg';
- $verzeichnis = $folder;
- if ( is_dir ( $verzeichnis )){
- // öffnen des Verzeichnisses
- if ( $handle = opendir($verzeichnis) ){
- $x = 0;
- while (($file = readdir($handle)) !== false){
- if($file != "." and $file != ".."){
- $filesArr[$x][0] = $file;
- $filesArr[$x][1] = filesize($folder.$file)."\n";
- $x++;
- }
- }
- }
- }
- closedir($handle);
- $filesArrSize1d = count($filesArr);
- $toDeleteImages = array();
- $t = 0;
- for($lx = 0;$lx < $filesArrSize1d;$lx++){
- //echo "$filesArr[$lx][0]."\n"";
- for($sx = $filesArrSize1d-1;$sx > 0;$sx--){
- // echo "Vergleich ".$filesArr[$lx][0]." mit ".$filesArr[$sx][0]."\n";
- if($filesArr[$lx][1] == $filesArr[$sx][1]){
- if($lx != $sx){
- if($filesArr[$lx][0] == "cover1.jpg"){
- $toDeleteImagesLength = count($toDeleteImages);
- $rm = 0;
- for($rt = 0;$rt < $toDeleteImagesLength;$rt++){
- if($toDeleteImages[$rt] == $filesArr[$sx][0]){
- $rm = 1; // file exists in toDeleteImages ARR
- }
- }
- if($rm == 0){
- $toDeleteImages[$t] = $filesArr[$sx][0];
- // echo $folder.$filesArr[$lx][0]." ist identisch mit ".$folder.$filesArr[$sx][0]."\n";
- $t++;
- }
- }
- else if($filesArr[$lx][0] != "cover1.jpg"){
- if($filesArr[$lx][0] != $filesArr[$sx][0]){
- $toDeleteImagesLength = count($toDeleteImages);
- $rm = 0;
- for($rt = 0;$rt < $toDeleteImagesLength;$rt++){
- if($toDeleteImages[$rt] == $filesArr[$lx][0] or $toDeleteImages[$rt] == $filesArr[$sx][0]){
- $rm = 1;
- }
- }
- if($rm == 0){
- $toDeleteImages[$t] = $filesArr[$sx][0];
- echo $folder.$filesArr[$lx][0]." ist identisch mit ".$folder.$filesArr[$sx][0]."\n";
- $t++;
- }
- }
- }
- }
- }
- }
- }
- $toDeleteImagesLength2 = count($toDeleteImages);
- for($ab = 0;$ab < $toDeleteImagesLength2;$ab++){
- echo $getProductId."\n";
- echo $toDeleteImages[$ab]."\n";
- unlink('../../../var/www/html/importProductsAuto/images/'.$getProductId.'/'.$toDeleteImages[$ab]);
- }
- /* COMPARE IMAGE SIZE END */
- /* COMPARE COVER IMAGE SIZE */
- $coverFilesArr = array();
- $folder = $s1;
- $fileType = '.jpg';
- $verzeichnis = $folder;
- if ( is_dir ( $verzeichnis )){
- // öffnen des Verzeichnisses
- if ( $handle = opendir($verzeichnis) ){
- $x = 0;
- while (($file = readdir($handle)) !== false){
- if($file != "." and $file != ".."){
- if(strpos($file,"cover")!==false){
- $coverFilesArr[$x][0] = $file;
- $coverFilesArr[$x][1] = filesize($folder.$file);
- $x++;
- }
- }
- }
- }
- closedir($handle);
- echo "LENGTH OF COVER ARRAY : ";
- echo $coverFilesArrLength = count($coverFilesArr);
- echo "\n";
- $dontDeleteImage = "";
- $maxSizeCover = 0;
- for($i = 0;$i < $coverFilesArrLength;$i++){
- if($maxSizeCover < $coverFilesArr[$i][1]){
- $dontDeleteImage = $coverFilesArr[$i][0];
- $maxSizeCover = $coverFilesArr[$i][1];
- }
- }
- //delete all other cover
- for($i = 0;$i < $coverFilesArrLength;$i++){
- if($coverFilesArr[$i][0] != $dontDeleteImage){
- unlink('../../../var/www/html/importProductsAuto/images/'.$getProductId.'/'.$coverFilesArr[$i][0]);
- echo "SELECTED : ".$dontDeleteImage."\n";
- echo "DELETES ".$coverFilesArr[$i][0]."\n";
- }
- }
- }
- /* COMPARE COVER IMAGE SIZE END */
- } // ImageExists
- }
- // CHECK : Upload to Shop ?
- $uploadToShop = 0; // $uploadToShop = 0 => Don't upload to Shop ; $uploadToShop = 1 => Upload to Shop
- $selectProductFromOwn = "SELECT * FROM product WHERE productId = '".$getProductId."'";
- $resultSelectProductFromOwn = $conn->query($selectProductFromOwn);
- $productArr = array();
- while($row = $resultSelectProductFromOwn->fetch_assoc()) {
- $productArr[0] = $row['productId'];
- $productArr[1] = $row['officialTitle'];
- $productArr[2] = $row['price_100'] * 1.4;
- if($row['platformId'] == 1 || $row['platformId'] == 5 || $row['platformId'] == 6 || $row['platformId'] == 7 || $row['platformId'] == 9 || $row['platformId'] == 10 || $row['platformId'] == 13){
- if($row['stock'] >= 3){
- $directory = "../../../var/www/html/importProductsAuto/images/".$getProductId."/";
- $countFiles = 0;
- if(is_dir($directory)){
- if ( $handle = opendir($directory) ){
- while (($file = readdir($handle)) !== false){
- $countFiles++;
- }
- }
- if($countFiles > 2){
- $uploadToShop = 1;
- }
- }
- }
- }
- }
- echo "§§§§§§§§§ Upload to Shop : ".$uploadToShop;
- // CHECK : UPLOAD TO SHOP ? END
- if($uploadToShop == 1){
- // Insert to Shop_Product_Ass
- $insertToShopProductAss = "INSERT INTO
- shop_product_ass (sz_id,productId,coverImageUrl,productTitle,shopId,sellprice,priceCalculation,available,shopDescription,shortShopDescription,importDateOwn,importTimeOwn,importDateBillbee,importTimeBillbee,online)
- VALUES
- (NULL,'".$productArr[0]."',NULL,'".$productArr[1]."',5,'".$productArr[2]."',1,1,NULL,NULL,NULL,NULL,NULL,NULL,1)";
- if ($conn->query($insertToShopProductAss) === TRUE) {
- echo "\n New record created successfully \n";
- } else {
- //echo "Error: " . $sql . "<br>" . $conn->error;
- }
- // Import to Woo Shop
- $productDataArray = array();
- $selectProductData = "SELECT product.productId, product.officialTitle, product.pegi, platform.platform, region.region, product.stock, product.price_100 , default_description.dDescription FROM product INNER JOIN platform ON product.platformId = platform.platformId INNER JOIN region_product_ass ON product.productId = region_product_ass.productId INNER JOIN region ON region_product_ass.regionId = region.regionId INNER JOIN default_description ON product.productId = default_description.productId WHERE product.productId = '".$getProductId."' AND default_description.ter_id = 4 LIMIT 1";
- $resultSelectProductData = $conn->query($selectProductData);
- if($resultSelectProductData->num_rows > 0){
- while($row = $resultSelectProductData->fetch_assoc()){
- $productDataArray[0] = $row['productId'];
- $productDataArray[1] = $row['officialTitle'];
- $productDataArray[2] = $row['pegi'];
- $productDataArray[3] = $row['platform'];
- $productDataArray[4] = $row['region'];
- $productDataArray[5] = $row['stock'];
- $productDataArray[6] = $row['price_100'] * 1.4;
- $productDataArray[6] = number_format($productDataArray[6] , 0, '', '' );
- $productDataArray[7] = $row['dDescription'];
- }
- $categoryArray = array();
- $selectCategorys = "SELECT * FROM category_product_ass INNER JOIN category ON category_product_ass.catId = category.catId WHERE category_product_ass.productId = '".$getProductId."'";
- $resultSelectCategorys = $conn->query($selectCategorys);
- if($resultSelectCategorys->num_rows > 0){
- $x = 0;
- while($row = $resultSelectCategorys->fetch_assoc()){
- $categoryArray[$x]['id'] = $row['catId_woo'];
- $x++;
- }
- }
- $languagesArray = array();
- $selectLanguages = "SELECT * FROM language_product_ass INNER JOIN language ON language_product_ass.languageId = language.languageId WHERE language_product_ass.productId = '".$getProductId."'";
- $resultSelectLanguages = $conn->query($selectLanguages);
- if($resultSelectLanguages->num_rows > 0){
- $x = 0;
- while($row = $resultSelectLanguages->fetch_assoc()){
- $languagesArray[$x] = $row['shortLang'];
- $x++;
- }
- }
- $coverFileName = "";
- $galleryArray = array();
- $directory = "../../../var/www/html/importProductsAuto/images/".$getProductId."/";
- //get cover filename
- if ( is_dir ( $directory )){
- // open directory
- if ( $handle = opendir($directory) ){
- while (($file = readdir($handle)) !== false){
- if($file != "." and $file != ".."){
- if(strpos($file,"cover")!==false){
- $coverFileName = $file;
- }
- }
- }
- }
- }
- closedir($handle);
- if(empty($coverFileName)){
- if ( is_dir ( $directory )){
- // open directory
- if ( $handle = opendir($directory) ){
- while (($file = readdir($handle)) !== false){
- if($file != "." and $file != ".."){
- $coverFileName = $file;
- }
- }
- }
- }
- closedir($handle);
- }
- // get cover filename END
- // get gallery images
- if ( is_dir ( $directory )){
- // open directory
- if ( $handle = opendir($directory) ){
- $x = 0;
- while (($file = readdir($handle)) !== false){
- if($file != "." && $file != ".." && $file != $coverFileName){
- $galleryArray[$x] = $file;
- $x++;
- }
- }
- }
- }
- closedir($handle);
- // END OF get gallery images
- ################# CREATE Images Array FOR WOOCOMMERCE #####################
- $wooImagesArray = array();
- $countGalleryArray = count($galleryArray);
- $x = 0;
- if(!empty($coverFileName)){
- $wooImagesArray[$x]['src'] = "SERVERURL/importProductsAuto/images/".$getProductId."/".$coverFileName;
- $x++;
- }
- for($i = 0;$i < $countGalleryArray;$i++){
- $wooImagesArray[$x]['src'] = "SERVERURL/importProductsAuto/images/".$getProductId."/".$galleryArray[$i];
- $x++;
- }
- ############################################################################
- ################# CREATE Attributes Array FOR WOOCOMMERCE ##################
- $attributesArray = array();
- ### => Languages
- $countLanguagesArray = count($languagesArray);
- $languagesString = "";
- for($i = 0;$i < $countLanguagesArray;$i++){
- if($i == 0){
- $languagesString = $languagesArray[$i];
- }
- else{
- $languagesString = $languagesString.",".$languagesArray[$i];
- }
- }
- $attributesArray[0]['id'] = 5;
- $attributesArray[0]['options'] = $languagesString;
- //$attributesArray[0]['options'] = "de,en";
- ### => Platform
- $attributesArray[1]['id'] = 3;
- $attributesArray[1]['options'] = $productDataArray[3];
- //$attributesArray[1]['options'] = "Steam";
- ### => Region
- $attributesArray[2]['id'] = 4;
- $attributesArray[2]['options'] = $productDataArray[4];
- //$attributesArray[2]['options'] = "WORLDWIDE";
- ### => Pegi
- $attributesArray[3]['id'] = 8;
- $attributesArray[3]['options'] = $productDataArray[2];
- //$attributesArray[3]['options'] = 18;
- ############################################################################
- ################# BUILD DATA ARRAY FOR WOOCOMMERCE #########################
- try{
- echo "\n START UPLOAD : ";
- echo $sku = $productDataArray[0];
- echo "\n";
- $title = $productDataArray[1];
- $price = $productDataArray[6];
- $description = $productDataArray[7];
- $stock = $productDataArray[5];
- $data = [
- 'sku' => $sku,
- 'status' => 'publish',
- 'catalog_visibility' => 'visible',
- 'name' => $title,
- 'type' => 'simple',
- 'regular_price' => $price,
- 'short_description' => '',
- 'manage_stock' => true,
- 'stock_quantity' => $stock,
- 'categories' => $categoryArray,
- 'description' => $description,
- 'images' => $wooImagesArray,
- 'attributes' => $attributesArray
- ];
- //sleep(5);
- //echo "\n \n \n \n \n \n \n \n \n \n \n \n";
- //print_r($data);
- //echo "\n \n \n \n \n \n \n \n \n \n \n \n";
- $woocommerce->post('products', $data);
- sleep(5);
- } // Try END
- catch (HttpClientException $e) {
- echo '<pre><code>' . print_r( $e->getMessage(), true ) . '</code><pre>'; // Error message.
- echo '<pre><code>' . print_r( $e->getRequest(), true ) . '</code><pre>'; // Last request data.
- echo '<pre><code>' . print_r( $e->getResponse(), true ) . '</code><pre>'; // Last response data.
- }
- ############################################################################
- }
- }
- } //NOT include in Shop END
- echo "\n ~~~~~~~~~~~~~~~ N E X T ~~~ P R O D U C T ~~~~~~~~~~~~~~~~~~~~";
- $countIndex++;
- }
- }
- catch (\\Resource\ResourceError $e) {
- if($e->isInvalidToken()) {
- echo "if you are using SessionStorage refresh your session and try one more time.";
- }
- echo $e->getCode();
- echo $e->getErrorCode();
- echo $e->getMoreInfo();
- echo $e->getDeveloperMessage();
- echo $e->getMessage();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement