/** * Update SKU info * * @param int $sku SKU * @param array $fields New info * @return throws exception */ public function updateSku($sku, $fields) { $checkIntegrity = true; //print_r($fields); die; if (isset($fields['sPrice'])) { $sku->setPrice($fields['sPrice']); } if (isset($fields['sAllowMultiple'])) { $sku->setAllowMultiple($fields['sAllowMultiple']); } if (isset($fields['sTrackInventory'])) { $sku->setTrackInventory($fields['sTrackInventory']); } if (isset($fields['sInventory']) && $fields['sInventory']) { $sku->setInventoryLevel($fields['sInventory']); } if (isset($fields['sSku'])) { $sku->setName($fields['sSku']); } if (isset($fields['state'])) { $sku->setActiveStatus($fields['state']); if (!$fields['state']) { $checkIntegrity = false; } } if (isset($fields['options'])) { $sku->setOptions($fields['options']); } // Meta if (isset($fields['meta'])) { foreach ($fields['meta'] as $metaKey => $metaVal) { $sku->addMeta($metaKey, $metaVal); } } // Before saving SKU, check the for possible conflicts (integrity check) except when the SKU gets unpublished if ($checkIntegrity) { require_once dirname(__DIR__) . DS . 'helpers' . DS . 'Integrity.php'; $integrityCheck = \Integrity::skuIntegrityCheck($sku); if ($integrityCheck->status != 'ok') { $errorMessage = "Integrity check error:"; foreach ($integrityCheck->errors as $error) { $errorMessage .= '<br>' . $error; } throw new \Exception($errorMessage); } } $sku->save(); return $sku; }
public function verify() { if (!isset($this->data->price) || !is_numeric($this->data->price)) { throw new \Exception(Lang::txt('No SKU price set')); } if (!isset($this->data->pId) || !is_numeric($this->data->pId)) { throw new \Exception(Lang::txt('No SKU Product Set')); } // Verify that the SKU has all options set (one option from each option group assigned to the parent product) $product = new Product($this->getProductId()); $productOptionGroups = $product->getOptionGroups(); // Init the set options array() $optionGroupOptionsSet = array(); // Init the flag whether the extra/useless options are set $extraOptionsSet = false; foreach ($this->getOptions() as $oId) { $option = new Option($oId); $optionGroupId = $option->getOptionGroupId(); if (in_array($optionGroupId, $productOptionGroups)) { $optionGroupOptionsSet[] = $optionGroupId; } else { // There are some options set that are from option groups not applied to this product // (most likely due to the removal of the option group from the product.) This should never happen. $extraOptionsSet = true; } } // At this point option groups options set must be the same as the product options groups, throw exception if not $missingOptions = array_diff($productOptionGroups, $optionGroupOptionsSet); if (!empty($missingOptions)) { throw new \Exception(Lang::txt('Not all product options are set')); } if ($extraOptionsSet) { throw new \Exception(Lang::txt('Extra options are set')); } // Integrity check require_once dirname(__DIR__) . DS . 'helpers' . DS . 'Integrity.php'; $integrityCheck = \Integrity::skuIntegrityCheck($this); if ($integrityCheck->status != 'ok') { $errorMessage = "Integrity check error:"; foreach ($integrityCheck->errors as $error) { $errorMessage .= '<br>' . $error; } throw new \Exception($errorMessage); } return true; }