Экспорт из hostcms в wp + woocommerce

#
Экспорт из hostcms в wp + woocommerce
После долгих попыток понять как пользоваться етим продуктом мною было принято решение отступиться
был написан скрипт експорта товаров ВП+ воокомерсе

ФАК
1 перенести ряд табличек от хостцмс в БД Вордпреса
2 залогиниться в админку ВП
3 создать в корне ВП файл вида ssssss.php
4 при желании увеличить отработку по времени более 30 секунд
5 модифицировать код експорта если нужно
6 перенести все картинки товаров хостцмс в отдельную папук



*********************************************************
<?php
/**
* WordPress Cron Implementation for hosts, which do not offer CRON or for which
* the user has not set up a CRON job pointing to this file.
*
* The HTTP request to this file will not slow down the visitor who happens to
* visit when the cron job is needed to run.
*
* @package WordPress
*/

ignore_user_abort(true);

/**
* Tell WordPress we are doing the CRON task.
*
* @var bool
*/
define('DOING_CRON', true);

if (!defined('ABSPATH')) {
    /** Set up WordPress environment */
    require_once (dirname(__file__) . '/wp-load.php');
}

// удаляем категории
global $wpdb;
WC()->api->includes();
WC()->api->register_resources(new WC_API_Server('/'));

$temp = WC()->api->WC_API_Products->get_product(1717);
//print_r($temp); die;
$temp = WC()->api->WC_API_Products->get_product_categories();
//$temp=get_product_categories();

if (count($temp['product_categories'])) {
    foreach ($temp['product_categories'] as $tmp) {
   //     wp_delete_term($tmp['id'], 'product_cat');

    }

}

// удаляем категории конец

// удаляем продукты
/*
$ii = WC()->api->WC_API_Products->get_products_count();

while ($ii['count']) {

    $temp = WC()->api->WC_API_Products->get_products();
    //print_r($temp);
    foreach ($temp['products'] as $tmp) {
        wp_delete_attachment($tmp['id']);
        $tt = wp_delete_post($tmp['id']);

    }
    $ii = WC()->api->WC_API_Products->get_products_count();
}

// удаляем продукты конец
*/
$sql = "SELECT * FROM shop_groups
WHERE shop_id=1 AND active =1 AND parent_id=0 AND deleted=0  
";
$category0 = $wpdb->get_results($sql);

$categoryAll = array();

foreach ($category0 as $tmp0) {

    $sql = "SELECT * FROM shop_groups
WHERE shop_id=1 AND active =1 AND deleted=0   AND parent_id=" . $tmp0->id;

    $category_parent = $wpdb->get_results($sql);

    global $_POST;
    $_POST = array();
    $_POST['taxonomy'] = 'product_cat';
    $_POST['post_type'] = 'product';
    $_POST['post_type'] = 'product';
    $_POST['tag-name'] = $tmp0->name;
    $_POST['slug'] = $tmp0->path;
    $_POST['description'] = $tmp0->description;
    $_POST['parent'] = 0;

   // $ret = wp_insert_term($_POST['tag-name'], 'product_cat', $_POST);
    $sql = "UPDATE shop_groups
            SET seo_keywords='" . $ret['term_id'] . "'
            WHERE id=" . $tmp0->id;

  //  $wpdb->query($sql);

    //   $tmp0->deleted = $ret['term_id'];

    //  $categoryAll[] = $tmp0;

    foreach ($category_parent as $tmpp) {
        $_POST['taxonomy'] = 'product_cat';
        $_POST['post_type'] = 'product';
        $_POST['post_type'] = 'product';
        $_POST['tag-name'] = $tmpp->name;
        $_POST['slug'] = $tmpp->path;
        $_POST['description'] = $tmpp->description;
        $_POST['parent'] = $ret['term_id'];

     //   $ret_t = wp_insert_term($_POST['tag-name'], 'product_cat', $_POST);
        //   print_r($ret_t);
        $sql = "UPDATE shop_groups
            SET seo_keywords='" . $ret_t['term_id'] . "'
            WHERE id=" . $tmpp->id;
        // print_r($tmpp);

       // $wpdb->query($sql);

        //    $tmpp->deleted = $ret_t['term_id'];
        //     $categoryAll[] = $tmpp;
    }

}

