protected function upsellingPrepare($product_id, $auto_title = false)
 {
     $model = $this->getModel();
     if (isset($this->options['product'])) {
         $product = $this->options['product'];
         $conditions = $this->options['conditions'];
     } else {
         $product = new shopProduct($product_id);
         $type_upselling_model = new shopTypeUpsellingModel();
         $conditions = $type_upselling_model->getByField('type_id', $product['type'], true);
     }
     $this->where[] = 'p.id != ' . (int) $product_id;
     $sum = array();
     foreach ($conditions as $row) {
         if ($row['feature'] == 'tag') {
             $tag_model = new shopTagModel();
             $tag = $tag_model->getByName($row['value']);
             if ($tag) {
                 $this->where[] = 'pt.tag_id = ' . (int) $tag['id'];
                 $this->joins[] = array('table' => 'shop_product_tags', 'alias' => 'pt');
             }
             continue;
         } elseif ($row['feature'] == 'type_id') {
             if ($row['cond'] == 'same') {
                 $this->where[] = 'p.type_id = ' . (int) $product['type_id'];
             } elseif ($row['cond'] == 'notsame') {
                 $this->where[] = 'p.type_id != ' . (int) $product['type_id'];
             } elseif ($row['cond'] == 'is') {
                 $this->where[] = 'p.type_id = ' . (int) $row['value'];
             }
             continue;
         }
         switch ($row['cond']) {
             case 'between':
                 list($min, $max) = explode(',', $row['value']);
                 if ($model->fieldExists($row['feature'])) {
                     $v = $product[$row['feature']];
                 } else {
                     $v = isset($product['features'][$row['feature']]) ? $product['features'][$row['feature']] : null;
                 }
                 if (!$v) {
                     continue;
                 }
                 $min = $v * (double) (100 + $min) / 100;
                 $max = $v * (double) (100 + $max) / 100;
                 $v = str_replace(',', '.', $v);
                 if ($model->fieldExists($row['feature'])) {
                     $this->where[] = 'p.' . $row['feature'] . ' > ' . str_replace(',', '.', $min);
                     $this->where[] = 'p.' . $row['feature'] . ' < ' . str_replace(',', '.', $max);
                     $sum[] = 'ABS(p.' . $row['feature'] . ' - ' . $v . ')/' . $v;
                 }
                 break;
             case 'is':
                 if ($model->fieldExists($row['feature'])) {
                     $this->where[] = 'p.' . $row['feature'] . " = '" . $model->escape($row['value']) . "'";
                 } else {
                     $this->addJoin('shop_product_features', null, ":table.feature_id = " . (int) $row['feature_id'] . " AND :table.feature_value_id = " . (int) $row['value']);
                     $this->group_by = 'p.id';
                 }
                 break;
             case 'any':
             case 'all':
                 if ($model->fieldExists($row['feature'])) {
                     //$this->where[] = 'p.'.$row['feture']." = '".$model->escape($row['value'])."'";
                 } else {
                     if ($row['value']) {
                         $this->addJoin('shop_product_features', null, ":table.feature_id = " . (int) $row['feature_id'] . " AND :table.feature_value_id IN (" . $row['value'] . ")");
                         $this->group_by = 'p.id';
                     } else {
                         $this->where[] = '0';
                     }
                 }
                 break;
             case 'notsame':
             case 'same':
                 if ($model->fieldExists($row['feature'])) {
                     $this->where[] = 'p.' . $row['feature'] . " " . ($row['cond'] == 'notsame' ? '!' : '') . "= '" . $model->escape($product->features[$row['feature']]) . "'";
                 } else {
                     $product_features_model = new shopProductFeaturesModel();
                     $rows = $product_features_model->getByField(array('product_id' => $product['id'], 'sku_id' => null, 'feature_id' => $row['feature_id']), true);
                     $values = array();
                     foreach ($rows as $r) {
                         $values[] = $r['feature_value_id'];
                     }
                     if ($values) {
                         $alias = $this->addJoin('shop_product_features');
                         $this->where[] = $alias . ".feature_id = " . $row['feature_id'];
                         $this->where[] = $alias . ".feature_value_id " . (count($values) == 1 ? ($row['cond'] == 'notsame' ? '!' : '') . "= " . $values[0] : ($row['cond'] == 'notsame' ? 'NOT ' : '') . "IN (" . implode(',', $values) . ")");
                         $this->group_by = 'p.id';
                     }
                 }
                 break;
         }
     }
     if ($sum) {
         $this->fields[] = '(' . implode(' + ', $sum) . ') AS upselling_deviation';
         $this->order_by = 'upselling_deviation';
     }
 }
示例#2
0
 /**
  * Returns products identified as upselling items for current product.
  *
  * @param int $limit Maximum number of items to be returned
  * @param bool $available_only Whether only products with positive or unlimited stock count must be returned
  *
  * @return array Array of upselling products' data sub-arrays
  */
 public function upSelling($limit = 5, $available_only = false)
 {
     $upselling = $this->getData('upselling');
     // upselling on (usign similar settting for type)
     if ($upselling == 1 || $upselling === null) {
         $type_upselling_model = new shopTypeUpsellingModel();
         $conditions = $type_upselling_model->getByField('type_id', $this->getData('type_id'), true);
         if ($conditions) {
             $collection = new shopProductsCollection('upselling/' . $this->getId(), array('product' => $this, 'conditions' => $conditions));
             if ($available_only) {
                 $collection->addWhere('(p.count > 0 OR p.count IS NULL)');
             }
             return $collection->getProducts('*', $limit);
         } else {
             return array();
         }
     } elseif (!$upselling) {
         return array();
     } else {
         $collection = new shopProductsCollection('related/upselling/' . $this->getId());
         if ($available_only) {
             $collection->addWhere('(p.count > 0 OR p.count IS NULL)');
         }
         return $collection->getProducts('*', $limit);
     }
 }