public function getProductSkus() { static $data = null; if ($data === null) { $product_skus_model = new shopProductSkusModel(); $data = $product_skus_model->getByField('product_id', $this->product_id, 'id'); } return $data; }
/** * * @param array $data * @return shopProduct */ private function findProduct(&$data) { static $currencies; static $model; static $sku_model; /** * @var shopTypeFeaturesModel $type_features_model */ static $type_features_model; if (empty($model)) { $model = new shopProductModel(); } if (empty($currencies)) { $currencies = array(); $config = wa()->getConfig(); /** * @var shopConfig $config */ $c = $config->getCurrency(); $currencies[$c] = $c; foreach ($config->getCurrencies() as $row) { $currencies[$row['code']] = $row['code']; } } if (!empty($data['skus'][-1]['stock'])) { $per_stock = false; $stock =& $data['skus'][-1]['stock']; foreach ($stock as $id => &$count) { if ($count === '') { $count = null; } else { $count = intval($count); if ($id) { $per_stock = true; } } } unset($count); if ($per_stock) { if (isset($stock[0])) { unset($stock[0]); } } else { $count = ifset($stock[0]); $stock = array(0 => $count); } unset($stock); } $stack = ifset($this->data['map'][self::STAGE_CATEGORY], array()); $category_id = end($stack); if (!$category_id) { $category_id = null; } $primary = $this->data['primary']; $fields = false; if (empty($primary)) { $keys = explode(':', $this->data['secondary']); if (empty($sku_model)) { $sku_model = new shopProductSkusModel(); } $sku_fields = array(end($keys) => self::getData($data, $keys)); //hack for empty SKU code ??? if (false && reset($sku_fields) === '' && $this->data['extra_secondary']) { $extra_keys = explode(':', $this->data['extra_secondary']); $sku_fields[end($extra_keys)] = self::getData($data, $this->data['extra_secondary']); } if ($sku = $sku_model->getByField($sku_fields)) { $fields = array('category_id' => $category_id, 'id' => $sku['product_id']); } } elseif (!empty($primary)) { $fields = array('category_id' => $category_id, $primary => ifset($data[$primary], '')); } if ($fields && $this->data['ignore_category']) { unset($fields['category_id']); } $key = 'p'; if ($fields && ($current_data = $model->getByField($fields))) { $product = new shopProduct($current_data['id']); $data['type_id'] = ifempty($current_data['type_id'], $this->data['type_id']); if (!empty($current_data['tax_id'])) { $data['tax_id'] = $current_data['tax_id']; } if (isset($data['currency']) && !isset($currencies[$data['currency']])) { $this->data['processed_count'][self::STAGE_PRODUCT]['currency']++; $data['currency'] = reset($currencies); } if (!empty($data['skus'])) { $data['sku_id'] = ifempty($current_data['sku_id'], -1); } foreach ($product->skus as $sku_id => $current_sku) { if (empty($data['skus'][$sku_id])) { if (!count($current_sku['stock']) && $current_sku['count'] !== null) { $current_sku['stock'][0] = $current_sku['count']; } $data['skus'][$sku_id] = $current_sku; } } $key .= ':u:' . $product->getId(); } else { $product = new shopProduct(); if ($category_id) { $data['categories'] = array($category_id); } $data['currency'] = ifempty($data['currency'], reset($currencies)); if (!isset($currencies[$data['currency']])) { $this->data['processed_count'][self::STAGE_PRODUCT]['currency']++; $data['currency'] = reset($currencies); } if (!empty($data['skus'])) { $sku = reset($data['skus']); $data['sku_id'] = key($data['skus']); if (!isset($sku['available'])) { $sku['available'] = true; $data['skus'][$data['sku_id']] = $sku; } } $key .= ':i:' . $this->getKey($fields); } if (!empty($data['features'])) { foreach ($data['features'] as $feature => &$values) { if (is_array($values)) { } elseif (preg_match('/^<\\{(.*)\\}>$/', $values, $matches)) { if (!isset($data['features_selectable'])) { $data['features_selectable'] = array(); } if ($values = explode(',', $matches[1])) { foreach ($values as &$value) { if (preg_match('@^(.+)=([\\+\\-]?(\\d+|\\.\\d+|\\d\\.\\d))$@', $value, $matches)) { $value = array('value' => trim($matches[1]), 'price' => $matches[2]); } else { $value = array('value' => trim($value)); } unset($value); } $data['features_selectable'][$feature] = array('values' => $values); if (!empty($this->data['virtual_sku_stock']) && isset($data['skus'][-1]['stock'])) { $stock = $data['skus'][-1]['stock']; switch ($this->data['virtual_sku_stock']) { case 'distribute': if (is_array($stock)) { foreach ($stock as &$stock_item) { $stock_item = $stock_item / count($values); unset($stock_item); } } else { $stock = $stock / count($values); } $data['features_selectable'][$feature]['stock'] = $stock; break; case 'set': $data['features_selectable'][$feature]['stock'] = $stock; break; } } $product->sku_type = shopProductModel::SKU_TYPE_SELECTABLE; if (isset($data['skus'][-1])) { if (!isset($data['base_price_selectable'])) { $data['base_price_selectable'] = ifset($data['skus'][-1]['price']); } if (!isset($data['purchase_price_selectable'])) { $data['purchase_price_selectable'] = ifset($data['skus'][-1]['purchase_price']); } if (!isset($data['compare_price_selectable'])) { $data['compare_price_selectable'] = ifset($data['skus'][-1]['compare_price']); } } unset($data['skus']); } unset($data['features'][$feature]); } elseif (preg_match('/^\\{(.*)\\}$/', $values, $matches)) { $values = explode(',', $matches[1]); } } unset($values); } $this->findTax($data); $access = $this->findType($data); if ($access) { $access = !$product->type_id || in_array($product->type_id, $this->data['types']); } if ($access) { $product->__hash = $key; foreach ($this->data['new_features'] as $code => &$feature) { if (isset($data['features'][$code]) || isset($data['features_selectable'][$code])) { if ($data['type_id'] && !in_array($data['type_id'], $feature['types'])) { if (empty($type_features_model)) { $type_features_model = new shopTypeFeaturesModel(); } $type_features_model->updateByFeature($feature['id'], array($data['type_id']), false); $feature['types'][] = $data['type_id']; } } unset($feature); } } return $access ? $product : null; }
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; }
public static function checkCart(&$cart = null) { $error = false; $cart = new shopCart(); $code = $cart->getCode(); $view = wa()->getView(); if (!wa()->getSetting('ignore_stock_count')) { $cart_model = new shopCartItemsModel(); $sku_model = new shopProductSkusModel(); $product_model = new shopProductModel(); $items = $cart->items(false); foreach ($items as &$item) { if (!isset($item['product_id'])) { $sku = $sku_model->getById($item['sku_id']); $product = $product_model->getById($sku['product_id']); } else { $product = $product_model->getById($item['product_id']); if (isset($item['sku_id'])) { $sku = $sku_model->getById($item['sku_id']); } else { if (isset($item['features'])) { $product_features_model = new shopProductFeaturesModel(); $sku_id = $product_features_model->getSkuByFeatures($product['id'], $item['features']); if ($sku_id) { $sku = $sku_model->getById($sku_id); } else { $sku = null; } } else { $sku = $sku_model->getById($product['sku_id']); if (!$sku['available']) { $sku = $sku_model->getByField(array('product_id' => $product['id'], 'available' => 1)); } if (!$sku) { $item['error'] = _w('This product is not available for purchase'); $error = true; } } } } $quantity = $item['quantity']; $c = $cart_model->countSku($code, $sku['id']); if ($sku['count'] !== null && $c + $quantity > $sku['count']) { $quantity = $sku['count'] - $c; $name = $product['name'] . ($sku['name'] ? ' (' . $sku['name'] . ')' : ''); if ($quantity < 0) { $item['error'] = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $sku['count'], $name); $error = true; } } } unset($item); 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; } $cart = array('items' => $items, 'total' => $cart->total(false), 'count' => $cart->count()); } return $error; }
public function getSkus($sku_ids) { if (!$sku_ids) { return array(); } $model = new shopProductSkusModel(); return $model->getByField('id', $sku_ids, 'id'); }
public function execute() { $code = waRequest::cookie('shop_cart'); if (!$code) { $code = md5(uniqid(time(), true)); // header for IE wa()->getResponse()->addHeader('P3P', 'CP="NOI ADM DEV COM NAV OUR STP"'); // set cart cookie wa()->getResponse()->setCookie('shop_cart', $code, time() + 30 * 86400, null, '', false, true); } $this->cart = new shopCart($code); $this->cart_model = new shopCartItemsModel(); $data = waRequest::post(); $this->is_html = waRequest::request('html'); // add service if (isset($data['parent_id'])) { $this->addService($data); return; } // add sku $sku_model = new shopProductSkusModel(); $product_model = new shopProductModel(); if (!isset($data['product_id'])) { $sku = $sku_model->getById($data['sku_id']); $product = $product_model->getById($sku['product_id']); } else { $product = $product_model->getById($data['product_id']); if (isset($data['sku_id'])) { $sku = $sku_model->getById($data['sku_id']); } else { if (isset($data['features'])) { $product_features_model = new shopProductFeaturesModel(); $sku_id = $product_features_model->getSkuByFeatures($product['id'], $data['features']); if ($sku_id) { $sku = $sku_model->getById($sku_id); } else { $sku = null; } } else { $sku = $sku_model->getById($product['sku_id']); if (!$sku['available']) { $sku = $sku_model->getByField(array('product_id' => $product['id'], 'available' => 1)); } if (!$sku) { $this->errors = _w('This product is not available for purchase'); return; } } } } $quantity = waRequest::post('quantity', 1); if ($product && $sku) { // check quantity if (!wa()->getSetting('ignore_stock_count')) { $c = $this->cart_model->countSku($code, $sku['id']); if ($sku['count'] !== null && $c + $quantity > $sku['count']) { $quantity = $sku['count'] - $c; $name = $product['name'] . ($sku['name'] ? ' (' . $sku['name'] . ')' : ''); if (!$quantity) { $this->errors = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $sku['count'], $name); return; } else { $this->response['error'] = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $sku['count'], $name); } } } $services = waRequest::post('services', array()); if ($services) { $variants = waRequest::post('service_variant'); $temp = array(); $service_ids = array(); foreach ($services as $service_id) { if (isset($variants[$service_id])) { $temp[$service_id] = $variants[$service_id]; } else { $service_ids[] = $service_id; } } if ($service_ids) { $service_model = new shopServiceModel(); $temp_services = $service_model->getById($service_ids); foreach ($temp_services as $row) { $temp[$row['id']] = $row['variant_id']; } } $services = $temp; } $item_id = null; $item = $this->cart_model->getItemByProductAndServices($code, $product['id'], $sku['id'], $services); if ($item) { $item_id = $item['id']; $this->cart->setQuantity($item_id, $item['quantity'] + $quantity); } if (!$item_id) { $data = array('create_datetime' => date('Y-m-d H:i:s'), 'product_id' => $product['id'], 'sku_id' => $sku['id'], 'quantity' => $quantity, 'type' => 'product'); if ($services) { $data_services = array(); foreach ($services as $service_id => $variant_id) { $data_services[] = array('service_id' => $service_id, 'service_variant_id' => $variant_id); } } else { $data_services = array(); } $item_id = $this->cart->addItem($data, $data_services); } if (waRequest::isXMLHttpRequest()) { $this->response['item_id'] = $item_id; $this->response['total'] = $this->currencyFormat($this->cart->total()); $this->response['discount'] = $this->currencyFormat($this->cart->discount()); $this->response['count'] = $this->cart->count(); } else { $this->redirect(waRequest::server('HTTP_REFERER')); } } else { throw new waException('product not found'); } }
public function productsAutocomplete($q, $limit = null) { $limit = $limit !== null ? $limit : $this->limit; $product_model = new shopProductModel(); $q = $product_model->escape($q, 'like'); $fields = 'id, name AS value, price, count, sku_id'; $products = $product_model->select($fields)->where("name LIKE '{$q}%'")->limit($limit)->fetchAll('id'); $count = count($products); if ($count < $limit) { $product_skus_model = new shopProductSkusModel(); $product_ids = array_keys($product_skus_model->select('id, product_id')->where("sku LIKE '{$q}%'")->limit($limit)->fetchAll('product_id')); if ($product_ids) { $data = $product_model->select($fields)->where('id IN (' . implode(',', $product_ids) . ')')->limit($limit - $count)->fetchAll('id'); // not array_merge, because it makes first reset numeric keys and then make merge $products = $products + $data; } } // try find with LIKE %query% if (!$products) { $products = $product_model->select($fields)->where("name LIKE '%{$q}%'")->limit($limit)->fetchAll(); } $currency = wa()->getConfig()->getCurrency(); foreach ($products as &$p) { $p['price_str'] = wa_currency($p['price'], $currency); } unset($p); if (waRequest::get('with_sku_name')) { $sku_ids = array(); foreach ($products as $p) { $sku_ids[] = $p['sku_id']; } $product_skus_model = new shopProductSkusModel(); $skus = $product_skus_model->getByField('id', $sku_ids, 'id'); $sku_names = array(); foreach ($skus as $sku_id => $sku) { $name = ''; if ($sku['name']) { $name = $sku['name']; if ($sku['sku']) { $name .= ' (' . $sku['sku'] . ')'; } } else { $name = $sku['sku']; } $sku_names[$sku_id] = $name; } foreach ($products as &$p) { $p['sku_name'] = $sku_names[$p['sku_id']]; } unset($p); } return array_values($products); }
public function getProductServiceFullInfo($product_id, $service_id = null) { $product = $this->getProduct($product_id); if (!$product) { return array(); } $services = $this->getServices($product, $service_id); if (!$services) { return array(); } if ($service_id) { $services = array($service_id => $services); } $service_ids = array_keys($services); $data = array(); $product_skus_model = new shopProductSkusModel(); $skus = $product_skus_model->getByField('product_id', $product_id, 'id'); $variants = $this->getVariants($product_id, $service_ids); foreach ($variants as $s_id => $service) { foreach ($service['variants'] as &$variant) { if ($variant['status'] === null) { $variant['status'] = $services[$s_id]['type_id'] ? self::STATUS_PERMITTED : self::STATUS_FORBIDDEN; } foreach ($skus as $sku_id => $sku) { $sk_item =& $variant['skus'][$sku_id]; if (empty($sk_item)) { $sk_item = array('id' => $variant['id'], 'sku_id' => $sku_id, 'price' => null, 'primary_price' => null, 'base_price' => $variant['base_price'], 'primary_base_price' => $variant['primary_base_price'], 'status' => self::STATUS_PERMITTED); } $sk_item['name'] = $sku['name']; // base_price on sku level is price on product level if ($variant['price'] !== null) { $sk_item['base_price'] = $variant['price']; $sk_item['primary_base_price'] = $variant['primary_base_price']; } if ($variant['status'] == self::STATUS_FORBIDDEN) { $sk_item['status'] = self::STATUS_FORBIDDEN; } unset($sk_item); } unset($variant); } $this->setDefaultVariant($service['variants'], $services[$s_id]['variant_id']); $data[$s_id] = $services[$s_id]; $data[$s_id]['variants'] = $service['variants']; } return $service_id ? $data[$service_id] : $data; }
public function execute() { $category = $this->getCategory(); $this->addCanonical(); // breadcrumbs $root_category_id = $category['id']; if ($category['parent_id']) { $breadcrumbs = array(); $path = array_reverse($this->getModel()->getPath($category['id'])); $root_category = reset($path); $root_category_id = $root_category['id']; foreach ($path as $row) { $breadcrumbs[] = array('url' => wa()->getRouteUrl('/frontend/category', array('category_url' => waRequest::param('url_type') == 1 ? $row['url'] : $row['full_url'])), 'name' => $row['name']); } if ($breadcrumbs) { $this->view->assign('breadcrumbs', $breadcrumbs); } } $this->view->assign('root_category_id', $root_category_id); // sort if ($category['type'] == shopCategoryModel::TYPE_DYNAMIC && !$category['sort_products']) { $category['sort_products'] = 'create_datetime DESC'; } if ($category['sort_products'] && !waRequest::get('sort')) { $sort = explode(' ', $category['sort_products']); $this->view->assign('active_sort', $sort[0] == 'count' ? 'stock' : $sort[0]); } elseif (!$category['sort_products'] && !waRequest::get('sort')) { $this->view->assign('active_sort', ''); } $this->view->assign('category', $category); // products $collection = new shopProductsCollection('category/' . $category['id']); // filters if ($category['filter']) { $filter_ids = explode(',', $category['filter']); $feature_model = new shopFeatureModel(); $features = $feature_model->getById(array_filter($filter_ids, 'is_numeric')); if ($features) { $features = $feature_model->getValues($features); } $category_value_ids = $collection->getFeatureValueIds(); $filters = array(); foreach ($filter_ids as $fid) { if ($fid == 'price') { $range = $collection->getPriceRange(); if ($range['min'] != $range['max']) { $filters['price'] = array('min' => shop_currency($range['min'], null, null, false), 'max' => shop_currency($range['max'], null, null, false)); } } elseif (isset($features[$fid]) && isset($category_value_ids[$fid])) { $filters[$fid] = $features[$fid]; $min = $max = $unit = null; foreach ($filters[$fid]['values'] as $v_id => $v) { if (!in_array($v_id, $category_value_ids[$fid])) { unset($filters[$fid]['values'][$v_id]); } else { if ($v instanceof shopRangeValue) { $begin = $this->getFeatureValue($v->begin); if ($min === null || $begin < $min) { $min = $begin; } $end = $this->getFeatureValue($v->end); if ($max === null || $end > $max) { $max = $end; if ($v->end instanceof shopDimensionValue) { $unit = $v->end->unit; } } } else { $tmp_v = $this->getFeatureValue($v); if ($min === null || $tmp_v < $min) { $min = $tmp_v; } if ($max === null || $tmp_v > $max) { $max = $tmp_v; if ($v instanceof shopDimensionValue) { $unit = $v->unit; } } } } } if (!$filters[$fid]['selectable'] && ($filters[$fid]['type'] == 'double' || substr($filters[$fid]['type'], 0, 6) == 'range.' || substr($filters[$fid]['type'], 0, 10) == 'dimension.')) { if ($min == $max) { unset($filters[$fid]); } else { $type = preg_replace('/^[^\\.]*\\./', '', $filters[$fid]['type']); if ($type != 'double') { $filters[$fid]['base_unit'] = shopDimension::getBaseUnit($type); $filters[$fid]['unit'] = shopDimension::getUnit($type, $unit); if ($filters[$fid]['base_unit']['value'] != $filters[$fid]['unit']['value']) { $dimension = shopDimension::getInstance(); $min = $dimension->convert($min, $type, $filters[$fid]['unit']['value']); $max = $dimension->convert($max, $type, $filters[$fid]['unit']['value']); } } $filters[$fid]['min'] = $min; $filters[$fid]['max'] = $max; } } } } $this->view->assign('filters', $filters); $this->setCollection($collection); // fix prices $products = $this->view->getVars('products'); $product_ids = array(); foreach ($products as $p_id => $p) { if ($p['sku_count'] > 1) { $product_ids[] = $p_id; } } if ($product_ids) { $min_price = $max_price = null; $tmp = array(); foreach ($filters as $fid => $f) { if ($fid == 'price') { $min_price = waRequest::get('price_min'); if (!empty($min_price)) { $min_price = (double) $min_price; } else { $min_price = null; } $max_price = waRequest::get('price_max'); if (!empty($max_price)) { $max_price = (double) $max_price; } else { $max_price = null; } } else { $fvalues = waRequest::get($f['code']); if ($fvalues && !isset($fvalues['min']) && !isset($fvalues['max'])) { $tmp[$fid] = $fvalues; } } } $product_skus = array(); if ($tmp) { $pf_model = new shopProductFeaturesModel(); $product_skus = $pf_model->getSkusByFeatures($product_ids, $tmp); } elseif ($min_price || $max_price) { $ps_model = new shopProductSkusModel(); $rows = $ps_model->getByField('product_id', $product_ids, true); foreach ($rows as $row) { $product_skus[$row['product_id']][] = $row; } } $default_currency = $this->getConfig()->getCurrency(true); if ($product_skus) { foreach ($product_skus as $product_id => $skus) { $currency = $products[$product_id]['currency']; usort($skus, array($this, 'sortSkus')); $k = 0; if ($min_price || $max_price) { foreach ($skus as $i => $sku) { if ($min_price) { $tmp_price = shop_currency($min_price, true, $currency, false); if ($sku['price'] < $tmp_price) { continue; } } if ($max_price) { $tmp_price = shop_currency($max_price, true, $currency, false); if ($sku['price'] > $tmp_price) { continue; } } $k = $i; break; } } $sku = $skus[$k]; if ($products[$product_id]['sku_id'] != $sku['id']) { $products[$product_id]['sku_id'] = $sku['id']; $products[$product_id]['frontend_url'] .= '?sku=' . $sku['id']; $products[$product_id]['price'] = shop_currency($sku['price'], $currency, $default_currency, false); $products[$product_id]['compare_price'] = shop_currency($sku['compare_price'], $currency, $default_currency, false); } } $this->view->assign('products', $products); } } } else { $this->setCollection($collection); } //отображение дополнительных размеров $products = $this->view->getVars('products'); $product_features_model = new shopProductFeaturesSelectableModel(); foreach ($products as &$p) { $sku_features = $product_features_model->getByProduct($p['id']); $sizes = $sku_features[3]; if (!$sizes) { $p['sizes'] = array(); continue; } $pf_names = $product_features_model->query("SELECT `id`, `value` FROM shop_feature_values_varchar where `id` IN (" . implode(',', $sizes) . ') ORDER BY `sort`;')->fetchAll(); foreach ($pf_names as $key => $val) { $sizes[$val['id']] = $val['value']; } $p['sizes'] = $pf_names; } //отображение всех картинок foreach ($products as &$p) { $images_full = shopViewHelper::images($p['id']); if (isset($images_full[$p['id']])) { $p['image_ids'] = array_keys($images_full[$p['id']]); } } $this->view->assign('products', $products); // set meta $title = $category['meta_title'] ? $category['meta_title'] : $category['name']; wa()->getResponse()->setTitle($title); wa()->getResponse()->setMeta('keywords', $category['meta_keywords']); wa()->getResponse()->setMeta('description', $category['meta_description']); /** * @event frontend_category * @return array[string]string $return[%plugin_id%] html output for category */ $this->view->assign('frontend_category', wa()->event('frontend_category', $category)); $this->setThemeTemplate('category.html'); }
/** * @param $items * @param $sku_ids * @param $product_ids * @return array */ private function getTypeIds(&$items, $sku_ids, $product_ids) { $product_model = new shopProductModel(); $sku_model = new shopProductSkusModel(); $image_model = new shopProductImagesModel(); /** * @todo: $products = $product_model->getWithCategoryUrl($product_ids); * $products = $product_model->getById($product_ids); */ $products = $product_model->getWithCategoryUrl($product_ids); $skus = $sku_model->getByField('id', $sku_ids, 'id'); $type_ids = array(); foreach ($items as $item_id => &$item) { if (!isset($skus[$item['sku_id']])) { unset($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['currency'] = $item['product']['currency']; $type_ids[] = $item['product']['type_id']; } } return array_unique($type_ids); }
public function execute() { $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')); } } $this->setThemeTemplate('cart.html'); $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')); $this->getResponse()->setTitle(_w('Cart')); $checkout_flow = new shopCheckoutFlowModel(); $checkout_flow->add(array('code' => $code, 'step' => 0, 'description' => null)); }