public function execute()
 {
     $service_model = new shopServiceModel();
     $service_product_model = new shopProductServicesModel();
     $id = waRequest::get('id', null, waRequest::TYPE_INT);
     $edit = waRequest::get('edit', null, waRequest::TYPE_STRING_TRIM);
     if ($edit == 'name') {
         $service_model->updateById($id, array('name' => waRequest::post('name', '', waRequest::TYPE_STRING_TRIM)));
         return;
     }
     if ($id) {
         $service = $service_model->getById($id);
         if (!$service) {
             $this->errors[] = _w("Unknown service to update");
             return;
         }
     }
     if ($id) {
         // delete products
         $delete_products = waRequest::post('delete_product', array(), waRequest::TYPE_ARRAY_INT);
         $service_product_model->deleteByProducts($delete_products, $id);
     }
     $id = $service_model->save($this->getData(), $id, true);
     $this->response = array('id' => $id);
 }
예제 #2
0
 public function delete($id)
 {
     if (!$this->deleteById($id)) {
         return false;
     }
     $product_services = new shopProductServicesModel();
     $product_services->deleteByField('service_variant_id', $id);
     return true;
 }
 public function getServices($product_id, $sku_id)
 {
     $product_model = new shopProductModel();
     $product = $product_model->getById($product_id);
     $type_service_model = new shopTypeServicesModel();
     $service_ids = $type_service_model->getServiceIds($product['type_id']);
     $sql = "SELECT v.*, ps.price p_price, ps.status, ps.sku_id, s.currency FROM shop_service_variants v\n                LEFT JOIN shop_product_services ps ON v.id = ps.service_variant_id AND ps.product_id = i:product_id\n                JOIN shop_service s ON v.service_id = s.id\n                WHERE " . ($service_ids ? "v.service_id IN (i:service_ids) OR " : '') . "\n                ps.product_id = i:product_id OR ps.sku_id = i:sku_id\n                ORDER BY ps.sku_id";
     $product_services_model = new shopProductServicesModel();
     $rows = $product_services_model->query($sql, array('service_ids' => $service_ids, 'product_id' => $product_id, 'sku_id' => $sku_id))->fetchAll();
     $services = array();
     foreach ($rows as $row) {
         $services[$row['service_id']][$row['id']] = array('name' => $row['name'], 'price' => $row['p_price'] ? $row['p_price'] : $row['price'], 'currency' => $row['currency']);
     }
     return $services;
 }
 public function execute()
 {
     $this->product_id = waRequest::get('product_id', null, waRequest::TYPE_INT);
     $this->service_id = waRequest::get('service_id', null, waRequest::TYPE_INT);
     if (!$this->product_id) {
         $this->errors[] = _w("Unknown product");
         return;
     }
     $product_model = new shopProductModel();
     if (!$product_model->checkRights($this->product_id)) {
         throw new waException(_w("Access denied"));
     }
     // check rights
     if (!$this->service_id) {
         $this->errors = _w("Unkown service");
         return;
     }
     $product_services_model = new shopProductServicesModel();
     $product_services_model->save($this->product_id, $this->service_id, $this->getData());
     $this->response = array('status' => $product_services_model->getProductStatus($this->product_id, $this->service_id), 'count' => $product_services_model->countServices($this->product_id));
 }
예제 #5
0
 public function execute()
 {
     $id = waRequest::get('id', null, waRequest::TYPE_INT);
     $service_model = new shopServiceModel();
     $service = array();
     $services = $service_model->getAll('id');
     if ($id !== 0) {
         if (!empty($services)) {
             if ($id && isset($services[$id])) {
                 $service = $services[$id];
             } else {
                 $service = current($services);
             }
         }
     }
     // blank area for adding new service
     if (!$service) {
         $services[] = $this->getEmptyService();
         $type_model = new shopTypeModel();
         $this->assign(array('services' => $services, 'types' => $type_model->getAll(), 'count' => $service_model->countAll(), 'taxes' => $this->getTaxes()));
         return;
     }
     $service_variants_model = new shopServiceVariantsModel();
     $variants = $service_variants_model->get($service['id']);
     $type_services_model = new shopTypeServicesModel();
     $types = $type_services_model->getTypes($service['id']);
     $products_count = 0;
     $selected_types = array();
     foreach ($types as $type) {
         if ($type['type_id']) {
             $selected_types[] = $type['type_id'];
         }
     }
     if (!empty($selected_types)) {
         $product_model = new shopProductModel();
         $products_count = $product_model->countByField(array('type_id' => $selected_types));
     }
     $product_services_model = new shopProductServicesModel();
     $this->assign(array('services' => $services, 'service' => $service, 'products' => $product_services_model->getProducts($service['id']), 'types' => $types, 'products_count' => $products_count, 'variants' => $variants, 'count' => $service_model->countAll(), 'taxes' => $this->getTaxes()));
 }
예제 #6
0
 /**
  * @param array $options
  * @return shopProduct
  * @throws waException
  */
 public function duplicate($options = array())
 {
     if (!$this->checkRights()) {
         throw new waRightsException('Access denied');
     }
     $data = $this->data;
     $skip = array('id', 'create_datetime', 'id_1c', 'rating', 'rating_count', 'total_sales', 'image_id', 'contact_id', 'ext', 'count', 'sku_count');
     foreach ($skip as $field) {
         if (isset($data[$field])) {
             unset($data[$field]);
         }
     }
     $duplicate = new shopProduct();
     $this->getStorage(null);
     $sku_files = array();
     $sku_images = array();
     $ignore_select = true;
     foreach (self::$data_storages as $key => $i) {
         $raw = $this->getStorage($key)->getData($this);
         switch ($key) {
             case 'features_selectable':
                 $storage_data = array();
                 if (!$ignore_select) {
                     if ($this->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) {
                         if (!is_array($raw)) {
                             $raw = array();
                         }
                         foreach ($raw as $id => $f) {
                             if (!empty($f['selected'])) {
                                 foreach ($f['values'] as $value_id => &$value) {
                                     if (!empty($value['selected'])) {
                                         $value = array('id' => $value_id);
                                     } else {
                                         unset($f['values'][$value_id]);
                                     }
                                 }
                                 $storage_data[$id] = $f;
                             }
                         }
                     }
                 }
                 break;
             case 'skus':
                 $storage_data = array();
                 $i = 0;
                 foreach ($raw as $sku_id => $sku) {
                     if (!empty($sku['virtual']) || $ignore_select) {
                         if ($file_path = shopProductSkusModel::getPath($sku)) {
                             $sku_files[$sku['id']] = array('file_name' => $sku['file_name'], 'file_description' => $sku['file_description'], 'file_size' => $sku['file_size'], 'file_path' => $file_path);
                         }
                         if (!empty($sku['image_id'])) {
                             $sku_images[$sku['id']] = $sku['image_id'];
                         }
                         foreach (array('id', 'id_1c', 'product_id', 'image_id', 'file_name', 'file_size', 'file_description') as $field) {
                             if (isset($sku[$field])) {
                                 unset($sku[$field]);
                             }
                         }
                         $storage_data[--$i] = $sku;
                     }
                 }
                 break;
             case 'tags':
                 $storage_data = array_values($raw);
                 break;
             case 'categories':
                 $storage_data = array_keys($raw);
                 break;
             default:
                 $storage_data = $raw;
                 break;
         }
         $duplicate->{$key} = $storage_data;
     }
     $counter = 0;
     $data['url'] = shopHelper::genUniqueUrl($this->url, $this->model, $counter);
     $data['name'] = $this->name . sprintf('(%d)', $counter ? $counter : 1);
     $duplicate->save($data);
     $product_id = $duplicate->getId();
     $sku_map = array_combine(array_keys($this->skus), array_keys($duplicate->skus));
     $config = wa('shop')->getConfig();
     $image_thumbs_on_demand = $config->getOption('image_thumbs_on_demand');
     /**
      * @var shopConfig $config
      */
     if ($this->pages) {
         $product_pages_model = new shopProductPagesModel();
         foreach ($this->pages as $page) {
             unset($page['id']);
             unset($page['create_time']);
             unset($page['update_datetime']);
             unset($page['create_contact_id']);
             $page['product_id'] = $duplicate->getId();
             $product_pages_model->add($page);
         }
     }
     #duplicate images
     $product_skus_model = new shopProductSkusModel();
     $images_model = new shopProductImagesModel();
     $images = $images_model->getByField('product_id', $this->getId(), $images_model->getTableId());
     $callback = create_function('$a, $b', 'return (max(-1, min(1, $a["sort"] - $b["sort"])));');
     usort($images, $callback);
     foreach ($images as $id => $image) {
         $source_path = shopImage::getPath($image);
         $original_file = shopImage::getOriginalPath($image);
         $image['product_id'] = $duplicate->getId();
         if ($sku_id = array_search($image['id'], $sku_images)) {
             $sku_id = $sku_map[$sku_id];
         }
         unset($image['id']);
         try {
             if ($image['id'] = $images_model->add($image, $id == $this->image_id)) {
                 waFiles::copy($source_path, shopImage::getPath($image));
                 if (file_exists($original_file)) {
                     waFiles::copy($original_file, shopImage::getOriginalPath($image));
                 }
                 if ($sku_id) {
                     $product_skus_model->updateById($sku_id, array('image_id' => $image['id']));
                 }
                 if (!$image_thumbs_on_demand) {
                     shopImage::generateThumbs($image, $config->getImageSizes());
                     //TODO use dummy copy  with rename files
                 }
             }
         } catch (waDbException $ex) {
             //just ignore it
             waLog::log('Error during copy product: ' . $ex->getMessage(), 'shop.log');
         } catch (waException $ex) {
             if (!empty($image['id'])) {
                 $images_model->deleteById($image['id']);
             }
             waLog::log('Error during copy product: ' . $ex->getMessage(), 'shop.log');
         }
     }
     foreach ($sku_files as $sku_id => $data) {
         $source_path = $data['file_path'];
         unset($data['file_path']);
         $sku_id = $sku_map[$sku_id];
         $sku = array_merge($duplicate->skus[$sku_id], $data);
         $product_skus_model->updateById($sku_id, $data);
         $target_path = shopProductSkusModel::getPath($sku);
         try {
             waFiles::copy($source_path, $target_path);
         } catch (waException $ex) {
             $data = array('file_name' => '', 'file_description' => '', 'file_size' => 0);
             $product_skus_model->updateById($sku_id, $data);
             print $ex->getMessage();
         }
     }
     $product_features_model = new shopProductFeaturesModel();
     $skus_features = $product_features_model->getSkuFeatures($this->id);
     $skus_features_data = array();
     foreach ($skus_features as $sku_id => $features) {
         $sku_id = $sku_map[$sku_id];
         foreach ($features as $feature_id => $feature_value_id) {
             $skus_features_data[] = compact('product_id', 'sku_id', 'feature_id', 'feature_value_id');
         }
     }
     if ($skus_features_data) {
         $product_features_model->multipleInsert($skus_features_data);
     }
     if ($this->sku_type == shopProductModel::SKU_TYPE_SELECTABLE) {
         $product_features_selectable_model = new shopProductFeaturesSelectableModel();
         if ($features_selectable = $product_features_selectable_model->getByField('product_id', $this->id, true)) {
             foreach ($features_selectable as &$feature_selectable) {
                 $feature_selectable['product_id'] = $product_id;
             }
             unset($feature_selectable);
             $product_features_selectable_model->multipleInsert($features_selectable);
         }
     }
     $product_services_model = new shopProductServicesModel();
     if ($services = $product_services_model->getByField('product_id', $this->id, true)) {
         foreach ($services as &$service) {
             unset($service['id']);
             $service['product_id'] = $product_id;
             $service['sku_id'] = ifset($sku_map[$service['sku_id']]);
             unset($service);
         }
         $product_services_model->multipleInsert($services);
     }
     $product_related_model = new shopProductRelatedModel();
     if ($related = $product_related_model->getByField('product_id', $this->id, true)) {
         foreach ($related as &$row) {
             $row['product_id'] = $product_id;
         }
         unset($row);
         $product_related_model->multipleInsert($related);
     }
     $params = array('product' => &$this, 'duplicate' => &$duplicate);
     /**
      * @wa-event product_duplicate
      */
     wa()->event('product_duplicate', $params);
     return $duplicate;
 }
예제 #7
0
 public function getByCode($code, $full_info = false, $hierarchy = true)
 {
     if (!$code) {
         return array();
     }
     $sql = "SELECT * FROM " . $this->table . " WHERE code = s:0 ORDER BY parent_id";
     $items = $this->query($sql, $code)->fetchAll('id');
     if ($full_info) {
         $product_ids = $sku_ids = $service_ids = $variant_ids = array();
         foreach ($items as $item) {
             $product_ids[] = $item['product_id'];
             $sku_ids[] = $item['sku_id'];
             if ($item['type'] == 'service') {
                 $service_ids[] = $item['service_id'];
                 if ($item['service_variant_id']) {
                     $variant_ids[] = $item['service_variant_id'];
                 }
             }
         }
         $product_model = new shopProductModel();
         $products = $product_model->getByField('id', $product_ids, 'id');
         $sku_model = new shopProductSkusModel();
         $skus = $sku_model->getByField('id', $sku_ids, 'id');
         $service_model = new shopServiceModel();
         $services = $service_model->getByField('id', $service_ids, 'id');
         $service_variants_model = new shopServiceVariantsModel();
         $variants = $service_variants_model->getByField('id', $variant_ids, 'id');
         $product_services_model = new shopProductServicesModel();
         $rows = $product_services_model->getByProducts($product_ids);
         $product_services = $sku_services = array();
         foreach ($rows as $row) {
             if ($row['sku_id'] && !in_array($row['sku_id'], $sku_ids)) {
                 continue;
             }
             $service_ids[] = $row['service_id'];
             if (!$row['sku_id']) {
                 $product_services[$row['product_id']][$row['service_variant_id']] = $row;
             }
             if ($row['sku_id']) {
                 $sku_services[$row['sku_id']][$row['service_variant_id']] = $row;
             }
         }
         foreach ($items as $item_key => &$item) {
             if ($item['type'] == 'product' && isset($products[$item['product_id']])) {
                 $item['product'] = $products[$item['product_id']];
                 if (!isset($skus[$item['sku_id']])) {
                     unset($items[$item_key]);
                     continue;
                 }
                 $sku = $skus[$item['sku_id']];
                 $item['sku_code'] = $sku['sku'];
                 $item['purchase_price'] = $sku['purchase_price'];
                 $item['sku_name'] = $sku['name'];
                 $item['currency'] = $item['product']['currency'];
                 $item['price'] = $sku['price'];
                 $item['name'] = $item['product']['name'];
                 if ($item['sku_name']) {
                     $item['name'] .= ' (' . $item['sku_name'] . ')';
                 }
             } elseif ($item['type'] == 'service' && isset($services[$item['service_id']])) {
                 $item['name'] = $item['service_name'] = $services[$item['service_id']]['name'];
                 $item['currency'] = $services[$item['service_id']]['currency'];
                 $item['service'] = $services[$item['service_id']];
                 $item['variant_name'] = $variants[$item['service_variant_id']]['name'];
                 if ($item['variant_name']) {
                     $item['name'] .= ' (' . $item['variant_name'] . ')';
                 }
                 $item['price'] = $variants[$item['service_variant_id']]['price'];
                 if (isset($product_services[$item['product_id']][$item['service_variant_id']])) {
                     if ($product_services[$item['product_id']][$item['service_variant_id']]['price'] !== null) {
                         $item['price'] = $product_services[$item['product_id']][$item['service_variant_id']]['price'];
                     }
                 }
                 if (isset($sku_services[$item['sku_id']][$item['service_variant_id']])) {
                     if ($sku_services[$item['sku_id']][$item['service_variant_id']]['price'] !== null) {
                         $item['price'] = $sku_services[$item['sku_id']][$item['service_variant_id']]['price'];
                     }
                 }
                 if ($item['currency'] == '%') {
                     $p = $items[$item['parent_id']];
                     $item['price'] = $item['price'] * $p['price'] / 100;
                     $item['currency'] = $p['currency'];
                 }
             }
         }
         unset($item);
     }
     // sort
     foreach ($items as $item_id => $item) {
         if ($item['parent_id']) {
             $items[$item['parent_id']]['services'][] = $item;
             unset($items[$item_id]);
         }
     }
     if (!$hierarchy) {
         $result = array();
         foreach ($items as $item_id => $item) {
             if (isset($item['services'])) {
                 $i = $item;
                 unset($i['services']);
                 $result[$item_id] = $i;
                 foreach ($item['services'] as $s) {
                     $result[$s['id']] = $s;
                 }
             } else {
                 $result[$item_id] = $item;
             }
         }
         $items = $result;
     }
     return $items;
 }
