private function _setPrices(array $row) { $default = null; foreach ($this->_priceTypes as $type) { $price = $this->_getPriceObject($type, $row, $default); if (null === $default && $type === 'retail') { $default = $price; } $this->_product->getPrices()->add($price); } }
/** * @param Product $product Instance of Product object to convert to array * @throws TransformationFailedException Throws exception if Product object not given * * @return array Returns array of product segments */ public function transform($product) { if ($product === null) { return []; // For chaining, see symfony docs } if (!$product instanceof Product) { throw new TransFailExeption("Parameter 1 must be instance of Message\\Mothership\\Commerce\\Product\\Product."); } $properties = ['name' => $product->getName(), 'brand' => $product->getBrand(), 'category' => $product->getCategory(), 'description' => $product->getDescription(), 'prices' => []]; foreach ($product->getPrices() as $key => $pricing) { $properties['prices'][$key] = $product->getPrice($key); } /* * TODO: Extend to enumerate $properties[units] fully */ return $properties; }
public function create(Product $product) { $result = $this->_query->run('INSERT INTO product SET product.type = :type?s, product.name = :name?s, product.weight_grams = :weight?i, product.tax_rate = :tax_rate?f, product.tax_strategy = :tax_strat?s, product.supplier_ref = :supplier?s, product.created_at = :created_at?d, product.created_by = :created_by?i, product.brand = :brand?s, product.category = :category?s ', ['type' => $product->type->getName(), 'name' => $product->name, 'weight' => $product->weight, 'tax_rate' => $product->taxRate, 'tax_strat' => $this->_defaultTaxStrategy->getName(), 'supplier' => $product->supplierRef, 'created_at' => $product->authorship->createdAt(), 'created_by' => $product->authorship->createdBy()->id, 'brand' => $product->getBrand(), 'category' => $product->getCategory()]); $productID = $result->id(); $info = $this->_query->run('INSERT INTO product_info SET product_id = :id?i, locale = :locale?s, display_name = :displayName?s, sort_name = :sortName?s, description = :description?s, short_description = :shortDesc?s', ['id' => $productID, 'locale' => $this->_locale->getID(), 'displayName' => $product->displayName, 'sortName' => $product->sortName, 'description' => $product->description, 'shortDesc' => $product->shortDescription]); $queryAppend = []; $queryVars = []; foreach ($this->_priceTypes as $type) { foreach ($this->_currencyIDs as $currency) { $price = $product->getPrices()->exists($type) ? $product->getPrice($type, $currency) : 0; $queryAppend[] = "(?i, ?s, ?f, ?s, ?s)"; $vars = [$productID, $type, $price, $currency, $this->_locale->getId()]; $queryVars = array_merge($queryVars, $vars); } } $defaultPrices = $this->_query->run('INSERT INTO product_price (product_id, type, price, currency_id, locale) VALUES ' . implode(', ', $queryAppend), $queryVars); $product->id = $productID; return $product; }
/** * Update the prices for the product * * @param Product $product Product object to update * * @return Product Saved Product object */ public function savePrices(Product $product) { $options = array(); $inserts = array(); foreach ($product->getPrices() as $type => $price) { foreach ($price->getCurrencies() as $currency) { $options[] = $product->id; $options[] = $type; $options[] = $product->getPrices()[$type]->getPrice($currency, $this->_locale); $options[] = $currency; $options[] = $this->_locale->getID(); $inserts[] = '(?i,?s,?s,?s,?s)'; } } $this->_trans->add('REPLACE INTO product_price ( product_id, type, price, currency_id, locale ) VALUES ' . implode(',', $inserts) . ' ', $options); if (!$this->_transOverridden) { $this->_trans->commit(); } return $product; }
public function getPrices() { $this->_load('prices'); return parent::getPrices(); }