/** * Load products added to cart * @return array */ public function getDataWithModels() { $data = $this->getData(); if (empty($data)) { return array(); } foreach ($data as $index => &$item) { $item['variant_models'] = array(); $item['model'] = StoreProduct::model()->findByPk($item['product_id']); // Load configurable product if ($item['configurable_id']) { $item['configurable_model'] = StoreProduct::model()->findByPk($item['configurable_id']); } // Process variants if (!empty($item['variants'])) { $item['variant_models'] = StoreProductVariant::model()->with(array('attribute', 'option'))->findAllByPk($item['variants']); } // If product was deleted during user session!. if (!$item['model']) { unset($data[$index]); } } unset($item); return $data; }
/** * Calculate product price by its variants, configuration and self price * @static * @param $product * @param array $variants * @param $configuration */ public static function calculatePrices($product, array $variants, $configuration) { if ($product instanceof StoreProduct === false) { $product = StoreProduct::model()->findByPk($product); } if ($configuration instanceof StoreProduct === false && $configuration > 0) { $configuration = StoreProduct::model()->findByPk($configuration); } if ($configuration instanceof StoreProduct) { $result = $configuration->price; } else { $result = $product->price; } // if $variants contains not models if (!empty($variants) && $variants[0] instanceof StoreProductVariant === false) { $variants = StoreProductVariant::model()->findAllByPk($variants); } foreach ($variants as $variant) { // Price is percent if ($variant->price_type == 1) { $result += $result / 100 * $variant->price; } else { $result += $variant->price; } } return $result; }
/** * Save product variants * @param StoreProduct $model */ protected function processVariants(StoreProduct $model) { $dontDelete = array(); if (!empty($_POST['variants'])) { foreach ($_POST['variants'] as $attribute_id => $values) { $i = 0; foreach ($values['option_id'] as $option_id) { // Try to load variant from DB $variant = StoreProductVariant::model()->findByAttributes(array('product_id' => $model->id, 'attribute_id' => $attribute_id, 'option_id' => $option_id)); // If not - create new. if (!$variant) { $variant = new StoreProductVariant(); } $variant->setAttributes(array('attribute_id' => $attribute_id, 'option_id' => $option_id, 'product_id' => $model->id, 'price' => $values['price'][$i], 'price_type' => $values['price_type'][$i], 'sku' => $values['sku'][$i]), false); $variant->save(false); array_push($dontDelete, $variant->id); $i++; } } } if (!empty($dontDelete)) { $cr = new CDbCriteria(); $cr->addNotInCondition('id', $dontDelete); $cr->addCondition('product_id=' . $model->id); StoreProductVariant::model()->deleteAll($cr); } else { StoreProductVariant::model()->deleteAllByAttributes(array('product_id' => $model->id)); } }
/** * Copy product variants * * @param StoreProduct $original * @param StoreProduct $copy */ public function copyVariants(StoreProduct $original, StoreProduct $copy) { $variants = $original->variants; if (!empty($variants)) { foreach ($variants as $v) { $record = new StoreProductVariant(); $record->product_id = $copy->id; $record->attribute_id = $v->attribute_id; $record->option_id = $v->option_id; $record->price = $v->price; $record->price_type = $v->price_type; $record->sku = $v->sku; $record->save(); } } }
/** * Check if product variantion exists * @param $product_id * @param $attribute_id * @param $variant_id * @return string */ protected function _checkVariantExists($product_id, $attribute_id, $variant_id) { return StoreProductVariant::model()->countByAttributes(array('id' => $variant_id, 'product_id' => $product_id, 'attribute_id' => $attribute_id)); }