/** * @throws CHttpException */ public function actionAdd() { if (!Yii::app()->getRequest()->getIsPostRequest()) { throw new CHttpException(404); } $product = Yii::app()->getRequest()->getPost('Product'); if (empty($product['id'])) { throw new CHttpException(404); } $model = CartProduct::model()->findByPk((int) $product['id']); if (null === $model) { throw new CHttpException(404); } $variantsId = Yii::app()->getRequest()->getPost('ProductVariant', []); $variants = []; foreach ((array) $variantsId as $var) { if (!$var) { continue; } $variant = ProductVariant::model()->findByPk($var); if ($variant && $variant->product_id == $model->id) { $variants[] = $variant; } } $model->selectedVariants = $variants; $quantity = empty($product['quantity']) ? 1 : (int) $product['quantity']; Yii::app()->cart->put($model, $quantity); Yii::app()->ajax->success(Yii::t("CartModule.cart", 'Product successfully added to your basket')); }
public function beforeSave() { if (Product::model()->exists('id = :product_id', [":product_id" => $this->product_id])) { $this->variant_ids = array_filter($this->variant_ids); // удаляем варианты, которые не были выбраны, старые не трогаем, чтобы оставить данные, на случай, если вариант был удален из системы foreach ($this->oldVariants as $key => $var) { if (!in_array($var['id'], $this->variant_ids)) { unset($this->oldVariants[$key]); } } $oldVariantIds = array_map(function ($x) { return $x['id']; }, $this->oldVariants); $newVariants = []; foreach ($this->variant_ids as $varId) { if (!in_array($varId, $oldVariantIds)) { /* @var $variant ProductVariant */ $variant = ProductVariant::model()->findByPk($varId); if ($variant) { // сохраняем информацию на случай удаления варианта из системы $newVariants[] = array_merge($variant->attributes, ['attribute_name' => $variant->attribute->name, 'attribute_title' => $variant->attribute->title, 'optionValue' => $variant->getOptionValue()]); } } } $combinedVariants = array_merge($this->oldVariants, $newVariants); $this->variants = serialize($combinedVariants); } return parent::beforeSave(); }
/** * @param $id * @throws CException */ public function actionVariantRow($id) { $variant = new ProductVariant(); $variant->setAttributes(['attribute_id' => (int) $id]); $this->renderPartial('_variant_row', ['variant' => $variant]); }
/** * @param array $variantsIds * @return float|mixed */ public function getPrice(array $variantsIds = []) { $variants = []; if (!empty($variantsIds)) { $criteria = new CDbCriteria(); $criteria->addInCondition("id", $variantsIds); $variants = ProductVariant::model()->findAll($criteria); } else { $variants = $this->selectedVariants; } $basePrice = $this->getResultPrice(); /* выбираем вариант, который меняет базовую цену максимально */ /* @var $variants ProductVariant[] */ $hasBasePriceVariant = false; foreach ($variants as $variant) { if ($variant->type == ProductVariant::TYPE_BASE_PRICE) { if (!$hasBasePriceVariant) { $hasBasePriceVariant = true; $basePrice = $variant->amount; } else { if ($basePrice < $variant->amount) { $basePrice = $variant->amount; } } } } $newPrice = $basePrice; foreach ($variants as $variant) { switch ($variant->type) { case ProductVariant::TYPE_SUM: $newPrice += $variant->amount; break; case ProductVariant::TYPE_PERCENT: $newPrice += $basePrice * ($variant->amount / 100); break; } } return $newPrice; }
public function run() { $shop = Shop::all()->first(); if (!$shop) { return; } $shopId = $shop->id; $seedData = [['shopify_id' => '32932092380', 'title' => 'Test Product 1', 'handle' => 'test-product-1', 'body_html' => '<h1>Test Product</h1>', 'vendor' => 'Shopify', 'product_type' => 'Shirts', 'published_at' => '', 'published_scope' => 'global', 'template_suffix' => '', 'tags' => ['TEST', 'RED', 'FLEECE', 'SALE', 'NEW'], 'options' => [['shopify_id' => '3490394803498', 'name' => 'Size', 'position' => 1, 'values' => ['XS', 'S', 'M', 'L', 'XL']], ['shopify_id' => '34903903498', 'name' => 'Color', 'position' => 2, 'values' => ['Red', 'Green', 'Blue', 'Purple', 'Black']], ['shopify_id' => '34903948038', 'name' => 'Material', 'position' => 3, 'values' => ['Fleece', 'Cotton', 'Leather', 'Silk']]], 'metafields' => [['shopify_id' => '349009222', 'namespace' => 'global', 'key' => 'size_chart', 'value' => 'https://google.com'], ['shopify_id' => '349009234322', 'namespace' => 'global', 'key' => 'youtube_video_url', 'value' => 'https://google.com'], ['shopify_id' => '3434349009222', 'namespace' => 'global', 'key' => 'sale_channel_url', 'value' => 'https://google.com']], 'variants' => [['shopify_id' => '398798731937', 'title' => 'M Black Fleece', 'price' => '200.99', 'compare_at_price' => '599.99', 'sku' => 'SL-34566', 'barcode' => '39083094-309309083-393094831', 'position' => 1, 'grams' => '', 'inventory_policy' => '', 'inventory_management' => 'shopify', 'inventory_quantity' => 200, 'old_inventory_quantity' => 198, 'fulfillment_service' => '', 'requires_shipping' => true, 'taxable' => true, 'option1' => 'M', 'option2' => 'Black', 'option3' => 'Fleece', 'shopify_image_id' => '', 'weight' => '', 'weight_unit' => 'lbs'], ['shopify_id' => '3987981937', 'title' => 'L Red Leather', 'price' => '2000.99', 'compare_at_price' => '5099.99', 'sku' => 'SL-34566', 'barcode' => '39083094-309309083-393094831', 'position' => 1, 'grams' => '', 'inventory_policy' => '', 'inventory_management' => 'shopify', 'inventory_quantity' => 2000, 'old_inventory_quantity' => 1980, 'fulfillment_service' => '', 'requires_shipping' => true, 'taxable' => true, 'option1' => 'L', 'option2' => 'Red', 'option3' => 'Leather', 'shopify_image_id' => '', 'weight' => '', 'weight_unit' => 'lbs'], ['shopify_id' => '398731937', 'title' => 'XL Purple Silk', 'price' => '400.99', 'compare_at_price' => '699.99', 'sku' => 'SL-34566', 'barcode' => '39083094-309309083-393094831', 'position' => 1, 'grams' => '', 'inventory_policy' => '', 'inventory_management' => 'shopify', 'inventory_quantity' => 200, 'old_inventory_quantity' => 198, 'fulfillment_service' => '', 'requires_shipping' => true, 'taxable' => true, 'option1' => 'XL', 'option2' => 'Purple', 'option3' => 'Silk', 'shopify_image_id' => '', 'weight' => '', 'weight_unit' => 'lbs']]], ['shopify_id' => '329320090980', 'title' => 'Test Product 2', 'handle' => 'test-product-2', 'body_html' => '<h1>Test Product 2</h1>', 'vendor' => 'Shopify', 'product_type' => 'Shirts', 'published_at' => '', 'published_scope' => 'global', 'template_suffix' => '', 'tags' => ['TEST', 'RED', 'FLEECE', 'SALE', 'NEW'], 'options' => [['shopify_id' => '3490394803498', 'name' => 'Size', 'position' => 1, 'values' => ['XS', 'S', 'M', 'L', 'XL']], ['shopify_id' => '34903903498', 'name' => 'Color', 'position' => 2, 'values' => ['Red', 'Green', 'Blue', 'Purple', 'Black']], ['shopify_id' => '34903948038', 'name' => 'Material', 'position' => 3, 'values' => ['Fleece', 'Cotton', 'Leather', 'Silk']]], 'metafields' => [['shopify_id' => '349009222', 'namespace' => 'global', 'key' => 'size_chart', 'value' => 'https://google.com'], ['shopify_id' => '349009234322', 'namespace' => 'global', 'key' => 'youtube_video_url', 'value' => 'https://google.com'], ['shopify_id' => '3434349009222', 'namespace' => 'global', 'key' => 'sale_channel_url', 'value' => 'https://google.com']], 'variants' => [['shopify_id' => '398798731937', 'title' => 'M Black Fleece', 'price' => '200.99', 'compare_at_price' => '599.99', 'sku' => 'SL-34566', 'barcode' => '39083094-309309083-393094831', 'position' => 1, 'grams' => '', 'inventory_policy' => '', 'inventory_management' => 'shopify', 'inventory_quantity' => 200, 'old_inventory_quantity' => 198, 'fulfillment_service' => '', 'requires_shipping' => true, 'taxable' => true, 'option1' => 'M', 'option2' => 'Black', 'option3' => 'Fleece', 'shopify_image_id' => '', 'weight' => '', 'weight_unit' => 'lbs'], ['shopify_id' => '3987981937', 'title' => 'L Red Leather', 'price' => '2000.99', 'compare_at_price' => '5099.99', 'sku' => 'SL-34566', 'barcode' => '39083094-309309083-393094831', 'position' => 1, 'grams' => '', 'inventory_policy' => '', 'inventory_management' => 'shopify', 'inventory_quantity' => 2000, 'old_inventory_quantity' => 1980, 'fulfillment_service' => '', 'requires_shipping' => true, 'taxable' => true, 'option1' => 'L', 'option2' => 'Red', 'option3' => 'Leather', 'shopify_image_id' => '', 'weight' => '', 'weight_unit' => 'lbs'], ['shopify_id' => '398731937', 'title' => 'XL Purple Silk', 'price' => '400.99', 'compare_at_price' => '699.99', 'sku' => 'SL-34566', 'barcode' => '39083094-309309083-393094831', 'position' => 1, 'grams' => '', 'inventory_policy' => '', 'inventory_management' => 'shopify', 'inventory_quantity' => 200, 'old_inventory_quantity' => 198, 'fulfillment_service' => '', 'requires_shipping' => true, 'taxable' => true, 'option1' => 'XL', 'option2' => 'Purple', 'option3' => 'Silk', 'shopify_image_id' => '', 'weight' => '', 'weight_unit' => 'lbs']]], ['shopify_id' => '3293297962380', 'title' => 'Test Product 3', 'handle' => 'test-product-3', 'body_html' => '<h1>Test Product 3</h1>', 'vendor' => 'Shopify', 'product_type' => 'Shirts', 'published_at' => '', 'published_scope' => 'global', 'template_suffix' => '', 'tags' => ['TEST', 'RED', 'FLEECE', 'SALE', 'NEW'], 'options' => [['shopify_id' => '3490394803498', 'name' => 'Size', 'position' => 1, 'values' => ['XS', 'S', 'M', 'L', 'XL']], ['shopify_id' => '34903903498', 'name' => 'Color', 'position' => 2, 'values' => ['Red', 'Green', 'Blue', 'Purple', 'Black']], ['shopify_id' => '34903948038', 'name' => 'Material', 'position' => 3, 'values' => ['Fleece', 'Cotton', 'Leather', 'Silk']]], 'metafields' => [['shopify_id' => '349009222', 'namespace' => 'global', 'key' => 'size_chart', 'value' => 'https://google.com'], ['shopify_id' => '349009234322', 'namespace' => 'global', 'key' => 'youtube_video_url', 'value' => 'https://google.com'], ['shopify_id' => '3434349009222', 'namespace' => 'global', 'key' => 'sale_channel_url', 'value' => 'https://google.com']], 'variants' => [['shopify_id' => '398798731937', 'title' => 'M Black Fleece', 'price' => '200.99', 'compare_at_price' => '599.99', 'sku' => 'SL-34566', 'barcode' => '39083094-309309083-393094831', 'position' => 1, 'grams' => '', 'inventory_policy' => '', 'inventory_management' => 'shopify', 'inventory_quantity' => 200, 'old_inventory_quantity' => 198, 'fulfillment_service' => '', 'requires_shipping' => true, 'taxable' => true, 'option1' => 'M', 'option2' => 'Black', 'option3' => 'Fleece', 'shopify_image_id' => '', 'weight' => '', 'weight_unit' => 'lbs'], ['shopify_id' => '3987981937', 'title' => 'L Red Leather', 'price' => '2000.99', 'compare_at_price' => '5099.99', 'sku' => 'SL-34566', 'barcode' => '39083094-309309083-393094831', 'position' => 1, 'grams' => '', 'inventory_policy' => '', 'inventory_management' => 'shopify', 'inventory_quantity' => 2000, 'old_inventory_quantity' => 1980, 'fulfillment_service' => '', 'requires_shipping' => true, 'taxable' => true, 'option1' => 'L', 'option2' => 'Red', 'option3' => 'Leather', 'shopify_image_id' => '', 'weight' => '', 'weight_unit' => 'lbs'], ['shopify_id' => '398731937', 'title' => 'XL Purple Silk', 'price' => '400.99', 'compare_at_price' => '699.99', 'sku' => 'SL-34566', 'barcode' => '39083094-309309083-393094831', 'position' => 1, 'grams' => '', 'inventory_policy' => '', 'inventory_management' => 'shopify', 'inventory_quantity' => 200, 'old_inventory_quantity' => 198, 'fulfillment_service' => '', 'requires_shipping' => true, 'taxable' => true, 'option1' => 'XL', 'option2' => 'Purple', 'option3' => 'Silk', 'shopify_image_id' => '', 'weight' => '', 'weight_unit' => 'lbs']]]]; DB::table('s_products')->delete(); foreach ($seedData as $seedProduct) { // products $product = new Product(); $product->shop_id = $shopId; $product->shopify_id = $seedProduct['shopify_id']; $product->title = $seedProduct['title']; $product->handle = $seedProduct['handle']; $product->body_html = $seedProduct['body_html']; $product->vendor = $seedProduct['vendor']; $product->product_type = $seedProduct['product_type']; $product->published_at = $seedProduct['published_at']; $product->published_scope = $seedProduct['published_scope']; $product->template_suffix = $seedProduct['template_suffix']; $product->tags = implode(',', $seedProduct['tags']); $product->save(); // options if (count($seedProduct['options'])) { foreach ($seedProduct['options'] as $seedOption) { $option = new ProductOption(); $option->product_id = $product->id; $option->shopify_id = $seedOption['shopify_id']; $option->shopify_product_id = $product->shopify_id; $option->name = $seedOption['name']; $option->position = $seedOption['position']; $option->values = implode(',', $seedOption['values']); $option->save(); } } // metafields if (count($seedProduct['metafields'])) { foreach ($seedProduct['metafields'] as $seedMetafield) { $metafield = new ProductMetafield(); $metafield->product_id = $product->id; $metafield->shopify_id = $seedMetafield['shopify_id']; $metafield->shopify_product_id = $product->shopify_id; $metafield->namespace = $seedMetafield['namespace']; $metafield->key = $seedMetafield['key']; $metafield->value = $seedMetafield['value']; $metafield->save(); } } // variants if (count($seedProduct['variants'])) { foreach ($seedProduct['variants'] as $seedVariant) { $variant = new ProductVariant(); $variant->product_id = $product->id; $variant->shopify_id = $seedVariant['shopify_id']; $variant->shopify_product_id = $product->shopify_id; $variant->title = $seedVariant['title']; $variant->price = $seedVariant['price']; $variant->compare_at_price = $seedVariant['compare_at_price']; $variant->sku = $seedVariant['sku']; $variant->barcode = $seedVariant['barcode']; $variant->position = $seedVariant['position']; $variant->grams = $seedVariant['grams']; $variant->inventory_policy = $seedVariant['inventory_policy']; $variant->inventory_management = $seedVariant['inventory_management']; $variant->inventory_quantity = $seedVariant['inventory_quantity']; $variant->old_inventory_quantity = $seedVariant['old_inventory_quantity']; $variant->fulfillment_service = $seedVariant['fulfillment_service']; $variant->requires_shipping = $seedVariant['requires_shipping']; $variant->taxable = $seedVariant['taxable']; $variant->option1 = $seedVariant['option1']; $variant->option2 = $seedVariant['option2']; $variant->option3 = $seedVariant['option3']; $variant->shopify_image_id = $seedVariant['shopify_image_id']; $variant->weight = $seedVariant['weight']; $variant->weight_unit = $seedVariant['weight_unit']; $variant->save(); } } } }
$prod_total += 1; $prod_id = intval(trim($product->ean)); $zProduct = new Product($db); $zProduct->loadByExtId($prod_id); $price_sales = trim($product->price_sales); $price_eus = trim($product->price_eus); $product_price = $price_sales ? $price_sales : $price_eus; $product_name = trim($product->name); $product_stock = intval(trim($product->stock)); /* varianty */ if (strpos($product_name, ' var.') > 0) { list($prodname, $variant_name) = explode(' var.', $product_name); $product_name = trim($prodname); $variant_name = trim($variant_name); if (strlen($variant_name) > 0) { $zVariant = new ProductVariant($db); $zVariant->loadByExtId($prod_id); if ($zVariant->is_loaded) { $zProduct->loadById($zVariant->ival('product_variant_product_id')); $zProduct->data['product_default_variant_id'] = $zVariant->val('product_variant_id'); } if (!$zProduct->is_loaded) { $zProduct->loadSingleFiltered('product_name = ?', [$product_name]); } if (!$zProduct->is_loaded) { $save_product = true; } elseif ($zProduct->val('product_default_variant_id') == $zVariant->val('product_variant_id')) { $save_product = true; } else { $save_product = false; }
/** * @param ProductVariant $variant * * @return bool */ public function isEqualToProductVariant(ProductVariant $variant) { return $this->getSku() == $variant->getSku() && $this->getCount() == $variant->getQuantity() && $this->getName() == $variant->getName() && $this->getPrice() == $variant->getPrice(); }
public function product_show($slug) { $output = ''; $product = Product::findBySlug($slug); $images = ProductImage::findByProduct($slug); $variants = ProductVariant::findByProduct($slug); $files = ProductFile::findByProduct($slug); $videos = ProductVideo::findByProduct($slug); $related_products = $this->products_related($slug); if ($product) { $output = '<div id="product_images">'; if ($images) { foreach ($images as $image) { $output .= '<img src="/public/ecommerce/images/products/' . $image->filename . '" />'; } } if ($variants) { foreach ($variants as $variant) { $description = ''; if ($variant->description) { $description = $variant->description . '<br />'; } $output .= ' <form class="cart_form" action="/products/cart" method="post"> <input type="hidden" name="variant_id" value="' . $variant->id . '" /> <label>' . str_replace('Default', '', $variant->title) . '<br />' . $description . '</label>'; if ($variant->price > 0) { $output .= '<span class="price">$' . number_format($variant->price, 2) . '</span> <input type="hidden" name="quantity" value="1" size="2" /> <input type="submit" name="submit" value="Add to Cart" />'; } $output .= '</form>'; } } if ($files) { $output .= '<div id="files"><h3>Files</h3><ul>'; foreach ($files as $file) { $output .= '<li><a href="' . $file->filename . '" rel="external">' . $file->title . '</a></li>'; } $output .= '</ul></div>'; } if ($videos) { $output .= '<div id="videos"><h3>Videos</h3><ul>'; foreach ($videos as $video) { $output .= '<li><a href="' . $video->filename . '" rel="shadowbox">' . $video->title . '</a></li>'; } $output .= '</ul></div>'; } $output .= '</div>'; $output .= $product->description; if ($related_products) { $output .= '<br clear="all"><h3>Related Products</h3>'; $output .= $this->products_related($slug); } } return $output; }