Skip to content

Instantly share code, notes, and snippets.

@eto4detak
Last active August 25, 2019 15:06
Show Gist options
  • Save eto4detak/bf3ee9646728b62df9aea5205c2f3419 to your computer and use it in GitHub Desktop.
Save eto4detak/bf3ee9646728b62df9aea5205c2f3419 to your computer and use it in GitHub Desktop.
woo product
<?php
// get price
$custom_price = floatval($price) > 0 ? floatval($price) : 0;
$price = wc_price($custom_price);
$discount_woo = get_woocommerce_currency_symbol() .' '. $discount;
// create product
function stickertime_set_product_default()
{
global $post;
$post_data = array(
'post_title' => 'Sticker',
'post_status' => 'publish',
'ping_status' => 'closed',
'post_type' => 'product',
);
$product_id = wp_insert_post(wp_slash($post_data));
$child_product = wc_get_product($product_id);
$child_product->set_catalog_visibility('hidden');
$child_product->save();
update_post_meta($product_id, '_regular_price', 10);
update_post_meta($product_id, '_price', 10);
update_option('stickertime_default_product', $product_id);
return $product_id;
}
//
public function create_product_variation($data)
{
$postname = sanitize_title($data['title']);
$author = empty($data['author']) ? '1' : $data['author'];
$post_data = array(
'post_author' => $author,
'post_name' => $postname,
'post_title' => $data['title'],
'post_content' => $data['content'],
'post_excerpt' => $data['excerpt'],
'post_status' => 'publish',
'ping_status' => 'closed',
'post_type' => 'product',
);
$product_id = wp_insert_post($post_data);
$terms = array('exclude-from-catalog', 'exclude-from-search');
wp_set_object_terms($product_id, $terms, 'product_visibility');
$product = new WC_Product_Variable($product_id);
$product->save();
return $product_id;
}
public function create_product_simple($product_arg = [])
{
if(isset($this->infoProduct[0]['price2']) || !empty($this->infoProduct[0]['price2'])){
$price2 = $this->infoProduct[0]['price2'];
}
$price = floatval(str_replace(' ', '', $this->infoProduct[0]['price']));
$title = sanitize_text_field($this->infoProduct[0]['nameProduct']);
$slug = self::str2url($this->infoProduct[0]['nameProduct']);
$category = sanitize_text_field($this->infoProduct[0]['manufacturer']);
$category_slug = sanitize_title($this->infoProduct[0]['manufacturer']);
$description = wp_kses_post($this->infoProduct[0]['description']);
$specifications = wp_kses_post($this->infoProduct[0]['specifications']);
if (!empty($product_arg['cat_id'])) {
$product_term_id = $product_arg['cat_id'];
}
// проверить существует ли такое название поста
$product_posts = get_posts(array(
'post_type' => array('product'),
'posts_per_page' => -1,
'meta_key' => '_is_factory_parse',
'meta_value' => '1',
));
$is_product = 0;
foreach ($product_posts as $key => $produ_post) {
if ($produ_post->post_title === $title) {
$is_product = 1;
}
}
if ($is_product) {
return;
}
$product_attr['article'] = ['name' => 'Артикул', 'value' => $this->infoProduct[0]['article']];
$product_attr['model'] = ['name' => 'Модель', 'value' => $this->infoProduct[0]['model']];
$product_attr['manufacturer'] = ['name' => 'Производитель', 'value' => $this->infoProduct[0]['manufacturer']];
$product_attr['land'] = ['name' => 'Страна', 'value' => $this->infoProduct[0]['land']];
$product_attr['collection'] = ['name' => 'Коллекция', 'value' => $this->infoProduct[0]['collection']];
$product_attr['color'] = ['name' => 'Цветовая гамма', 'value' => $this->infoProduct[0]['color']];
$product_attr['type'] = ['name' => 'Тип', 'value' => $this->infoProduct[0]['type']];
$product_attr['purpose'] = ['name' => 'Назначение', 'value' => $this->infoProduct[0]['purpose']];
$product_attr['format'] = ['name' => 'Формат/размер, мм', 'value' => $this->infoProduct[0]['format']];
$product_attr['weight'] = ['name' => 'Вес, кг/шт', 'value' => $this->infoProduct[0]['weight']];
// добавить таксономии атрибуты глобально
foreach ($product_attr as $key => $field) {
$this->save_product_attribute_from_name($key, $field['name']);
}
$post = array(
// 'post_author' => $user_id,
'post_name' => $slug,
'post_content' => $description,
'post_status' => "publish",
'post_title' => $title,
'post_parent' => '',
'post_type' => "product",
);
$post_id = wp_insert_post($post);
update_post_meta($post_id, '_et_pb_page_layout', 'et_full_width_page');
wp_set_object_terms($post_id, 'simple', 'product_type');
//присвоить/создать категорию
if (isset($product_term_id)) {
wp_set_object_terms($post_id, $product_term_id, 'product_cat');
} else {
$term = get_term_by('name', $category, 'product_cat');
if (empty($term->term_id)) {
wp_insert_term($category, 'product_cat', array('description' => '', 'parent' => 0, 'slug' => $category_slug));
$term = get_term_by('name', $category, 'product_cat');
}
wp_set_object_terms($post_id, $term->term_id, 'product_cat');
}
$sku = 'VK' . '-' . sprintf( "%'.06d", $post_id);
// add metafield
update_post_meta($post_id, '_visibility', 'visible');
update_post_meta($post_id, '_stock_status', 'instock');
// update_post_meta($post_id, 'total_sales', '0');
update_post_meta($post_id, '_downloadable', 'no');
update_post_meta($post_id, '_virtual', 'no');
if(empty($price2)){
update_post_meta($post_id, '_regular_price', $price);
update_post_meta($post_id, '_price', $price);
}else{
update_post_meta($post_id, '_regular_price', $price2);
update_post_meta($post_id, '_price', $price2);
update_post_meta($post_id, '_sale_price', $price);
}
update_post_meta($post_id, '_purchase_note', "");
update_post_meta($post_id, '_featured', "no");
update_post_meta($post_id, '_weight', "");
update_post_meta($post_id, '_length', "");
update_post_meta($post_id, '_width', "");
update_post_meta($post_id, '_height', "");
update_post_meta($post_id, '_sku', $sku);
update_post_meta($post_id, '_product_attributes', array());
update_post_meta($post_id, '_sale_price_dates_from', "");
update_post_meta($post_id, '_sale_price_dates_to', "");
update_post_meta($post_id, '_sold_individually', "");
update_post_meta($post_id, '_manage_stock', "no");
update_post_meta($post_id, '_backorders', "no");
update_post_meta($post_id, '_stock', "");
update_post_meta($post_id, 'custom_specifications', $specifications);
update_post_meta($post_id, '_is_factory_parse', 1);
// add atribute
$arr_attr_to_paren_product = [];
foreach ($product_attr as $key => $field) {
// $arr_attr_to_paren_product[$field['name']] = $this->to_string_attribute($field['value']);
$arr_attr_to_paren_product[$field['name']] = $field['value'];
}
// dowload file
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$is_thumbnail = 0;
$product_image_gallery = [];
foreach ($this->infoProduct[0]['image_src'] as $src_key => $src) {
// получть название файлов
$attachment_posts = get_posts(array(
'post_type' => array('attachment'),
'post_status' => array('inherit'),
'meta_key' => '_is_factory_parse',
'meta_value' => '1',
'posts_per_page' => -1,
));
$attachment_file = [];
foreach ($attachment_posts as $id_key => $attach) {
$attachment_file[$attach->ID] = basename($attach->guid);
}
$file_name = basename($src);
$file_name = str_replace("_", "-", $file_name);
$result_media_id = array_search($file_name, $attachment_file);
if (empty($result_media_id)) {
$file_array = [];
$tmp = download_url($src);
preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $src, $matches);
// $file_array['name'] = basename($matches[0]);
$name_file = explode('.', basename($matches[0]));
$extension_file = end($name_file);
$file_array['name'] = self::generateRandomString(20).'.'.$extension_file;
$file_array['tmp_name'] = $tmp;
$media_id = media_handle_sideload($file_array, $post_id);
update_post_meta($media_id, '_is_factory_parse', 1);
// if (is_wp_error($media_id)) {
// @unlink($file_array['tmp_name']);
// }
@unlink($file_array['tmp_name']);
if (!$is_thumbnail) {
set_post_thumbnail($post_id, $media_id);
$is_thumbnail = 1;
} else {
$product_image_gallery[] = $media_id;
}
} else {
if (!$is_thumbnail) {
set_post_thumbnail($post_id, $result_media_id);
$is_thumbnail = 1;
} else {
$product_image_gallery[] = $result_media_id;
}
}
}
update_post_meta($post_id, '_download_limit', '');
update_post_meta($post_id, '_download_expiry', '');
update_post_meta($post_id, '_download_type', '');
if(!empty($product_image_gallery)){
update_post_meta($post_id, '_product_image_gallery', implode(',', $product_image_gallery));
}
return ['attr' => $product_attr, 'post_id' => $post_id];
// $this->wcproduct_set_attributes($post_id, $arr_attr_to_paren_product);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment