Commit d6beb22b authored by Quan Nguyen's avatar Quan Nguyen

update code

parent 70e67e4e
Pipeline #513 failed with stages
......@@ -18,8 +18,8 @@
* @package WordPress
*/
// ** URL setting ** //
define( 'WP_HOME', 'https://rangrangcoffee' );
define( 'WP_SITEURL', 'https://rangrangcoffee' );
define( 'WP_HOME', 'https://rangrangcoffee.com' );
define( 'WP_SITEURL', 'https://rangrangcoffee.com' );
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
......
......@@ -170,17 +170,6 @@ class RRCKiotvietWcProduct
// Now we will add it as a attribute connected to the product
$product_attributes = get_post_meta( $product_id, '_product_attributes', true );
if(empty($product_attributes)){
$product_attributes = [];
$product_attributes[$taxonomy] = array (
'name' => $taxonomy,
'value' => '',
'position' => 0,
'is_visible' => 0,
'is_variation' => 1,
'is_taxonomy' => 1
);
} else {
if(empty($product_attributes[$taxonomy])){
$product_attributes[$taxonomy] = array (
'name' => $taxonomy,
......@@ -191,9 +180,10 @@ class RRCKiotvietWcProduct
'is_taxonomy' => 1
);
}
}
update_post_meta( $product_id,'_product_attributes', $product_attributes);
update_option('test_units', $data['units']);
// START
foreach( $data['units'] as $value ){
$term_name = ucfirst($value->unit);
......@@ -204,7 +194,8 @@ class RRCKiotvietWcProduct
$term_add = wp_insert_term( $term_name, $taxonomy, array('slug' => $term_slug ) ); // Create the term
// Set attribute values
wp_set_object_terms( $product_id, $term_name, $taxonomy, true );
wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
$term = get_term( $term_add->term_id, $taxonomy );
// And finally a variation is created
$variation_post = array(
......@@ -214,10 +205,10 @@ class RRCKiotvietWcProduct
'post_excerpt'=> 'My attribute: ' . $term_name,
'post_parent' => $product_id,
'post_type' => 'product_variation',
'guid' => $product->get_permalink()
// 'meta_input' => array(
// 'attribute_pa_bag' => $term_slug
// )
'guid' => $product->get_permalink(),
'meta_input' => array(
'attribute_pa_bag' => $term->slug
)
);
// Creating the product variation
......@@ -225,11 +216,6 @@ class RRCKiotvietWcProduct
$variation = new WC_Product_Variation( $variation_id );
$this->set_image_data($variation, $data);
// Set/save the attribute data in the product variation
update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
## Set/save all other data
......@@ -241,8 +227,7 @@ class RRCKiotvietWcProduct
$variation->set_downloadable(false);
// Description.
$variation->set_description(wp_kses_post($product['description']));
//set image
// Stock
$variation->set_manage_stock(false);
......@@ -254,55 +239,6 @@ class RRCKiotvietWcProduct
}
/**
* Save product images.
*
* @throws WC_REST_Exception REST API exceptions.
* @param WC_Product $product Product instance.
* @param array $images Images data.
* @return WC_Product
*/
protected function save_product_images($product, $images)
{
if (is_array($images)) {
$gallery = array();
foreach ($images as $image) {
$attachment_id = isset($image['id']) ? absint($image['id']) : 0;
if (0 === $attachment_id && isset($image['src'])) {
$upload = wc_rest_upload_image_from_url(esc_url_raw($image['src']));
if (is_wp_error($upload)) {
if (!apply_filters('woocommerce_rest_suppress_image_upload_error', false, $upload, $product->get_id(), $images)) {
throw new WC_REST_Exception('woocommerce_product_image_upload_error', $upload->get_error_message(), 400);
} else {
continue;
}
}
$attachment_id = wc_rest_set_uploaded_image_as_attachment($upload, $product->get_id());
}
if (isset($image['position']) && 0 === $image['position']) {
$product->set_image_id($attachment_id);
} else {
$gallery[] = $attachment_id;
}
// Set the image alt if present.
if (!empty($image['alt'])) {
update_post_meta($attachment_id, '_wp_attachment_image_alt', wc_clean($image['alt']));
}
// Set the image name if present.
if (!empty($image['name'])) {
wp_update_post(array('ID' => $attachment_id, 'post_title' => $image['name']));
}
}
if (!empty($gallery)) {
$product->set_gallery_image_ids($gallery);
}
} else {
$product->set_image_id('');
$product->set_gallery_image_ids(array());
}
return $product;
}
// public function get_product_by_sku($sku)
// {
// global $wpdb;
......@@ -1219,7 +1155,7 @@ class RRCKiotvietWcProduct
'product_id' => $parentId,
'product_kv_id' => $product['kv_id'],
'data_raw' => json_encode($product, JSON_UNESCAPED_UNICODE),
'parent' => $product['master_product_id'],
'parent' => 0,
'retailer' => $this->retailer,
'created_at' => rrc_kiotviet_sync_get_current_time(),
'status' => 1
......@@ -1231,17 +1167,17 @@ class RRCKiotvietWcProduct
public function productVariation($product)
{
$productSync = $this->wpdb->get_row("SELECT * FROM {$this->wpdb->prefix}rrc_kiotviet_sync_products WHERE `parent` = " . $product['master_product_id'] . " AND `retailer` = '" . $this->retailer . "'", ARRAY_A);
$productSync = $this->wpdb->get_row("SELECT * FROM {$this->wpdb->prefix}rrc_kiotviet_sync_products WHERE `product_kv_id` = " . $product['master_product_id'] . " AND `retailer` = '" . $this->retailer . "'", ARRAY_A);
$productVariant = [];
if ($productSync) {
$product['parent_id'] = $productSync['product_id'];
$product['parent_id'] = $productSync->product_id;
$productVariant = $this->import_product($product);
if (!is_wp_error($productVariant)) {
$this->insertProductMap([
'product_id' => $productVariant,
'product_kv_id' => $product['kv_id'],
'data_raw' => json_encode($product, JSON_UNESCAPED_UNICODE),
'parent' => 0,
'parent' => $product['master_product_id'],
'retailer' => $this->retailer,
'status' => $productSync['status'],
'created_at' => rrc_kiotviet_sync_get_current_time(),
......
......@@ -178,6 +178,13 @@ class Rrc_Kiotviet_Sync_Service_Product
if(!empty($item->getUnits())){
$units = [];
array_push($units, [
'name' => $item->getName(),
'conversionValue' => $item->getConversionValue(),
'unit' => $item->getUnit(),
'basePrice' => $item->getBasePrice()
]);
$attr_values = [];
$attr_units = [];
array_push($attr_values, $item->getUnit());
......@@ -351,6 +358,7 @@ class Rrc_Kiotviet_Sync_Service_Product
}
//map product type in kiotviet and wc
if ($product['hasVariants'] == true) {
if ($product['kvProductType'] == "thue-bao"){
$product['type'] = "variable";
$result = $this->KiotvietWcProduct->productVariable($product);
......@@ -361,6 +369,13 @@ class Rrc_Kiotviet_Sync_Service_Product
}
$result = $this->KiotvietWcProduct->productSimple($product);
}
} else {
$product['type'] = "simple";
if (isset($product['unit']) && !empty($product['unit'])) {
$product['name'] = $product['name'] . " - " . $product['unit'];
}
$result = $this->KiotvietWcProduct->productSimple($product);
}
//check meta data, location, product type
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment