Advertisement
nicolaslagios

bestprice variables

May 22nd, 2023
1,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.29 KB | None | 0 0
  1. <?php
  2. $absolute = '/home/bodypro/public_html/';
  3. require_once($absolute . 'wp-config.php');
  4. $connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  5. if (mysqli_connect_errno()) {
  6.     echo "Failed to connect to MySQL: " . mysqli_connect_error();
  7.     exit();
  8. }
  9.  
  10. $start_from = 0;
  11. $batch_size = 50;
  12.  
  13. $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  14. <webstore>
  15.    <date>[todays_date({Backorders})]</date>
  16.    <products>
  17.        <!-- BEGIN LOOP -->
  18.        <!-- END LOOP -->
  19.    </products>
  20. </webstore>";
  21.  
  22. $firstiteration = 50;
  23.  
  24. while (true) {
  25.     // Retrieve data from SQL query
  26.     $sql = "SELECT
  27.        t2.meta_value as mpn,
  28.        t1.ID as productId,
  29.        t1.post_title as name,
  30.        t1.guid as link,
  31.        (SELECT wp_posts.guid
  32.        FROM wp_posts
  33.        INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.meta_value
  34.        WHERE wp_posts.post_type = 'attachment'
  35.        AND wp_postmeta.meta_key = '_thumbnail_id'
  36.        AND wp_postmeta.post_id = t1.post_parent
  37.        ) AS img_url_1,
  38.        (SELECT TRIM(TRAILING '|' FROM IFNULL(REPLACE(GROUP_CONCAT(wp_postmeta.meta_value), ',', '|'), ''))
  39.            FROM wp_postmeta
  40.            WHERE wp_postmeta.post_id = t1.post_parent
  41.            AND wp_postmeta.meta_key LIKE '%\_gallery%'
  42.        ) AS additionalimage,
  43.        ( SELECT CONCAT_WS(' > ', wp_terms4.name, wp_terms3.name, wp_terms2.name, wp_terms1.name) AS breadcrumb
  44.            FROM wp_term_relationships
  45.            INNER JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id AND wp_term_taxonomy.taxonomy = 'product_cat'
  46.            INNER JOIN wp_terms wp_terms1 ON wp_terms1.term_id = wp_term_taxonomy.term_id
  47.            LEFT JOIN wp_term_taxonomy wp_term_taxonomy2 ON wp_term_taxonomy2.term_taxonomy_id = wp_term_taxonomy.parent
  48.            LEFT JOIN wp_terms wp_terms2 ON wp_terms2.term_id = wp_term_taxonomy2.term_id
  49.            LEFT JOIN wp_term_taxonomy wp_term_taxonomy3 ON wp_term_taxonomy3.term_taxonomy_id = wp_term_taxonomy2.parent
  50.            LEFT JOIN wp_terms wp_terms3 ON wp_terms3.term_id = wp_term_taxonomy3.term_id
  51.            LEFT JOIN wp_term_taxonomy wp_term_taxonomy4 ON wp_term_taxonomy4.term_taxonomy_id = wp_term_taxonomy3.parent
  52.            LEFT JOIN wp_terms wp_terms4 ON wp_terms4.term_id = wp_term_taxonomy4.term_id
  53.            WHERE wp_term_relationships.object_id = t1.post_parent
  54.            AND (wp_terms4.name IS NOT NULL OR wp_terms3.name IS NOT NULL OR wp_terms2.name IS NOT NULL OR wp_term_taxonomy.parent = 0)
  55.            ORDER BY wp_term_taxonomy4.term_taxonomy_id DESC, wp_term_taxonomy3.term_taxonomy_id DESC, wp_term_taxonomy2.term_taxonomy_id DESC, wp_term_taxonomy.term_taxonomy_id DESC
  56.            LIMIT 1
  57.        ) AS categoryPath,
  58.        t4.meta_value as price,
  59.        t3.post_content as description,
  60.        REPLACE(REPLACE(t5.meta_value, 'instock', 'Y'), 'outofstock', 'N') as instock,
  61.        REPLACE(REPLACE(t5.meta_value, 'instock', 'Παράδοση σε 3–9 ημέρες'), 'outofstock', 'Μη διαθέσιμο') as availability,
  62.        (SELECT CONCAT(
  63.            wp_postmeta2.meta_value,
  64.            ' x ',
  65.            wp_postmeta3.meta_value,
  66.            ' x ',
  67.            wp_postmeta1.meta_value
  68.            ) AS dimensions
  69.            FROM wp_postmeta wp_postmeta1
  70.            INNER JOIN wp_posts ON wp_postmeta1.post_id = wp_posts.ID
  71.            LEFT JOIN wp_postmeta wp_postmeta2 ON wp_postmeta2.post_id = wp_posts.ID AND wp_postmeta2.meta_key = '_length'
  72.            LEFT JOIN wp_postmeta wp_postmeta3 ON wp_postmeta3.post_id = wp_posts.ID AND wp_postmeta3.meta_key = '_width'
  73.            WHERE wp_postmeta1.meta_key = '_height'
  74.            AND wp_posts.ID = t1.ID
  75.        ) as size,
  76.        (SELECT wp_terms.name
  77.            FROM wp_terms
  78.            LEFT JOIN wp_wc_product_attributes_lookup ON wp_terms.term_id = wp_wc_product_attributes_lookup.term_id
  79.            WHERE wp_wc_product_attributes_lookup.taxonomy = 'pa_marka'
  80.            AND wp_wc_product_attributes_lookup.product_id = t1.post_parent
  81.        ) as manufacturer,
  82.        t6.meta_value as weight,
  83.        (SELECT wp_terms.name
  84.            FROM wp_terms
  85.            LEFT JOIN wp_wc_product_attributes_lookup ON wp_terms.term_id = wp_wc_product_attributes_lookup.term_id
  86.            WHERE wp_wc_product_attributes_lookup.taxonomy = 'pa_color'
  87.            AND wp_wc_product_attributes_lookup.product_id = t1.post_parent
  88.        ) as color
  89.        FROM
  90.            wp_posts as t1
  91.            INNER JOIN wp_postmeta as t2 ON t1.post_parent = t2.post_id AND t2.meta_key = '_sku'
  92.            INNER JOIN wp_postmeta as t4 ON t1.post_parent = t4.post_id AND t4.meta_key = '_price'
  93.            INNER JOIN wp_postmeta as t5 ON t1.post_parent = t5.post_id AND t5.meta_key = '_stock_status'
  94.            INNER JOIN wp_postmeta as t6 ON t1.post_parent = t6.post_id AND t6.meta_key = '_weight'
  95.            INNER JOIN wp_posts as t3 ON t1.post_parent = t3.ID
  96.        WHERE
  97.            t1.post_type = 'product_variation'
  98.            AND t1.post_status = 'publish'
  99.            AND t1.post_parent NOT IN (
  100.                SELECT tr.object_id
  101.                FROM wp_term_relationships AS tr
  102.                INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
  103.                WHERE tt.taxonomy = 'product_cat' AND tr.term_taxonomy_id IN (3949, 6117, 6140, 6098, 6078, 6137, 6113, 6128, 6152, 6074, 6122, 3943, 3757, 4982, 3907, 3886, 3732, 3768, 6078, 3733, 6137, 3812, 3868, 6113, 3890, 684, 6128, 3741, 6152, 6074, 6122, 3755, 1259, 3912)
  104.            )
  105.            LIMIT $start_from, $batch_size";
  106.    
  107.     $result = mysqli_query($connection, $sql);
  108.  
  109.     if (!$result) {
  110.         echo "Error: " . mysqli_error($connection);
  111.         exit();
  112.     }
  113.  
  114.     $products = '';
  115.     while ($row = mysqli_fetch_assoc($result)) {
  116.         $product = "<product>
  117.                        <mpn><![CDATA[" . $row['mpn'] . "]]></mpn>
  118.                        <productId><![CDATA[" . $row['productId'] . "]]></productId>
  119.                        <name><![CDATA[" . $row['name'] . "]]></name>
  120.                        <link><![CDATA[" . $row['link'] . "]]></link>
  121.                        <img_url_1><![CDATA[" . $row['img_url_1'] . "]]></img_url_1>
  122.                        "./*<additionalimage><![CDATA[" . str_replace('|', ']]></additionalimage><additionalimage><![CDATA[', $row['additionalimage']) . "]]></additionalimage>*/"
  123.                        <categoryPath><![CDATA[" . $row['categoryPath'] . "]]></categoryPath>
  124.                        <price><![CDATA[" . $row['price'] . "]]></price>
  125.                        <description><![CDATA[" . $row['description'] . "]]></description>
  126.                        <instock><![CDATA[" . $row['instock'] . "]]></instock>
  127.                        <availability><![CDATA[" . $row['availability'] . "]]></availability>
  128.                        <size><![CDATA[" . $row['size'] . "]]></size>
  129.                        <manufacturer><![CDATA[" . $row['manufacturer'] . "]]></manufacturer>
  130.                        <weight><![CDATA[" . $row['weight'] . " kg]]></weight>
  131.                        <color><![CDATA[" . $row['color'] . "]]></color>
  132.                        <features>
  133.                            <brand><![CDATA[" . $row['manufacturer'] . "]]></brand>
  134.                            <color><![CDATA[" . $row['color'] . "]]></color>
  135.                            <material></material>
  136.                        </features>
  137.                    </product>";
  138.                    
  139.         $products .= $product;
  140.     }
  141.  
  142.     // Replace the placeholder with the product elements
  143.     //$xml = str_replace('<!-- END LOOP -->', $products, $xml);
  144.     $xml = str_replace('<!-- END LOOP -->', $products . PHP_EOL . '        <!-- END LOOP -->', $xml);
  145.  
  146.     // Save the modified XML to the file
  147.     file_put_contents($absolute . 'API/bestprice/variables.xml', $xml);
  148.  
  149.     $start_from += $batch_size;
  150.  
  151.     if ($result->num_rows < $batch_size) {
  152.         break;
  153.     }
  154.     $output = "Totally added " . $firstiteration . " child products to xml" . PHP_EOL;
  155.     fwrite(STDOUT, $output);
  156.     $firstiteration += 50;
  157. }
  158.  
  159. mysqli_close($connection);
  160.  
  161. copy($absolute . 'API/bestprice/variables.xml', $absolute . 'API/bestprice/bestprice2.xml');
  162. unlink($absolute . 'API/bestprice/variables.xml');
  163.  
  164. echo "done!!!";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement