/**
  * @param Market_ProductModel $product
  *
  * @return bool
  * @throws Exception
  * @throws \Exception
  */
 public function save(Market_ProductModel $product)
 {
     if (!$product->id) {
         $record = new Market_ProductRecord();
     } else {
         $record = Market_ProductRecord::model()->findById($product->id);
         if (!$record) {
             throw new Exception(Craft::t('No product exists with the ID “{id}”', ['id' => $product->id]));
         }
     }
     $record->availableOn = $product->availableOn;
     $record->expiresOn = $product->expiresOn;
     $record->typeId = $product->typeId;
     $record->authorId = $product->authorId;
     $record->promotable = $product->promotable;
     $record->freeShipping = $product->freeShipping;
     $record->taxCategoryId = $product->taxCategoryId;
     $record->validate();
     $product->addErrors($record->getErrors());
     MarketDbHelper::beginStackedTransaction();
     try {
         if (!$product->hasErrors()) {
             if (craft()->elements->saveElement($product)) {
                 $record->id = $product->id;
                 $record->save(false);
                 MarketDbHelper::commitStackedTransaction();
                 return true;
             }
         }
     } catch (\Exception $e) {
         MarketDbHelper::rollbackStackedTransaction();
         throw $e;
     }
     MarketDbHelper::rollbackStackedTransaction();
     return false;
 }
 /**
  * @param Market_ProductModel $product
  *
  * @return Market_VariantModel
  */
 private function _setImplicitVariantFromPost(Market_ProductModel $product)
 {
     $attributes = craft()->request->getPost('implicitVariant');
     $implicitVariant = $product->getImplicitVariant() ?: new Market_VariantModel();
     $implicitVariant->setAttributes($attributes);
     $implicitVariant->isImplicit = true;
     return $implicitVariant;
 }
 /**
  * @throws \Craft\Exception
  * @throws \Craft\HttpException
  * @throws \Exception
  */
 private function defaultProducts()
 {
     $productTypes = \Craft\craft()->market_productType->getAll();
     //first test product
     /** @var Market_ProductModel $product */
     $product = Market_ProductModel::populateModel(['typeId' => $productTypes[0]->id, 'enabled' => 1, 'authorId' => \Craft\craft()->userSession->id, 'availableOn' => new DateTime(), 'expiresOn' => NULL, 'promotable' => 1, 'taxCategoryId' => \Craft\craft()->market_taxCategory->getDefaultId()]);
     $product->getContent()->title = 'Nice Shirt';
     \Craft\craft()->market_product->save($product);
     //implicit variant
     /** @var Market_VariantModel $implicitVariant */
     $implicitVariant = Market_VariantModel::populateModel(['productId' => $product->id, 'isImplicit' => 1, 'sku' => 'ABC', 'price' => 10, 'unlimitedStock' => 1]);
     \Craft\craft()->market_variant->save($implicitVariant);
     //another test product
     /** @var Market_ProductModel $product */
     $product = Market_ProductModel::populateModel(['typeId' => $productTypes[0]->id, 'enabled' => 1, 'authorId' => \Craft\craft()->userSession->id, 'availableOn' => new DateTime(), 'expiresOn' => NULL, 'promotable' => 1, 'taxCategoryId' => \Craft\craft()->market_taxCategory->getDefaultId()]);
     $product->getContent()->title = 'Really Nice Shirt';
     \Craft\craft()->market_product->save($product);
     //implicit variant
     $implicitVariant = Market_VariantModel::populateModel(['productId' => $product->id, 'isImplicit' => 1, 'sku' => 'CBA', 'price' => 20, 'unlimitedStock' => 1]);
     \Craft\craft()->market_variant->save($implicitVariant);
 }