예제 #8
0
 public function deleteServices($product_id, $sku_id)
 {
     $product_services_model = new shopProductServicesModel();
     return $product_services_model->deleteByField(array('product_id' => $product_id, 'sku_id' => $sku_id));
 }
 public function cart()
 {
     $this->getResponse()->addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
     $this->getResponse()->addHeader("Expires", date("r"));
     if (waRequest::method() == 'post') {
         $data = wa()->getStorage()->get('shop/checkout', array());
         if ($coupon_code = waRequest::post('coupon_code')) {
             $data['coupon_code'] = $coupon_code;
         } elseif (isset($data['coupon_code'])) {
             unset($data['coupon_code']);
         }
         if (($use = waRequest::post('use_affiliate')) !== null) {
             if ($use) {
                 $data['use_affiliate'] = 1;
             } elseif (isset($data['use_affiliate'])) {
                 unset($data['use_affiliate']);
             }
         }
         if ($coupon_code || $use) {
             wa()->getStorage()->set('shop/checkout', $data);
             wa()->getStorage()->remove('shop/cart');
         }
     }
     $cart_model = new shopCartItemsModel();
     $cart = new shopCart();
     $code = $cart->getCode();
     $errors = array();
     if (waRequest::post('checkout')) {
         $saved_quantity = $cart_model->select('id,quantity')->where("type='product' AND code = s:code", array('code' => $code))->fetchAll('id');
         $quantity = waRequest::post('quantity');
         foreach ($quantity as $id => $q) {
             if ($q != $saved_quantity[$id]) {
                 $cart->setQuantity($id, $q);
             }
         }
         $not_available_items = $cart_model->getNotAvailableProducts($code, !wa()->getSetting('ignore_stock_count'));
         foreach ($not_available_items as $row) {
             if ($row['sku_name']) {
                 $row['name'] .= ' (' . $row['sku_name'] . ')';
             }
             if ($row['available']) {
                 $errors[$row['id']] = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $row['count'], $row['name']);
             } else {
                 $errors[$row['id']] = _w('Oops! %s is not available for purchase at the moment. Please remove this product from your shopping cart to proceed.');
             }
         }
         if (!$errors) {
             $this->redirect(wa()->getRouteUrl('/frontend/checkout'));
         }
     }
     $items = $cart_model->where('code= ?', $code)->order('parent_id')->fetchAll('id');
     $product_ids = $sku_ids = $service_ids = $type_ids = array();
     foreach ($items as $item) {
         $product_ids[] = $item['product_id'];
         $sku_ids[] = $item['sku_id'];
     }
     $product_ids = array_unique($product_ids);
     $sku_ids = array_unique($sku_ids);
     $product_model = new shopProductModel();
     if (waRequest::param('url_type') == 2) {
         $products = $product_model->getWithCategoryUrl($product_ids);
     } else {
         $products = $product_model->getById($product_ids);
     }
     $sku_model = new shopProductSkusModel();
     $skus = $sku_model->getByField('id', $sku_ids, 'id');
     $image_model = new shopProductImagesModel();
     $delete_items = array();
     foreach ($items as $item_id => &$item) {
         if (!isset($skus[$item['sku_id']])) {
             unset($items[$item_id]);
             $delete_items[] = $item_id;
             continue;
         }
         if ($item['type'] == 'product') {
             $item['product'] = $products[$item['product_id']];
             $sku = $skus[$item['sku_id']];
             if ($sku['image_id'] && $sku['image_id'] != $item['product']['image_id']) {
                 $img = $image_model->getById($sku['image_id']);
                 if ($img) {
                     $item['product']['image_id'] = $sku['image_id'];
                     $item['product']['ext'] = $img['ext'];
                 }
             }
             $item['sku_name'] = $sku['name'];
             $item['sku_code'] = $sku['sku'];
             $item['price'] = $sku['price'];
             $item['compare_price'] = $sku['compare_price'];
             $item['currency'] = $item['product']['currency'];
             $type_ids[] = $item['product']['type_id'];
             if (isset($errors[$item_id])) {
                 $item['error'] = $errors[$item_id];
                 if (strpos($item['error'], '%s') !== false) {
                     $item['error'] = sprintf($item['error'], $item['product']['name'] . ($item['sku_name'] ? ' (' . $item['sku_name'] . ')' : ''));
                 }
             }
         }
     }
     unset($item);
     if ($delete_items) {
         $cart_model->deleteByField(array('code' => $code, 'id' => $delete_items));
     }
     $type_ids = array_unique($type_ids);
     // get available services for all types of products
     $type_services_model = new shopTypeServicesModel();
     $rows = $type_services_model->getByField('type_id', $type_ids, true);
     $type_services = array();
     foreach ($rows as $row) {
         $service_ids[] = $row['service_id'];
         $type_services[$row['type_id']][$row['service_id']] = true;
     }
     // get services for all products
     $product_services_model = new shopProductServicesModel();
     $rows = $product_services_model->getByProducts($product_ids);
     $product_services = $sku_services = array();
     foreach ($rows as $row) {
         if ($row['sku_id'] && !in_array($row['sku_id'], $sku_ids)) {
             continue;
         }
         $service_ids[] = $row['service_id'];
         if (!$row['sku_id']) {
             $product_services[$row['product_id']][$row['service_id']]['variants'][$row['service_variant_id']] = $row;
         }
         if ($row['sku_id']) {
             $sku_services[$row['sku_id']][$row['service_id']]['variants'][$row['service_variant_id']] = $row;
         }
     }
     $service_ids = array_unique($service_ids);
     $service_model = new shopServiceModel();
     $variant_model = new shopServiceVariantsModel();
     $services = $service_model->getByField('id', $service_ids, 'id');
     foreach ($services as &$s) {
         unset($s['id']);
     }
     unset($s);
     $rows = $variant_model->getByField('service_id', $service_ids, true);
     foreach ($rows as $row) {
         $services[$row['service_id']]['variants'][$row['id']] = $row;
         unset($services[$row['service_id']]['variants'][$row['id']]['id']);
     }
     foreach ($items as $item_id => $item) {
         if ($item['type'] == 'product') {
             $p = $item['product'];
             $item_services = array();
             // services from type settings
             if (isset($type_services[$p['type_id']])) {
                 foreach ($type_services[$p['type_id']] as $service_id => &$s) {
                     $item_services[$service_id] = $services[$service_id];
                 }
             }
             // services from product settings
             if (isset($product_services[$item['product_id']])) {
                 foreach ($product_services[$item['product_id']] as $service_id => $s) {
                     if (!isset($s['status']) || $s['status']) {
                         if (!isset($item_services[$service_id])) {
                             $item_services[$service_id] = $services[$service_id];
                         }
                         // update variants
                         foreach ($s['variants'] as $variant_id => $v) {
                             if ($v['status']) {
                                 if ($v['price'] !== null) {
                                     $item_services[$service_id]['variants'][$variant_id]['price'] = $v['price'];
                                 }
                             } else {
                                 unset($item_services[$service_id]['variants'][$variant_id]);
                             }
                         }
                     } elseif (isset($item_services[$service_id])) {
                         // remove disabled service
                         unset($item_services[$service_id]);
                     }
                 }
             }
             // services from sku settings
             if (isset($sku_services[$item['sku_id']])) {
                 foreach ($sku_services[$item['sku_id']] as $service_id => $s) {
                     if (!isset($s['status']) || $s['status']) {
                         // update variants
                         foreach ($s['variants'] as $variant_id => $v) {
                             if ($v['status']) {
                                 if ($v['price'] !== null) {
                                     $item_services[$service_id]['variants'][$variant_id]['price'] = $v['price'];
                                 }
                             } else {
                                 unset($item_services[$service_id]['variants'][$variant_id]);
                             }
                         }
                     } elseif (isset($item_services[$service_id])) {
                         // remove disabled service
                         unset($item_services[$service_id]);
                     }
                 }
             }
             foreach ($item_services as $s_id => &$s) {
                 if (!$s['variants']) {
                     unset($item_services[$s_id]);
                     continue;
                 }
                 if ($s['currency'] == '%') {
                     foreach ($s['variants'] as $v_id => $v) {
                         $s['variants'][$v_id]['price'] = $v['price'] * $item['price'] / 100;
                     }
                     $s['currency'] = $item['currency'];
                 }
                 if (count($s['variants']) == 1) {
                     $v = reset($s['variants']);
                     $s['price'] = $v['price'];
                     unset($s['variants']);
                 }
             }
             unset($s);
             uasort($item_services, array('shopServiceModel', 'sortServices'));
             $items[$item_id]['services'] = $item_services;
         } else {
             $items[$item['parent_id']]['services'][$item['service_id']]['id'] = $item['id'];
             if (isset($item['service_variant_id'])) {
                 $items[$item['parent_id']]['services'][$item['service_id']]['variant_id'] = $item['service_variant_id'];
             }
             unset($items[$item_id]);
         }
     }
     foreach ($items as $item_id => $item) {
         $price = shop_currency($item['price'] * $item['quantity'], $item['currency'], null, false);
         if (isset($item['services'])) {
             foreach ($item['services'] as $s) {
                 if (!empty($s['id'])) {
                     if (isset($s['variants'])) {
                         $price += shop_currency($s['variants'][$s['variant_id']]['price'] * $item['quantity'], $s['currency'], null, false);
                     } else {
                         $price += shop_currency($s['price'] * $item['quantity'], $s['currency'], null, false);
                     }
                 }
             }
         }
         $items[$item_id]['full_price'] = $price;
     }
     $total = $cart->total(false);
     $order = array('total' => $total, 'items' => $items);
     $order['discount'] = $discount = shopDiscounts::calculate($order);
     $order['total'] = $total = $total - $order['discount'];
     $data = wa()->getStorage()->get('shop/checkout');
     $this->view->assign('cart', array('items' => $items, 'total' => $total, 'count' => $cart->count()));
     $this->view->assign('coupon_code', isset($data['coupon_code']) ? $data['coupon_code'] : '');
     if (shopAffiliate::isEnabled()) {
         $affiliate_bonus = 0;
         if ($this->getUser()->isAuth()) {
             $customer_model = new shopCustomerModel();
             $customer = $customer_model->getById($this->getUser()->getId());
             $affiliate_bonus = $customer ? round($customer['affiliate_bonus'], 2) : 0;
         }
         $this->view->assign('affiliate_bonus', $affiliate_bonus);
         $use = !empty($data['use_affiliate']);
         $this->view->assign('use_affiliate', $use);
         if ($use) {
             $discount -= shop_currency(shopAffiliate::convertBonus($order['params']['affiliate_bonus']), $this->getConfig()->getCurrency(true), null, false);
             $this->view->assign('used_affiliate_bonus', $order['params']['affiliate_bonus']);
         }
         $order['currency'] = $this->getConfig()->getCurrency(false);
         $add_affiliate_bonus = shopAffiliate::calculateBonus($order);
         $this->view->assign('add_affiliate_bonus', round($add_affiliate_bonus, 2));
     }
     $this->view->assign('discount', $discount);
     /**
      * @event frontend_cart
      * @return array[string]string $return[%plugin_id%] html output
      */
     $this->view->assign('frontend_cart', wa()->event('frontend_cart'));
     $checkout_flow = new shopCheckoutFlowModel();
     $checkout_flow->add(array('code' => $code, 'step' => 0, 'description' => null));
 }
예제 #10
0
    public function execute()
    {
        $this->setLayout(new shopFrontendLayout());
        if ($this->params) {
            $product = $this->params;
        } else {
            $product_model = new shopProductModel();
            $product = $product_model->getByField('url', waRequest::param('product_url'));
        }
        if (!$product) {
            throw new waException(_w('Product not found'), 404);
        }
        if ($types = waRequest::param('type_id')) {
            if (!in_array($product['type_id'], (array) $types)) {
                throw new waException(_w('Product not found'), 404);
            }
        }
        $is_cart = waRequest::get('cart');
        if ($is_cart) {
            $this->setLayout(null);
        }
        $product = new shopProduct($product);
        if (!$is_cart) {
            $this->getBreadcrumbs($product);
        }
        // check url
        $product['num'] = 141;
        if ($product['url'] !== urldecode(waRequest::param('product_url'))) {
            $url_params = array('product_url' => $product['url']);
            if ($product['category_id']) {
                $url_params['category_url'] = $product['category_url'];
            }
            $q = waRequest::server('QUERY_STRING');
            $this->redirect(wa()->getRouteUrl('/frontend/product', $url_params) . ($q ? '?' . $q : ''), 301);
        }
        $this->prepareProduct($product);
        $this->addCanonical();
        // get services
        $type_services_model = new shopTypeServicesModel();
        $services = $type_services_model->getServiceIds($product['type_id']);
        $service_model = new shopServiceModel();
        $product_services_model = new shopProductServicesModel();
        $services = array_merge($services, $product_services_model->getServiceIds($product['id']));
        $services = array_unique($services);
        $services = $service_model->getById($services);
        $variants_model = new shopServiceVariantsModel();
        $rows = $variants_model->getByField('service_id', array_keys($services), true);
        foreach ($rows as $row) {
            if (!$row['price']) {
                $row['price'] = $services[$row['service_id']]['price'];
            }
            $services[$row['service_id']]['variants'][$row['id']] = $row;
        }
        $rows = $product_services_model->getByField('product_id', $product['id'], true);
        $skus_services = array();
        foreach ($product['skus'] as $sku) {
            $skus_services[$sku['id']] = array();
        }
        foreach ($rows as $row) {
            if (!$row['sku_id']) {
                // remove disabled services and variantsimg
                if (!$row['status']) {
                    unset($services[$row['service_id']]['variants'][$row['service_variant_id']]);
                } elseif ($row['price'] !== null) {
                    // update price
                    $services[$row['service_id']]['variants'][$row['service_variant_id']]['price'] = $row['price'];
                }
                if ($row['status'] == shopProductServicesModel::STATUS_DEFAULT) {
                    // update default
                    $services[$row['service_id']]['variant_id'] = $row['service_variant_id'];
                }
            } else {
                if (!$row['status']) {
                    $skus_services[$row['sku_id']][$row['service_id']][$row['service_variant_id']] = false;
                } else {
                    $skus_services[$row['sku_id']][$row['service_id']][$row['service_variant_id']] = $row['price'];
                }
            }
        }
        foreach ($skus_services as $sku_id => &$sku_services) {
            $sku_price = $product['skus'][$sku_id]['price'];
            foreach ($services as $service_id => $service) {
                if (isset($sku_services[$service_id])) {
                    if ($sku_services[$service_id]) {
                        foreach ($service['variants'] as $v) {
                            if (!isset($sku_services[$service_id][$v['id']]) || $sku_services[$service_id][$v['id']] === null) {
                                $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($v['price'], $service['currency'], $sku_price, $product['currency']));
                            } elseif ($sku_services[$service_id][$v['id']]) {
                                $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($sku_services[$service_id][$v['id']], $service['currency'], $sku_price, $product['currency']));
                            }
                        }
                    }
                } else {
                    foreach ($service['variants'] as $v) {
                        $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($v['price'], $service['currency'], $sku_price, $product['currency']));
                    }
                }
            }
        }
        unset($sku_services);
        // disable service if all variants disabled
        foreach ($skus_services as $sku_id => $sku_services) {
            foreach ($sku_services as $service_id => $service) {
                if (is_array($service)) {
                    $disabled = true;
                    foreach ($service as $v) {
                        if ($v !== false) {
                            $disabled = false;
                            break;
                        }
                    }
                    if ($disabled) {
                        $skus_services[$sku_id][$service_id] = false;
                    }
                }
            }
        }
        foreach ($services as $s_id => &$s) {
            if (!$s['variants']) {
                unset($services[$s_id]);
                continue;
            }
            if ($s['currency'] == '%') {
                foreach ($s['variants'] as $v_id => $v) {
                    $s['variants'][$v_id]['price'] = $v['price'] * $product['skus'][$product['sku_id']]['price'] / 100;
                }
                $s['currency'] = $product['currency'];
            }
            if (count($s['variants']) == 1) {
                $v = reset($s['variants']);
                if ($v['name']) {
                    $s['name'] .= ' ' . $v['name'];
                }
                $s['variant_id'] = $v['id'];
                $s['price'] = $v['price'];
                unset($s['variants']);
                foreach ($skus_services as $sku_id => $sku_services) {
                    if (isset($sku_services[$s_id]) && isset($sku_services[$s_id][$v['id']])) {
                        $skus_services[$sku_id][$s_id] = $sku_services[$s_id][$v['id']][1];
                    }
                }
            }
        }
        unset($s);
        uasort($services, array('shopServiceModel', 'sortServices'));
        $this->view->assign('sku_services', $skus_services);
        $this->view->assign('services', $services);
        $compare = waRequest::cookie('shop_compare', array(), waRequest::TYPE_ARRAY_INT);
        $this->view->assign('compare', in_array($product['id'], $compare) ? $compare : array());
        if (!$is_cart) {
            $this->view->assign('reviews', $this->getTopReviews($product['id']));
            $this->view->assign('rates', $this->reviews_model->getProductRates($product['id']));
            $this->view->assign('reviews_total_count', $this->getReviewsTotalCount($product['id']));
            $meta_fields = $this->getMetafields($product);
            $title = $meta_fields['meta_title'] ? $meta_fields['meta_title'] : $product['name'];
            wa()->getResponse()->setTitle($title);
            wa()->getResponse()->setMeta('keywords', $meta_fields['meta_keywords']);
            wa()->getResponse()->setMeta('description', $meta_fields['meta_description']);
            $feature_codes = array_keys($product->features);
            $feature_model = new shopFeatureModel();
            $features = $feature_model->getByCode($feature_codes);
            $this->view->assign('features', $features);
        }
        $this->view->assign('currency_info', $this->getCurrencyInfo());
        /**
         * @event frontend_product
         * @param shopProduct $product
         * @return array[string][string]string $return[%plugin_id%]['menu'] html output
         * @return array[string][string]string $return[%plugin_id%]['cart'] html output
         * @return array[string][string]string $return[%plugin_id%]['block_aux'] html output
         * @return array[string][string]string $return[%plugin_id%]['block'] html output
         */
        $this->view->assign('frontend_product', wa()->event('frontend_product', $product, array('menu', 'cart', 'block_aux', 'block')));
        $sku_stocks = array();
        foreach ($product->skus as $sku) {
            $sku_stocks[$sku_id] = array($sku['count'], $sku['stock']);
        }
        $stock_model = new shopStockModel();
        $this->view->assign('stocks', $stock_model->getAll('id'));
        $duble = db_query("SELECT * FROM shop_product where id=" . $product['id']);
        $dubles = mysql_fetch_assoc($duble);
        $dubles_sku = db_query("SELECT * FROM shop_product_skus where product_id=" . $product['id'] . " and  id=" . $dubles['sku_id']);
        $dubl_sku = mysql_fetch_assoc($dubles_sku);
        $dubles_prod = db_query("SELECT * FROM shop_product_skus where product_id!=" . $product['id'] . " and  sku='" . $dubl_sku['sku'] . "' Group by product_id");
        while ($dubl_product = mysql_fetch_array($dubles_prod)) {
            $dubles_img_pr = db_query("SELECT * FROM shop_product p, shop_product_images img where p.id=" . $dubl_product['product_id'] . " and img.product_id=" . $dubl_product['product_id'] . "");
            $dubles_img = mysql_fetch_assoc($dubles_img_pr);
            if ($dubles_img['status'] != 0) {
                $koldz = substr($dubles_img['product_id'], -2, 2);
                $kolds = str_replace($koldz, '', $dubles_img['product_id']);
                if (preg_match_all("#\\d#", $kolds) < 2) {
                    $kolds = '0' . $kolds;
                }
                if ($kolds == 0) {
                    $kolds = '00';
                }
                $alt_test = db_query("SELECT * FROM shop_product_features_selectable where feature_id=12 and  product_id=" . $dubles_img['product_id'] . "");
                $alt_t = mysql_fetch_assoc($alt_test);
                $alt_value = db_query("SELECT * FROM shop_feature_values_color where id=" . $alt_t['value_id'] . "");
                $alt = mysql_fetch_assoc($alt_value);
                $dubl_p = $dubl_p . ' <a  class="duble_prod"  href="http://' . $_SERVER['HTTP_HOST'] . '/index.php/' . $dubles_img['url'] . '" >
		 
		 
		 <img alt="' . $alt['value'] . '" title="' . $alt['value'] . '"  style="width:50px" src="/wa-data/public/shop/products/' . $koldz . '/' . $kolds . '/' . $dubles_img['product_id'] . '/images/' . $dubles_img['id'] . '/' . $dubles_img['id'] . '.96x96.jpg"/></a>';
                $dubl_p2 = $dubl_p2 . '<a onclick="doubl(' . $dubles_img['product_id'] . ')" class="duble_prod" >
		 
		 
		 <img alt="' . $alt['value'] . '" title="' . $alt['value'] . '" style="width:50px" src="/wa-data/public/shop/products/' . $koldz . '/' . $kolds . '/' . $dubles_img['product_id'] . '/images/' . $dubles_img['id'] . '/' . $dubles_img['id'] . '.96x96.jpg"/></a>';
            }
            $dubl_p = $dubl_p;
            $dubl_p2 = $dubl_p2;
        }
        $this->view->assign('duble', $dubl_p);
        $this->view->assign('duble2', $dubl_p2);
        $this->view->assign('duble_list', $dubl_p);
        $product['num'] = $_GET['num'];
        $this->view->assign('numis', $_GET['num']);
        $this->setThemeTemplate($is_cart ? 'product.cart.html' : 'product.html');
    }
예제 #11
0
<?php

$model = new waModel();
$service_variants_model = new shopServiceVariantsModel();
$product_services_model = new shopProductServicesModel();
// one service - at least one variant
$sql = "SELECT s.* FROM `shop_service` s\n            LEFT JOIN `shop_service_variants` sv ON s.id = sv.service_id\n        WHERE sv.id IS NULL";
foreach ($model->query($sql) as $service) {
    $id = $service_variants_model->insert(array('service_id' => $service['id'], 'name' => '', 'price' => $service['price'], 'primary_price' => $service['primary_price']));
}
// default variant_id
$model->exec("\n    UPDATE `shop_service` s JOIN `shop_service_variants` sv ON s.id = sv.service_id\n    SET s.variant_id = sv.id\n    WHERE s.variant_id IS NULL\n");
// one service - at least one variant, so correct shop_product_services
$model->exec("\n    UPDATE `shop_product_services` ps JOIN `shop_service` s ON s.id = ps.service_id\n    SET ps.service_variant_id = s.variant_id\n    WHERE ps.service_variant_id IS NULL\n");
foreach ($product_services_model->query($sql) as $item) {
    $product_services_model->updateById($item['id'], array('status' => (int) ($item['status'] > 0)));
}
$model->exec("\n    ALTER TABLE `shop_product_services` CHANGE service_variant_id service_variant_id INT (11) NOT NULL\n");
$model->exec("\n    ALTER TABLE `shop_service` CHANGE variant_id variant_id INT(11) NOT NULL\n");
$model->exec("\n    ALTER TABLE `shop_service_variants` CHANGE price price decimal(15,4) NOT NULL DEFAULT 0\n");
$model->exec("\n    ALTER TABLE `shop_service_variants` CHANGE primary_price primary_price decimal(15,4) NOT NULL DEFAULT 0\n");
try {
    $sql = "UPDATE `shop_service` SET price = primary_price";
    $model->exec($sql);
    $model->exec("\n        ALTER TABLE `shop_service` DROP primary_price\n    ");
} catch (waDbException $e) {
}
$sql = "UPDATE `shop_service` s\n        JOIN `shop_service_variants` sv ON s.id = sv.service_id AND s.variant_id = sv.id\n        SET s.price = sv.primary_price\n        WHERE s.price != sv.primary_price";
$model->exec($sql);
$sql = "UPDATE `shop_order_items` oi\n        JOIN `shop_service` s ON oi.service_id = s.id\n        SET oi.service_variant_id = s.variant_id\n        WHERE oi.type = 'service' AND service_variant_id IS NULL";
$model->exec($sql);
예제 #12
0
 /**
  * Returns total cost of current shopping cart's item with specified id, expressed in default currency.
  * 
  * @param int|array $item_id Item id or item data array.
  * @return float
  */
 public function getItemTotal($item_id)
 {
     if (is_array($item_id)) {
         $item = $item_id;
     } else {
         $item = $this->getItem($item_id);
     }
     $cart_items_model = new shopCartItemsModel();
     $items = $cart_items_model->getByField('parent_id', $item['id'], true);
     $price = shop_currency($item['price'] * $item['quantity'], $item['currency'], null, false);
     if (!$items) {
         return $price;
     }
     $variants = array();
     foreach ($items as $s) {
         $variants[] = $s['service_variant_id'];
     }
     $product_services_model = new shopProductServicesModel();
     $sql = "SELECT v.id, s.currency, ps.sku_id, ps.price, v.price base_price FROM shop_service_variants v\n                LEFT JOIN shop_product_services ps ON\n                v.id = ps.service_variant_id AND ps.product_id = i:0 AND (ps.sku_id = i:1 OR ps.sku_id IS NULL)\n                JOIN shop_service s ON v.service_id = s.id\n                WHERE v.id IN (i:2)\n                ORDER BY ps.sku_id";
     $rows = $product_services_model->query($sql, $item['product_id'], $item['sku_id'], $variants)->fetchAll();
     $prices = array();
     foreach ($rows as $row) {
         if (!isset($prices[$row['id']]) || $row['price']) {
             if ($row['price'] === null) {
                 $row['price'] = $row['base_price'];
             }
             $prices[$row['id']] = $row;
         }
     }
     foreach ($items as $s) {
         $v = $prices[$s['service_variant_id']];
         if ($v['currency'] == '%') {
             $v['price'] = $v['price'] * $item['price'] / 100;
             $v['currency'] = $item['currency'];
         }
         $price += shop_currency($v['price'] * $item['quantity'], $v['currency'], null, false);
     }
     return $price;
 }
 protected function getServiceVars($product)
 {
     $type_services_model = new shopTypeServicesModel();
     $services = $type_services_model->getServiceIds($product['type_id']);
     // Fetch services
     $service_model = new shopServiceModel();
     $product_services_model = new shopProductServicesModel();
     $services = array_merge($services, $product_services_model->getServiceIds($product['id']));
     $services = array_unique($services);
     $services = $service_model->getById($services);
     shopRounding::roundServices($services);
     // Convert service.price from default currency to service.currency
     foreach ($services as &$s) {
         $s['price'] = shop_currency($s['price'], null, $s['currency'], false);
     }
     unset($s);
     // Fetch service variants
     $variants_model = new shopServiceVariantsModel();
     $rows = $variants_model->getByField('service_id', array_keys($services), true);
     shopRounding::roundServiceVariants($rows, $services);
     foreach ($rows as $row) {
         if (!$row['price']) {
             $row['price'] = $services[$row['service_id']]['price'];
         } else {
             if ($services[$row['service_id']]['variant_id'] == $row['id']) {
                 $services[$row['service_id']]['price'] = $row['price'];
             }
         }
         $services[$row['service_id']]['variants'][$row['id']] = $row;
     }
     // Fetch service prices for specific products and skus
     $rows = $product_services_model->getByField('product_id', $product['id'], true);
     shopRounding::roundServiceVariants($rows, $services);
     $skus_services = array();
     // sku_id => [service_id => price]
     $frontend_currency = wa('shop')->getConfig()->getCurrency(false);
     foreach ($product['skus'] as $sku) {
         $skus_services[$sku['id']] = array();
     }
     foreach ($rows as $row) {
         if (!$row['sku_id']) {
             if (!$row['status']) {
                 // remove disabled services and variants
                 unset($services[$row['service_id']]['variants'][$row['service_variant_id']]);
             } elseif ($row['price'] !== null) {
                 // update price for service variant, when it is specified for this product
                 $services[$row['service_id']]['variants'][$row['service_variant_id']]['price'] = $row['price'];
                 // !!! also set other keys related to price
             }
             if ($row['status'] == shopProductServicesModel::STATUS_DEFAULT) {
                 // default variant is different for this product
                 $services[$row['service_id']]['variant_id'] = $row['service_variant_id'];
             }
         } else {
             if (!$row['status']) {
                 $skus_services[$row['sku_id']][$row['service_id']][$row['service_variant_id']] = false;
             } else {
                 $skus_services[$row['sku_id']][$row['service_id']][$row['service_variant_id']] = $row['price'];
             }
         }
     }
     // Fill in gaps in $skus_services
     foreach ($skus_services as $sku_id => &$sku_services) {
         $sku_price = $product['skus'][$sku_id]['price'];
         foreach ($services as $service_id => $service) {
             if (isset($sku_services[$service_id])) {
                 if ($sku_services[$service_id]) {
                     foreach ($service['variants'] as $v) {
                         if (!isset($sku_services[$service_id][$v['id']]) || $sku_services[$service_id][$v['id']] === null) {
                             $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($v['price'], $service['currency'], $sku_price, $product['currency']));
                         } elseif ($sku_services[$service_id][$v['id']]) {
                             $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($sku_services[$service_id][$v['id']], $service['currency'], $sku_price, $product['currency']));
                         }
                     }
                 }
             } else {
                 foreach ($service['variants'] as $v) {
                     $sku_services[$service_id][$v['id']] = array($v['name'], $this->getPrice($v['price'], $service['currency'], $sku_price, $product['currency']));
                 }
             }
         }
     }
     unset($sku_services);
     // disable service if all variants are disabled
     foreach ($skus_services as $sku_id => $sku_services) {
         foreach ($sku_services as $service_id => $service) {
             if (is_array($service)) {
                 $disabled = true;
                 foreach ($service as $v) {
                     if ($v !== false) {
                         $disabled = false;
                         break;
                     }
                 }
                 if ($disabled) {
                     $skus_services[$sku_id][$service_id] = false;
                 }
             }
         }
     }
     // Calculate prices for %-based services,
     // and disable variants selector when there's only one value available.
     foreach ($services as $s_id => &$s) {
         if (!$s['variants']) {
             unset($services[$s_id]);
             continue;
         }
         if ($s['currency'] == '%') {
             foreach ($s['variants'] as $v_id => $v) {
                 $s['variants'][$v_id]['price'] = $v['price'] * $product['skus'][$product['sku_id']]['price'] / 100;
             }
             $s['currency'] = $product['currency'];
         }
         if (count($s['variants']) == 1) {
             $v = reset($s['variants']);
             if ($v['name']) {
                 $s['name'] .= ' ' . $v['name'];
             }
             $s['variant_id'] = $v['id'];
             $s['price'] = $v['price'];
             unset($s['variants']);
             foreach ($skus_services as $sku_id => $sku_services) {
                 if (isset($sku_services[$s_id]) && isset($sku_services[$s_id][$v['id']])) {
                     $skus_services[$sku_id][$s_id] = $sku_services[$s_id][$v['id']][1];
                 }
             }
         }
     }
     unset($s);
     uasort($services, array('shopServiceModel', 'sortServices'));
     return array($services, $skus_services);
 }
