id = $saved->id; $this->id_woocommerce = $saved->id_woocommerce; $this->id_miravia = $saved->id_miravia; $this->profile = $saved->profile_id; $this->last_updated = $saved->last_updated; $_product = wc_get_product(intval($this->id_woocommerce)); }else{ $this->id_woocommerce = $id; //Load product $_product = wc_get_product(intval($this->id_woocommerce)); $this->profile = $profile['id']; $wpdb->insert($wpdb->prefix.'miravia_products', array( 'id_woocommerce' => $this->id_woocommerce, 'sku' => $_product->get_sku() ?: $id, 'id_miravia' => $this->id_miravia, 'profile_id' => $this->profile, 'stock' => $_product->get_regular_price(), 'price' => $_product->get_regular_price(), 'sale_price' => $_product->get_sale_price(), )); $this->id = $wpdb->insert_id; } if($_product->get_manage_stock() === false) { if($_product->get_stock_status() == "instock") { $stock_available = get_option('_miravia_default_stock', 100); //Stock por defecto }else{ $stock_available = 0; } }else{ $stock_available = $_product->get_stock_quantity(); } $local_category = MiraviaCore::get_miravia_category($_product, $profile); // $attributes_product = MiraviaCore::get_attributes($_product, $local_category); // $attributes_product = $_product->get_attributes(); // die("
".print_r($attributes_product, true).""); $this->product = new MiraviaProduct(); $this->product->id = $this->id_woocommerce; $this->product->id_miravia = $this->id_miravia; $this->product->sku = $_product->get_sku() ?: $id; $this->product->lang = 'es-ES'; // ISO lang cod $this->product->name = $_product->get_name(); $this->product->short_description = $_product->get_short_description(); $this->product->description = $_product->get_description(); $this->product->brand = $defaultBrand; $this->product->model = ''; $this->product->ean_code = $ean; $this->product->warranty = ''; $this->product->id_category = $profile['miravia_category']; $this->product->id_brand = ''; $this->product->images = []; // Se completa después $this->product->video = ''; $this->product->price = $_product->get_regular_price(); $this->product->special_price = $_product->get_sale_price(); $this->product->width = $_product->get_width(); $this->product->height = $_product->get_height(); $this->product->length = $_product->get_length(); $this->product->weight = $_product->get_weight(); $this->product->delivery = get_option('miravia_transport_mode', 'dbm'); $this->product->info = array( 'id_category' => $local_category ); //$this->product->extra_attributes = []; # TODO agregar caracteristicas //Attrs $this->product->addAttr("unit_count_type", $defaultUnit); $this->product->addAttr("Unit_Count", $defaultUnitValue); if($_product->is_type( 'variable' )) { $combinations = []; $variations = $_product->get_available_variations(); // MiraviaCore::debug($variations); foreach($variations as $v) { if($v['is_in_stock'] == true) { if($v['max_qty'] == "") { $stock_available = get_option('_miravia_default_stock', 100); //Stock por defecto }else{ $stock_available = $v["max_qty"]; } }else{ $stock_available = 0; //Stock por defecto } $ean = ''; $keyEAN = get_option('miravia_ean_key', ''); if($keyEAN != '') { $ean = get_post_meta($v['variation_id'], $keyEAN, true); } $com = new MiraviaCombination(); $com->sku = $v['sku']; $com->ean_code = $ean; $com->price = $v['display_regular_price']; $com->special_price = $v['display_price']; $com->quantity = intval($stock_available); $com->images = []; $com->width = $v['dimensions']['width']; $com->height = $v['dimensions']['height']; $com->length = $v['dimensions']['length']; $com->weight = $v['weight']; $com->variation = []; $encontrados = []; $labels = []; $lastAttribute = ""; foreach($v['attributes'] as $k => $a) { $term_name = ( $term = get_term_by( 'slug', $a, str_replace("attribute_", "", $k) ) ) ? $term->name : $a; $nameWithOutAttribute = str_replace("attribute_", "", $k); if(isset($encontrados[$nameWithOutAttribute])) { $encontrados[$nameWithOutAttribute] .= $term_name; }else{ $encontrados[$nameWithOutAttribute] = $term_name; } } foreach($encontrados as $b => $c) { $com->addVariation($b, $c); } $combinations[] = $com; } $this->product->combinations = $combinations; } //Set stock $this->product->quantity = $stock_available; //Load images $attachment_ids = $_product->get_gallery_image_ids(); $sizeImage = get_option('miravia_size_image', 'single-post-thumbnail'); $featured = wp_get_attachment_image_src( get_post_thumbnail_id( $this->product->id ), $sizeImage ); if($featured and is_array($featured)) { $this->product->images[] = $featured[0]; } foreach( $attachment_ids as $attachment_id ) { $this->product->images[] = wp_get_attachment_image_src( $attachment_id, $sizeImage )[0]; } } public function getData() { $error = false; //Control de productos sin variaciones ni precio if(($this->product->price == 0 or $this->product->price == "") and count($this->product->combinations) == 0) { LOG::add("Producto {$this->product->id} no agregado por no tener precio y no tener variaciones", false, 'errors_products_send'); $error = true; } if(count($this->product->images) == 0) { LOG::add("Producto {$this->product->id} no tiene imagenes, no enviado", false, 'errors_products_send'); $error = true; } if($error) { return false; } return $this->product; } function send() { return false; } } }