/** * Count total price */ public function getTotalPrice() { $result = 0; $data = $this->getDataWithModels(); foreach ($data as $item) { $configurable = isset($item['configurable_model']) ? $item['configurable_model'] : 0; $result += StoreProduct::calculatePrices($item['model'], $item['variants'], $configurable) * $item['quantity']; } return $result; }
$imgSource = $product['model']->mainImage->getUrl('100x100'); } else { $imgSource = 'http://placehold.it/100x100'; } echo CHtml::link(CHtml::image($imgSource, '', array('class' => 'shop_thumbnail')), array('/store/frontProduct/view', 'url' => $product['model']->url)) . '<br/>'; ?> </td> <td class="product-name"> <? echo CHtml::link(CHtml::encode($product['model']->name), array('/store/frontProduct/view', 'url'=>$product['model']->url)).'<br/>'; ?> </td> <td class="product-price"> <? $price = StoreProduct::calculatePrices($product['model'], $product['variant_models'], $product['configurable_id']); echo CHtml::openTag('span', array('class'=>'amount')); echo StoreProduct::formatPrice(Yii::app()->currency->convert($price)); echo ' '.Yii::app()->currency->active->symbol; echo CHtml::closeTag('span'); ?> </td> <td class="product-quantity"> <div class="quantity buttons_added"> <button class="small_silver_button minus">−</button> <?php echo CHtml::textField("quantities[{$index}]", $product['quantity'], array('class' => 'count', 'onkeyup' => 'ff2(this)')); ?> <button class="small_silver_button plus">+</button> </div> </td>
/** * @param array $products */ public function renderProducts(array $products) { foreach ($products as $p) { if (!count($p->variants)) { $this->renderOffer($p, array('url' => Yii::app()->createAbsoluteUrl('/store/frontProduct/view', array('url' => $p->url)), 'price' => Yii::app()->currency->convert($p->price, $this->_config['currency_id']), 'currencyId' => $this->currencyIso, 'categoryId' => $p->mainCategory->id, 'picture' => $p->mainImage ? Yii::app()->createAbsoluteUrl($p->mainImage->getUrl()) : null, 'name' => CHtml::encode($p->name), 'description' => $this->clearText($p->short_description))); } else { foreach ($p->variants as $v) { $name = strtr('{product}({attr} {option})', array('{product}' => $p->name, '{attr}' => $v->attribute->title, '{option}' => $v->option->value)); $hashtag = '#' . $v->attribute->name . ':' . $v->option->id; $this->renderOffer($p, array('url' => Yii::app()->createAbsoluteUrl('/store/frontProduct/view', array('url' => $p->url)) . $hashtag, 'price' => Yii::app()->currency->convert(StoreProduct::calculatePrices($p, $p->variants, 0), $this->_config['currency_id']), 'currencyId' => $this->currencyIso, 'categoryId' => $p->mainCategory->id, 'picture' => $p->mainImage ? Yii::app()->createAbsoluteUrl($p->mainImage->getUrl()) : null, 'name' => CHtml::encode($name), 'description' => $this->clearText($p->short_description))); } } } }
/** * Create new order * @return Order */ public function createOrder() { if (Yii::app()->cart->countItems() == 0) { return false; } $order = new Order(); // Set main data $order->user_id = Yii::app()->user->isGuest ? null : Yii::app()->user->id; $order->user_name = $this->form->name; $order->user_email = $this->form->email; $order->user_phone = $this->form->phone; $order->user_address = $this->form->address; $order->user_comment = $this->form->comment; $order->delivery_id = $this->form->delivery_id; if ($order->validate()) { $order->save(); } else { throw new CHttpException(503, Yii::t('OrdersModule.core', 'Ошибка создания заказа')); } // Process products foreach (Yii::app()->cart->getDataWithModels() as $item) { $ordered_product = new OrderProduct(); $ordered_product->order_id = $order->id; $ordered_product->product_id = $item['model']->id; $ordered_product->configurable_id = $item['configurable_id']; $ordered_product->name = $item['model']->name; $ordered_product->quantity = $item['quantity']; $ordered_product->sku = $item['model']->sku; $ordered_product->price = StoreProduct::calculatePrices($item['model'], $item['variant_models'], $item['configurable_id']); // Process configurable product if (isset($item['configurable_model']) && $item['configurable_model'] instanceof StoreProduct) { $configurable_data = array(); $ordered_product->configurable_name = $item['configurable_model']->name; // Use configurable product sku $ordered_product->sku = $item['configurable_model']->sku; // Save configurable data $attributeModels = StoreAttribute::model()->findAllByPk($item['model']->configurable_attributes); foreach ($attributeModels as $attribute) { $method = 'eav_' . $attribute->name; $configurable_data[$attribute->title] = $item['configurable_model']->{$method}; } $ordered_product->configurable_data = serialize($configurable_data); } // Save selected variants as key/value array if (!empty($item['variant_models'])) { $variants = array(); foreach ($item['variant_models'] as $variant) { $variants[$variant->attribute->title] = $variant->option->value; } $ordered_product->variants = serialize($variants); } $ordered_product->save(); } // Reload order data. $order->refresh(); // All products added. Update delivery price. $order->updateDeliveryPrice(); // Send email to user. $this->sendEmail($order); return $order; }