예제 #14
0
 private function updateProducts($service_id, $products)
 {
     $variants_model = new shopServiceVariantsModel();
     $variants = array_keys($variants_model->getByField('service_id', $service_id, 'id'));
     $model = new shopProductServicesModel();
     $old_data = array();
     foreach ($this->query("\n            SELECT * FROM `" . $model->getTableName() . "`\n            WHERE service_id = {$service_id} AND sku_id IS NULL\n            ORDER BY `service_id`, `service_variant_id`\n        ") as $item) {
         $old_data[$item['product_id']][$item['service_variant_id']] = $item;
     }
     $add = array();
     foreach ($products as $product_id) {
         foreach ($variants as $variant_id) {
             if (!isset($old_data[$product_id][$variant_id])) {
                 $add[] = array('product_id' => $product_id, 'service_id' => $service_id, 'service_variant_id' => $variant_id);
             }
         }
     }
     if (!empty($add)) {
         $model->multipleInsert($add);
     }
     /*
     $delete = array(
     'service_variant_id' => array()
     );
     
     foreach ($old_data as $product_id => $items) {
     foreach ($items as $variant_id => $item) {
     $delete['service_variant_id'][] = $variant_id;
     }
     }
     
     if ($delete['product_id'] && $delete['service_variant_id']) {
     $where = $model->getWhereByField(array('product_id' => $delete['product_id'], 'service_id' => $service_id));
     if ($where) {
     $model->query("DELETE FROM `".$model->getTableName()."` WHERE $where");
     }
     }
     */
 }
 /**
  * @param $type_ids
  * @param $product_ids
  * @param $sku_ids
  * @return array
  */
 private function getServiceIds($type_ids, $product_ids, $sku_ids)
 {
     $service_ids = array();
     $type_services_model = new shopTypeServicesModel();
     $rows = $type_services_model->getByField('type_id', $type_ids, true);
     $type_services = array();
     foreach ($rows as $row) {
         $service_ids[] = $row['service_id'];
         $type_services[$row['type_id']][$row['service_id']] = true;
     }
     // get services for all products
     $product_services_model = new shopProductServicesModel();
     $rows = $product_services_model->getByProducts($product_ids);
     $product_services = $sku_services = array();
     foreach ($rows as $row) {
         if ($row['sku_id'] && !in_array($row['sku_id'], $sku_ids)) {
             continue;
         }
         $service_ids[] = $row['service_id'];
         if (!$row['sku_id']) {
             $product_services[$row['product_id']][$row['service_id']]['variants'][$row['service_variant_id']] = $row;
         }
         if ($row['sku_id']) {
             $sku_services[$row['sku_id']][$row['service_id']]['variants'][$row['service_variant_id']] = $row;
         }
     }
     $service_ids = array_unique($service_ids);
     return array($service_ids, $type_services, $product_services, $sku_services);
 }
예제 #16
0
 public function execute()
 {
     $product = new shopProduct(waRequest::get('id', 0, waRequest::TYPE_INT));
     if (!$product->id) {
         if (waRequest::get('id') == 'new') {
             $product->name = '';
             $product->id = 'new';
             $product->status = 1;
         } else {
             throw new waException("Product not found", 404);
         }
     }
     $counters = array('reviews' => 0, 'images' => 0, 'pages' => 0, 'services' => 0);
     $sidebar_counters = array();
     $config = $this->getConfig();
     /**
      * @var shopConfig $config
      */
     #load product types
     $type_model = new shopTypeModel();
     $product_types = $type_model->getTypes(true);
     $product_types_count = count($product_types);
     if (intval($product->id)) {
         # 1 fill extra product data
         # 1.1 fill product reviews
         $product_reviews_model = new shopProductReviewsModel();
         $product['reviews'] = $product_reviews_model->getReviews($product->id, 0, $config->getOption('reviews_per_page_product'), 'datetime DESC', array('is_new' => true));
         $counters['reviews'] = $product_reviews_model->count($product->id);
         $sidebar_counters['reviews'] = array('new' => $product_reviews_model->countNew());
         $counters['images'] = count($product['images']);
         $product_pages_model = new shopProductPagesModel();
         $counters['pages'] = $product_pages_model->count($product->id);
         $product_services_model = new shopProductServicesModel();
         $counters['services'] = $product_services_model->countServices($product->id);
         $product_stocks_log_model = new shopProductStocksLogModel();
         $counters['stocks_log'] = $product_stocks_log_model->countByField('product_id', $product->id);
         $this->view->assign('edit_rights', $product->checkRights());
     } else {
         $counters += array_fill_keys(array('images', 'services', 'pages', 'reviews'), 0);
         $product['images'] = array();
         reset($product_types);
         $product->type_id = 0;
         if ($product_types_count) {
             if (!$product_types) {
                 throw new waRightsException(_w("Access denied"));
             } else {
                 reset($product_types);
                 $product->type_id = key($product_types);
             }
         } elseif (!$product->checkRights()) {
             throw new waRightsException(_w("Access denied"));
         }
         $this->view->assign('edit_rights', true);
         $product['skus'] = array('-1' => array('id' => -1, 'sku' => '', 'available' => 1, 'name' => '', 'price' => 0.0, 'purchase_price' => 0.0, 'count' => null, 'stock' => array(), 'virtual' => 0));
         $product->currency = $config->getCurrency();
     }
     $this->assignReportsData($product);
     $stock_model = new shopStockModel();
     $taxes_mode = new shopTaxModel();
     $this->view->assign('stocks', $stock_model->getAll('id'));
     $this->view->assign(array('use_product_currency' => wa()->getSetting('use_product_currency'), 'currencies' => $this->getCurrencies(), 'primary_currency' => $config->getCurrency(), 'taxes' => $taxes_mode->getAll()));
     $category_model = new shopCategoryModel();
     $categories = $category_model->getFullTree('id, name, depth, url, full_url, parent_id', true);
     $frontend_urls = array();
     if (intval($product->id)) {
         $routing = wa()->getRouting();
         $domain_routes = $routing->getByApp($this->getAppId());
         foreach ($domain_routes as $domain => $routes) {
             foreach ($routes as $r) {
                 if (!empty($r['private'])) {
                     continue;
                 }
                 if (empty($r['type_id']) || in_array($product->type_id, (array) $r['type_id'])) {
                     $routing->setRoute($r, $domain);
                     $params = array('product_url' => $product->url);
                     if ($product->category_id && isset($categories[$product->category_id])) {
                         if (!empty($r['url_type']) && $r['url_type'] == 1) {
                             $params['category_url'] = $categories[$product->category_id]['url'];
                         } else {
                             $params['category_url'] = $categories[$product->category_id]['full_url'];
                         }
                     }
                     $frontend_url = $routing->getUrl('/frontend/product', $params, true);
                     $frontend_urls[] = array('url' => $frontend_url);
                 }
             }
         }
     } else {
         $frontend_urls[] = array('url' => wa()->getRouteUrl('/frontend/product', array('product_url' => '%product_url%'), true));
     }
     $stuff = intval($product->id) ? $product->url : '%product_url%';
     foreach ($frontend_urls as &$frontend_url) {
         $pos = strrpos($frontend_url['url'], $stuff);
         $frontend_url['base'] = $pos !== false ? rtrim(substr($frontend_url['url'], 0, $pos), '/') . '/' : $frontend_url['url'];
     }
     unset($frontend_url);
     $product_model = new shopProductModel();
     $this->view->assign('storefront_map', $product_model->getStorefrontMap($product->id));
     /**
      * Backend product profile page
      * UI hook allow extends product profile page
      * @event backend_product
      * @param shopProduct $entry
      * @return array[string][string]string $return[%plugin_id%]['title_suffix'] html output
      * @return array[string][string]string $return[%plugin_id%]['action_button'] html output
      * @return array[string][string]string $return[%plugin_id%]['toolbar_section'] html output
      * @return array[string][string]string $return[%plugin_id%]['image_li'] html output
      */
     $this->view->assign('backend_product', wa()->event('backend_product', $product));
     /**
      * @event backend_product_edit
      */
     $this->view->assign('backend_product_edit', wa()->event('backend_product_edit', $product));
     $this->view->assign('categories', $categories);
     $this->view->assign('counters', $counters);
     $this->view->assign('product', $product);
     $this->view->assign('current_author', shopProductReviewsModel::getAuthorInfo(wa()->getUser()->getId()));
     $this->view->assign('reply_allowed', true);
     $this->view->assign('review_allowed', true);
     $this->view->assign('sidebar_counters', $sidebar_counters);
     $this->view->assign('lang', substr(wa()->getLocale(), 0, 2));
     $this->view->assign('frontend_urls', $frontend_urls);
     $tag_model = new shopTagModel();
     $this->view->assign('popular_tags', $tag_model->popularTags());
     $counts = array();
     // Selectable features
     $features_selectable = $product->features_selectable;
     if (is_array($features_selectable)) {
         foreach ($features_selectable as $f) {
             if ($f['selected']) {
                 $counts[] = $f['selected'];
             }
         }
     }
     $feature_model = new shopTypeFeaturesModel();
     $features_selectable_types = $feature_model->getSkuTypeSelectableTypes();
     foreach ($product_types as $type_id => &$type) {
         $type['sku_type'] = empty($features_selectable_types[$type_id]) ? shopProductModel::SKU_TYPE_FLAT : shopProductModel::SKU_TYPE_SELECTABLE;
     }
     $this->view->assign('features', $features_selectable);
     $this->view->assign('duble', '???');
     $this->view->assign('features_counts', $counts);
     #load product types
     $this->view->assign('product_types', $product_types);
     $this->view->assign('sidebar_width', $config->getSidebarWidth());
 }