public function actionCreate() { IsAuth::Admin(); $model = new ProductOption(); if (isset($_POST['ProductOption'])) { $model->attributes = $_POST['ProductOption']; if ($model->save()) { $this->redirect(array('view', 'id' => $model->product_option_id)); } } $this->render('create', array('model' => $model)); }
public function updateProductOption() { if (!Request::ajax()) { return App::abort(404); } $arrReturn = ['status' => 'error']; if (Input::has('pk')) { $id = (int) Input::get('pk'); $name = (string) Input::get('name'); $value = e((string) Input::get('value')); try { $option = ProductOption::findorFail($id); $option->{$name} = $value; } catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) { return App::abort(404); } } else { $option = new ProductOption(); $option->name = Input::get('name'); $option->key = Input::get('key'); $option->option_group_id = Input::has('option_group_id') ? (int) Input::get('option_group_id') : 0; } $pass = $option->valid(); if ($pass->passes()) { $option->save(); $arrReturn = ['status' => 'ok']; $arrReturn['message'] = $option->name . ' has been saved'; $arrReturn['data'] = $option; } else { $arrReturn['message'] = ''; $arrErr = $pass->messages()->all(); foreach ($arrErr as $value) { $arrReturn['message'] .= "{$value}\n"; } } $response = Response::json($arrReturn); $response->header('Content-Type', 'application/json'); return $response; }
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(); } } } }
public function setOption(OptionForm $OptionForm) { if (!empty($this->id)) { $id = $this->id; Yii::app()->db->createCommand("DELETE FROM {{store_product_option}} WHERE product_id=:id")->bindParam(':id', $id)->execute(); } for ($i = 1; $i <= OptionForm::count; ++$i) { if ($OptionForm->{"value_{$i}"} == '') { break; } $option = new ProductOption(); $option->product_id = $this->id; $option->title = $OptionForm->{"title_{$i}"}; $option->value = $OptionForm->{"value_{$i}"}; $option->save(); } }
/** * Creates a new or modifies an exisitng specification field (according to a passed parameters) * * @return JSONResponse Returns success status or failure status with array of erros */ private function save(ProductOption $productOption) { $this->getProductOptionConfig(); $errors = $this->validate($this->request->getValueArray(array('values', 'name', 'type', 'parentID', 'ID')), $this->productOptionConfig['languageCodes']); if (!$errors) { $productOption->loadRequestData($this->request); $productOption->save(); // create a default choice for non-select options if (!$productOption->isSelect()) { if (!$productOption->defaultChoice->get()) { $defChoice = ProductOptionChoice::getNewInstance($productOption); } else { $defChoice = $productOption->defaultChoice->get(); $defChoice->load(); } $defChoice->loadRequestData($this->request); $defChoice->save(); if (!$productOption->defaultChoice->get()) { $productOption->defaultChoice->set($defChoice); $productOption->save(); } } $parentID = (int) $this->request->get('parentID'); $values = $this->request->get('values'); // save specification field values in database $newIDs = array(); if ($productOption->isSelect() && is_array($values)) { $position = 1; $countValues = count($values); $i = 0; $prices = $this->request->get('prices'); foreach ($values as $key => $value) { $i++; // If last new is empty miss it if ($countValues == $i && preg_match('/new/', $key) && empty($value[$this->productOptionConfig['languageCodes'][0]])) { continue; } if (preg_match('/^new/', $key)) { $productOptionValues = ProductOptionChoice::getNewInstance($productOption); } else { $productOptionValues = ProductOptionChoice::getInstanceByID((int) $key); } $productOptionValues->setLanguageField('name', $value, $this->productOptionConfig['languageCodes']); $productOptionValues->priceDiff->set($prices[$key]); $productOptionValues->position->set($position++); $productOptionValues->save(); if (preg_match('/^new/', $key)) { $newIDs[$productOptionValues->getID()] = $key; } } } return new JSONResponse(array('id' => $productOption->getID(), 'newIDs' => $newIDs), 'success'); } else { return new JSONResponse(array('errors' => $this->translateArray($errors))); } }