Esempio n. 1
0
 private function _setPrices(Product &$product, $param)
 {
     if (isset($param->CallbackParameter->prices) && count($prices = $param->CallbackParameter->prices) > 0) {
         //delete all price first
         $deleteIds = array();
         foreach ($prices as $price) {
             if (trim($price->active) === '0' && isset($price->id)) {
                 $deleteIds[] = trim($price->id);
             }
         }
         if (count($deleteIds) > 0) {
             ProductPrice::updateByCriteria('active = 0', 'id in (' . str_repeat('?', count($deleteIds)) . ')', $deleteIds);
         }
         //update or create new
         foreach ($prices as $price) {
             if (isset($price->id) && in_array(trim($price->id), $deleteIds)) {
                 continue;
             }
             if (!($type = ProductPriceType::get(trim($price->typeId))) instanceof ProductPriceType) {
                 continue;
             }
             $priceValue = trim($price->value);
             $start = trim($price->start);
             $end = trim($price->end);
             if (!is_numeric(StringUtilsAbstract::getValueFromCurrency($priceValue))) {
                 throw new Exception('Invalid price: ' . $priceValue);
             }
             if (!isset($price->id) || ($id = trim($price->id)) === '') {
                 if (trim($price->active) === '1') {
                     ProductPrice::create($product, $type, $priceValue, $start, $end);
                 }
                 //if it's deactivated one, ignore
             } else {
                 if (($productPrice = ProductPrice::get($id)) instanceof ProductPrice) {
                     $productPrice->setPrice($priceValue)->setType($type)->setProduct($product)->setStart($start)->setEnd($end)->save();
                 }
             }
         }
     }
     return $this;
 }
Esempio n. 2
0
 /**
  * removing the prices
  *
  * @param ProductPriceType $type
  *
  * @return Product
  */
 public function clearAllPrice()
 {
     ProductPrice::updateByCriteria('active = 0', 'productId = ?', array($this->getId()));
     return $this;
 }