$sql = "SELECT * FROM shop_items
WHERE shop_id=1 AND deleted=0 AND shop_group_id<>0 AND seo_keywords<>'1'
GROUP BY marking
ORDER BY shop_group_id";


$product = $wpdb->get_results($sql);

foreach ($product as $tmp) {

    //print_r($tmp);
    $data = array();
    $data['description'] = $tmp->description;
    $data['title'] = $tmp->name;
    $sql = "SELECT seo_keywords FROM shop_groups
            WHERE id=" . $tmp->shop_group_id;

    $shop_group_id = $wpdb->get_var($sql);
    // print_r($shop_group_id);die;

    $term = get_term($shop_group_id, 'product_cat');

    $data['categories'] = array($term->name);
    $data['sku'] = $tmp->marking;
    $data['price'] = $tmp->price;

    $data['product'] = $data;

    $temp = WC()->api->WC_API_Products->create_product($data);

    $st = (string )$tmp->id;

    $st = $st[0] . '/' . $st[1] . '/' . $st[2];
    $image = 'http://6.abg.pp.ua/tmp/'. $tmp->image_large;
    
  // print_r($temp); die;  
    set_featured_image($temp['product']['id'], $image);

    //  забиваем атрибуты
    $sql = "SELECT
*
FROM `property_value_strings` a
LEFT JOIN properties b ON a.property_id=b.id
WHERE  `entity_id` = '$tmp->id' AND b.deleted=0  ";

    $attributes = array();
    $property = $wpdb->get_results($sql);


    if (count($property)) {

        foreach ($property as $pr) {
            $is_visible = 1;
            if ($pr->property_id == 65 or $pr->property_id == 13) {
                $is_visible = 0;
            }
            $attributes[$pr->tag_name] = array(
                'name' => $pr->name,
                'value' => $pr->value,
                'position' => 0,
                'is_visible' => $is_visible,
                'is_variation' => 0,
                'is_taxonomy' => 0);

        }
    }

  //  update_post_meta($temp['product']['id'], '_product_attributes', $attributes);

    $sql = "SELECT a.id,b.tag_name tag_name ,b.`name` `name`, if(b.tag_name='quantity',a.`value`,c.`value`)  `value` FROM property_value_ints a
LEFT JOIN  properties b ON a.property_id=b.id
LEFT JOIN list_items c ON a.`value`=c.id
WHERE a.entity_id='$tmp->id' AND b.deleted=0";
  
  //  $attributes = array();
    $property = $wpdb->get_results($sql);
   //print_r($property);
    if (count($property)) {

        foreach ($property as $pr) {

            $is_visible = 1;
            $attributes[$pr->tag_name] = array(
                'name' => $pr->name,
                'value' => $pr->value,
                'position' => 0,
                'is_visible' => $is_visible,
                'is_variation' => 0,
                'is_taxonomy' => 0);

        }
    }
    
    update_post_meta($temp['product']['id'], '_product_attributes', $attributes);
    
    
$sql = "UPDATE shop_items
SET seo_keywords='1'
WHERE id= '$tmp->id'";
$wpdb->query($sql);
//echo $temp['product']['id']."<br> ";
}

echo "DONE";

$sql = "UPDATE shop_groups
SET seo_keywords=''
WHERE shop_id=1 AND active =1
";
//$wpdb->query($sql);

function set_featured_image($post_id, $image)
{
    // example image
    //$image = 'http://example.com/logo.png';

    // magic sideload image returns an HTML image, not an ID
    $media = media_sideload_image($image, $post_id);

    // therefore we must find it so we can set it as featured ID
    if (!empty($media) && !is_wp_error($media)) {
        $args = array(
            'post_type' => 'attachment',
            'posts_per_page' => -1,
            'post_status' => 'any',
            'post_parent' => $post_id);

        // reference new image to set as featured
        $attachments = get_posts($args);

        if (isset($attachments) && is_array($attachments)) {
            foreach ($attachments as $attachment) {
                // grab source of full size images (so no 300x150 nonsense in path)
                $image = wp_get_attachment_image_src($attachment->ID, 'full');
                // determine if in the $media image we created, the string of the URL exists
                if (strpos($media, $image[0]) !== false) {
                    // if so, we found our image. set it as thumbnail
                    set_post_thumbnail($post_id, $attachment->ID);
                    // only want one image
                    break;
                }
            }
        }
    }

}


Авторизация