Last active
March 21, 2024 05:56
-
-
Save PhongGCS/23f44147a52c92f27c79dec70f7e7047 to your computer and use it in GitHub Desktop.
Woo commerce CRUD product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CRUDProduct { | |
public function sync_product($item){ | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'posts_per_page' => 1, | |
'p' => $item['id'], | |
); | |
$products_exist = new WP_Query($args); | |
if (isset($products_exist) && !empty($products_exist) && !empty($products_exist -> posts[0])) { | |
# code... | |
$item['post_id'] = $products_exist -> posts[0] -> ID; | |
$item['Description'] = get_post_field('post_content', $item['post_id']); | |
$product_parent = $this -> update_product_exists($item); | |
}else{ | |
$product_parent = $this -> add_new_product($item); | |
} | |
} | |
} | |
public function add_new_product($product){ | |
set_time_limit(0); | |
$post = array( | |
'ID' => isset($product['post_id']) && !empty($product['post_id'])?$product['post_id']:null, | |
'post_author' => isset($product['post_author']) && !empty($product['post_author'])?$product['post_author']:null , | |
'post_content' => isset($product['Description'])?$product['Description']:'', | |
'post_status' => isset($product['post_status']) && !empty($product['post_status'])? :"publish", | |
'post_title' => $product['Name'], | |
'post_parent' => isset($product['product_parent_id'])?$product['product_parent_id']:"", | |
'post_type' => "product", | |
'post_category' => isset($product['post_category'])?$product['post_category']:array(0), | |
); | |
$post_id = wp_insert_post( $post, $wp_error ); | |
$product['post_id'] = $post_id; | |
if($post_id){ | |
$attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true); | |
add_post_meta($post_id, '_thumbnail_id', $attach_id); | |
} | |
update_post_meta( $post_id, '_regular_price', $product['SellingPrice'] ); | |
update_post_meta( $post_id, '_sku', $product['Code']); | |
$attributes = []; | |
if (isset($product['Size']) && !empty($product['Size'])) { | |
array_push($attributes, array('name'=> 'Kích thước', 'value' => $product['Size'])); | |
} | |
if (isset($product['Color']) && !empty($product['Color'])) { | |
array_push($attributes, array('name'=> 'Màu sắc','value' => $product['Color'])); | |
} | |
$this -> wcproduct_set_attributes($post_id,$attributes); | |
// lấy số lượng sản phẩm | |
$stock=0; | |
update_post_meta( $post_id, '_price', $product['SellingPrice']); | |
update_post_meta( $post_id, '_stock', $stock); | |
// update_post_meta($post_id, '_manage_stock', 'yes'); | |
if (isset($product['post_id']) && !empty($product['post_id'])) { | |
echo("set atrib"); | |
# code... | |
wp_set_object_terms( $post_id, 'Races', 'product_cat' ); | |
wp_set_object_terms( $post_id, 'simple', 'product_type'); | |
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', 'yes'); | |
update_post_meta( $post_id, '_virtual', 'yes'); | |
update_post_meta( $post_id, '_sale_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, '_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', "yes" ); // cho phép thiết lập sử dụng thiết lập tồn kho | |
update_post_meta( $post_id, '_backorders', "no" ); // hết hàng sẽ không cho nhà hàng bán sản phẩm nữa = no, = yes cho bán | |
update_post_meta( $post_id, '_download_limit', ''); | |
update_post_meta( $post_id, '_download_expiry', ''); | |
update_post_meta( $post_id, '_download_type', ''); | |
update_post_meta( $post_id, '_product_image_gallery', ''); | |
} | |
$this -> upload_thumbnail_product(array('url'=> $product['Picture'], 'post_id'=> $post_id,'alt'=> json_encode($product['Name'],JSON_UNESCAPED_UNICODE))); | |
} | |
public function update_product_exists($product){ | |
$post = array( | |
'ID' => isset($product['post_id']) && !empty($product['post_id'])?$product['post_id']:null, | |
'post_author' => isset($product['post_author']) && !empty($product['post_author'])?$product['post_author']:null , | |
'post_content' => isset($product['Description'])?$product['Description']:'', | |
'post_status' => isset($product['post_status']) && !empty($product['post_status'])? :"publish", | |
'post_title' => $product['Name'], | |
'post_parent' => isset($product['product_parent_id'])?$product['product_parent_id']:"", | |
'post_type' => "product", | |
'post_category' => isset($product['post_category'])?$product['post_category']:array(0), | |
); | |
$post_id = wp_insert_post( $post, $wp_error ); | |
$product['post_id'] = $post_id; | |
if($post_id){ | |
$attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true); | |
add_post_meta($post_id, '_thumbnail_id', $attach_id); | |
} | |
update_post_meta( $post_id, '_regular_price', $product['SellingPrice'] ); | |
update_post_meta( $post_id, '_sku', $product['Code']); | |
update_post_meta( $post_id, '_price', $product['SellingPrice']); | |
//update_post_meta( $post_id, '_stock', $stock); | |
update_post_meta( $post_id, '_manage_stock', "yes" ); // cho phép thiết lập sử dụng thiết lập tồn kho | |
update_post_meta( $post_id, '_backorders', "no" ); // hết hàng sẽ không cho nhà hàng bán sản phẩm nữa = no, = yes cho bán | |
return $product; | |
} | |
/*thêm thuộc tính cho sản phẩm*/ | |
public function wcproduct_set_attributes($post_id,$attributes){ | |
if (isset($attributes) && count($attributes) > 0 ) { | |
# code... | |
$i= 0; | |
foreach ($attributes as $name => $value) { | |
# code... | |
$product_attributes[$i] = array( | |
'name' => $value['name'], | |
'is_visible' => 1, | |
'is_variation' => 0, | |
'is_taxonomy' => 0, | |
'value' => str_replace(',', '|', $value['value']) | |
); | |
$i++; | |
} | |
update_post_meta( $post_id, '_product_attributes',$product_attributes); | |
} | |
} | |
public function upload_thumbnail_product($image_product){ | |
set_time_limit(0); | |
$filename = $image_product['post_id'] .'-'. $this->vn_to_str(sanitize_file_name($image_product['alt'])) .'-'.$_SERVER['HTTP_HOST']; | |
$exist_file_name = get_post_meta( $image_product['post_id'], $key = '_mshopkeeper_product_Image', $single = false ); | |
// if ( empty($exist_file_name) || (!empty($exist_file_name) && $exist_file_name[0] != $filename)) { | |
if (!isset($exist_file_name) || empty($exist_file_name)) { | |
# code... | |
$image_id = $this->uploadRemoteImageAndAttach($image_product['url'],$image_product['post_id'],$image_product['alt'],$filename); | |
if (isset($image_id) && !empty($image_id)) { | |
# code... | |
set_post_thumbnail( $image_product['post_id'], $image_id ); | |
update_post_meta( $image_product['post_id'], '_mshopkeeper_product_Image', $filename); | |
} | |
} | |
} | |
function uploadRemoteImageAndAttach($image_url, $parent_id,$alt,$filename){ | |
try { | |
if (isset($image_url) && !empty($image_url)) { | |
# code... | |
$image = $image_url; | |
$get = wp_remote_get( $image ); | |
$type = wp_remote_retrieve_header( $get, 'content-type' ); | |
if (!$type) | |
return false; | |
$mirror = wp_upload_bits( $filename . '.jpeg', '', wp_remote_retrieve_body( $get ) ); | |
$attachment = array( | |
'post_title'=> $alt, | |
'post_mime_type' => $type, | |
'post_content' => $alt, | |
'post_status' => 'inherit' | |
); | |
$attach_id = wp_insert_attachment( $attachment, $mirror['file'], $parent_id ); | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $mirror['file'] ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
return $attach_id; | |
} | |
} catch (Exception $e) { | |
$this-> log(json_encode($e)); | |
} | |
} | |
function vn_to_str ($str){ | |
$unicode = array( | |
'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ', | |
'd'=>'đ', | |
'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ', | |
'i'=>'í|ì|ỉ|ĩ|ị', | |
'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ', | |
'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự', | |
'y'=>'ý|ỳ|ỷ|ỹ|ỵ', | |
'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ', | |
'D'=>'Đ', | |
'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ', | |
'I'=>'Í|Ì|Ỉ|Ĩ|Ị', | |
'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ', | |
'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự', | |
'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ', | |
); | |
foreach($unicode as $nonUnicode=>$uni){ | |
$str = preg_replace("/($uni)/i", $nonUnicode, $str); | |
} | |
$str = str_replace(' ','_',$str); | |
return $str; | |
} | |
/** | |
* Get batch | |
* | |
* @return stdClass Return the all batch from the queue | |
*/ | |
function get_all_batch() { | |
global $wpdb; | |
$table = $wpdb->options; | |
$column = 'option_name'; | |
if ( is_multisite() ) { | |
$table = $wpdb->sitemeta; | |
$column = 'meta_key'; | |
} | |
$key = $this->identifier . '_batch_%'; | |
$count = $wpdb->get_var( $wpdb->prepare( " | |
SELECT COUNT(*) | |
FROM {$table} | |
WHERE {$column} LIKE %s | |
", $key ) ); | |
return $count; | |
} | |
public function upload_thumbnail_product($image_product){ | |
$filenametemp = $image_product['url']; | |
preg_match('/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/',$filenametemp,$filename) ; | |
$filename = $image_product['post_id'] .'-'. $filename[0]; | |
set_time_limit(0); | |
$exist_file_name = get_post_meta( $image_product['post_id'], $key = '_mshopkeeper_product_Image', $single = false ); | |
if ( empty($exist_file_name) || (!empty($exist_file_name) && $exist_file_name[0] != $filename)) { | |
# code... | |
$image_id = $this->uploadRemoteImageAndAttach($image_product['url'],$image_product['post_id'],$image_product['alt'],$filename); | |
if (isset($image_id) && !empty($image_id)) { | |
# code... | |
set_post_thumbnail( $image_product['post_id'], $image_id ); | |
update_post_meta( $image_product['post_id'], '_mshopkeeper_product_Image', $filename); | |
} | |
} | |
} | |
function uploadRemoteImageAndAttach($image_url, $parent_id,$alt,$filename){ | |
try { | |
set_time_limit(0); | |
if (isset($image_url) && !empty($image_url)) { | |
# code... | |
$image = $image_url; | |
$get = wp_remote_get( $image ); | |
$type = wp_remote_retrieve_header( $get, 'content-type' ); | |
if (!$type) | |
return false; | |
$mirror = wp_upload_bits( $filename . '.jpeg', '', wp_remote_retrieve_body( $get ) ); | |
$attachment = array( | |
'post_title'=> $alt, | |
'post_mime_type' => $type, | |
'post_content' => $alt, | |
'post_status' => 'inherit' | |
); | |
$attach_id = wp_insert_attachment( $attachment, $mirror['file'], $parent_id ); | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $mirror['file'] ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
return $attach_id; | |
} | |
return null; | |
} catch (Exception $e) { | |
$this-> log(json_encode($e)); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment