array()); public $Attributes = array(); public $Skus = array('Sku' => array()); public $PrimaryCategory = 0; function __construct($id){ $this->product = wc_get_product($id); //Por defecto de la categoria $terms = get_the_terms( $this->product->get_id(), 'product_cat' ); $this->category = $terms[0]; //Exist Product $miraviaId = get_post_meta($id, '_miravia_product_id', true); if($miraviaId) { $this->ItemId = $miraviaId; } $atributos_guardados = get_term_meta($this->category->term_id, "_miravia_attr", true); if($atributos_guardados) { $this->Attributes = $atributos_guardados; } //Completar los datos básicos $this->PrimaryCategory = get_term_meta($this->category->term_id, "_miravia_category", true); $this->Attributes['name'] = $this->product->get_name(); $this->Attributes['description'] = $this->product->get_description(); //Cargar imagenes $attachment_ids = $this->product->get_gallery_image_ids(); foreach( $attachment_ids as $attachment_id ) { $this->Images['Image'][] = $this->defaul_image; } $this->get_skus(); } function get_skus() { if($this->product->is_type('simple')) { $skus = [$this->product]; }else{ $skus = $this->product->get_available_variations(); } foreach($skus as $index => $sku) { $this->Skus['Sku'][$index]["SellerSku"] = $sku->get_sku(); $this->Skus['Sku'][$index]["quantity"] = $sku->get_stock_quantity(); $this->Skus['Sku'][$index]["price"] = $sku->get_regular_price(); if($sku->is_on_sale()) { $this->Skus['Sku'][$index]["special_price"] = $sku->get_sale_price(); } $this->Skus['Sku'][$index]["price"] = $sku->get_regular_price(); if($sku->get_manage_stock() === false) { if($sku->get_stock_status() == "instock") { $stock_available = intval(100); //Stock por defecto }else{ $stock_available = 0; } }else{ $stock_available = $sku->get_stock_quantity(); } $this->Skus['Sku'][$index]["quantity"] = $stock_available; $this->Skus['Sku'][$index]["package_height"] = $sku->get_height(); $this->Skus['Sku'][$index]["package_length"] = $sku->get_length(); $this->Skus['Sku'][$index]["ean_code"] = '0'; //Implementar $this->Skus['Sku'][$index]["package_width"] = $sku->get_width(); $this->Skus['Sku'][$index]["package_weight"] = $sku->get_weight(); $this->Skus['Sku'][$index]["package_content"] = ''; //Implementar $attachment_ids = $sku->get_gallery_image_ids(); foreach( $attachment_ids as $attachment_id ) { $this->Skus['Sku'][$index]["Images"]["Image"][] = $this->defaul_image; } } } function send() { global $MIRAVIAWOO; $send_product = json_encode(array("Request" => array("Product" => array( 'Attributes' => $this->Attributes, 'PrimaryCategory' => $this->PrimaryCategory, 'Skus' => $this->Skus, 'Images' => $this->Images )))); $result_product = $MIRAVIAWOO->client->post('/product/create', array( 'body' => $send_product )); return $result_product; } function update() { global $MIRAVIAWOO; $send_product = json_encode(array("Request" => array("Product" => array( 'ItemId' => $this->ItemId, 'Attributes' => $this->Attributes, 'PrimaryCategory' => $this->PrimaryCategory, 'Skus' => $this->Skus, 'Images' => $this->Images )))); $result_product = $MIRAVIAWOO->client->post('/product/update', array( 'payload' => $send_product )); error_log(json_encode($result_product)); return $result_product; } } }