public function testSimpleImport() { $lv = ActiveRecordModel::getNewInstance('Language'); $lv->setID('xx'); $lv->save(); $profile = new CsvImportProfile('Product'); $profile->setField(0, 'Product.sku'); $profile->setField(1, 'Product.name', array('language' => 'en')); $profile->setField(2, 'Product.name', array('language' => 'xx')); $profile->setField(3, 'Product.shippingWeight'); $profile->setParam('delimiter', ';'); $csvFile = ClassLoader::getRealPath('cache.') . 'testDataImport.csv'; file_put_contents($csvFile, 'test; "Test Product"; "Parbaudes Produkts"; 15' . "\n" . 'another; "Another Test"; "Vel Viens"; 12.44'); $import = new ProductImport($this->getApplication()); $csv = $profile->getCsvFile($csvFile); $cnt = $import->importFile($csv, $profile); $this->assertEquals($cnt, 2); $test = Product::getInstanceBySKU('test'); $this->assertTrue($test instanceof Product); $this->assertEquals($test->shippingWeight->get(), '15'); $this->assertEquals($test->getValueByLang('name', 'en'), 'Test Product'); $another = Product::getInstanceBySKU('another'); $this->assertEquals($another->getValueByLang('name', 'xx'), 'Vel Viens'); unlink($csvFile); }
public function set() { $request = $this->getApplication()->getRequest(); $sku = $request->get('sku'); $quantity = $request->get('quantity'); if (is_numeric($quantity) == false) { throw new Exception('Stock quantity must be numeric'); } $product = Product::getInstanceBySKU($sku); if ($product == null) { throw new Exception('Product not found'); } $product->stockCount->set($quantity); $product->save(); return $this->statusResponse($sku, 'updated'); }
/** * @role mass */ public function processMass() { $filter = $this->getSelectFilter(); $act = $this->request->get('act'); $field = array_pop(explode('_', $act, 2)); if ('move' == $act) { new ActiveGrid($this->application, $filter, $this->getClassName()); $cat = Category::getInstanceById($this->request->get('categoryID'), Category::LOAD_DATA); $update = new ARUpdateFilter(); $update->setCondition($filter->getCondition()); $update->addModifier('Product.categoryID', $cat->getID()); $update->joinTable('ProductPrice', 'Product', 'productID AND (ProductPrice.currencyID = "' . $this->application->getDefaultCurrencyCode() . '")', 'ID'); ActiveRecord::beginTransaction(); ActiveRecord::updateRecordSet('Product', $update, Product::LOAD_REFERENCES); Category::recalculateProductsCount(); ActiveRecord::commit(); return new JSONResponse(array('act' => $this->request->get('act')), 'success', $this->translate('_move_succeeded')); } // remove design themes if ('theme' == $act && !$this->request->get('theme')) { ClassLoader::import('application.model.presentation.CategoryPresentation'); ActiveRecord::deleteRecordSet('CategoryPresentation', new ARDeleteFilter($filter->getCondition()), null, array('Product', 'Category')); return new JSONResponse(array('act' => $this->request->get('act')), 'success', $this->translate('_themes_removed')); } $params = array(); if ('manufacturer' == $act) { $params['manufacturer'] = Manufacturer::getInstanceByName($this->request->get('manufacturer')); } else { if ('price' == $act || 'inc_price' == $act) { $params['baseCurrency'] = $this->application->getDefaultCurrencyCode(); $params['price'] = $this->request->get($act); $params['currencies'] = $this->application->getCurrencySet(); $params['inc_price_value'] = $this->request->get('inc_price_value'); $params['inc_quant_price'] = $this->request->get('inc_quant_price'); } else { if ('addRelated' == $act) { $params['relatedProduct'] = Product::getInstanceBySKU($this->request->get('related')); if (!$params['relatedProduct']) { return new JSONResponse(0); } } else { if ($this->request->get('categoryID')) { $params['category'] = Category::getInstanceById($this->request->get('categoryID'), Category::LOAD_DATA); } else { if ('theme' == $act) { ClassLoader::import('application.model.presentation.CategoryPresentation'); $params['theme'] = $this->request->get('theme'); } else { if ('shippingClass' == $act) { $params['shippingClass'] = $this->request->get('shippingClass'); } else { if ('taxClass' == $act) { $params['taxClass'] = $this->request->get('taxClass'); } } } } } } } $response = parent::processMass($params); if ($this->request->get('categoryID')) { Category::recalculateProductsCount(); } return $response; }
private function getCategory(CsvImportProfile $profile, $record) { $fields = $this->getSortedFields($profile); // detect product category if (isset($fields['Product']['parentID']) && !empty($record[$fields['Product']['parentID']])) { $cat = Product::getInstanceByID($record[$fields['Product']['parentID']], true); } else { if (isset($fields['Parent']['parentSKU']) && !empty($record[$fields['Parent']['parentSKU']])) { $cat = Product::getInstanceBySKU($record[$fields['Parent']['parentSKU']]); } else { if (isset($fields['Category']['ID'])) { try { $cat = Category::getInstanceById($record[$fields['Category']['ID']], Category::LOAD_DATA); } catch (ARNotFoundException $e) { $failed++; continue; } } else { if (isset($fields['Categories']['Categories'])) { $index = $fields['Categories']['Categories']; $categories = explode('; ', $record[$index]); $cat = $this->getCategoryByPath($profile, array_shift($categories)); $extraCategories = $categories; } else { if (isset($fields['Category'])) { $path = array(); foreach ($fields['Category'] as $level => $csvIndex) { if ($record[$csvIndex]) { $path[] = $record[$csvIndex]; } } $cat = $this->getCategoryByPath($profile, $path); } else { return $this->getRoot($profile); } } } } } return $cat; }
public function getNextCustomerOrder() { if (!($data = $this->loadRecord('SELECT * FROM ' . $this->getTablePrefix() . 'orders WHERE customerID > 0'))) { return null; } $user = ActiveRecordModel::getInstanceByIDIfExists('User', $this->getRealId('User', $data['customerID'], false)); if (!$user || !$user->isExistingRecord()) { return $this->getNextCustomerOrder(); } $currCode = $data['currency_code']; if (!($currency = ActiveRecordModel::getInstanceByIDIfExists('Currency', $currCode, false))) { $currency = Currency::getNewInstance($currCode); $currency->save(); } $order = CustomerOrder::getNewInstance($user); $order->currency->set($currency); $order->dateCompleted->set($data['order_time']); // products foreach ($this->getDataBySql('SELECT * FROM ' . $this->getTablePrefix() . 'ordered_carts WHERE orderID=' . $data['orderID']) as $item) { $product = null; // try to identify product by SKU preg_match('/\\[(.*)\\]/', $item['name'], $sku); if (isset($sku[1])) { $product = Product::getInstanceBySKU($sku[1]); } // if that doesn't work, then try to match the exact name if (!$product) { $productData = array_shift($this->getDataBySQL('SELECT productID FROM ' . $this->getTablePrefix() . 'products WHERE name="' . addslashes($item['name']) . '"')); if ($productData && is_array($productData)) { $product = Product::getInstanceByID($this->getRealId('Product', $productData['productID']), Product::LOAD_DATA); } } if ($product) { $order->addProduct($product, $item['Quantity'], true); $orderedItem = array_shift($order->getItemsByProduct($product)); $orderedItem->price->set($item['Price']); } } // addresses $order->shippingAddress->set($this->getUserAddress($data, 'shipping_')); $order->billingAddress->set($this->getUserAddress($data, 'billing_')); $order->status->set($this->getOrderStatus($data['statusID'])); if ($order->status->get() == CustomerOrder::STATUS_SHIPPED) { $order->isPaid->set(true); } $order->rawData = $data; return $order; }
/** * Import or update an ordered product */ protected function set_OrderedItem_sku($instance, $value, $record, CsvImportProfile $profile) { if (!$value) { return; } $product = Product::getInstanceBySKU($value); if (!$product) { return; } $items = $instance->getItemsByProduct($product); // create initial shipment if (!$instance->getShipments()->size()) { $shipment = Shipment::getNewInstance($instance); $shipment->save(); } // any particular shipment? $shipment = $item = null; if ($profile->isColumnSet('OrderedItem.shipment')) { // internal indexes are 0-based, but the import references are 1-based $shipmentNo = $this->getColumnValue($record, $profile, 'OrderedItem.shipment') - 1; if (is_numeric($this->getColumnValue($record, $profile, 'OrderedItem.shipment'))) { foreach ($instance->getShipments() as $key => $shipment) { if ($key == $shipmentNo) { break; } $shipment = null; } // create a new shipment if (!$shipment) { $shipment = Shipment::getNewInstance($instance); $shipment->save(); } foreach ($items as $item) { if ($item->shipment->get() == $shipment) { break; } unset($item); } } } if (!$item) { $item = array_shift($items); } if (!$item) { $count = $this->getColumnValue($record, $profile, 'OrderedItem.count'); $item = OrderedItem::getNewInstance($instance, $product, max(1, $count)); $instance->addItem($item); } if ($profile->isColumnSet('OrderedItem.count')) { $count = $this->getColumnValue($record, $profile, 'OrderedItem.count'); $item->count->set(max(1, $count)); } if ($profile->isColumnSet('OrderedItem.price')) { $item->price->set($this->getColumnValue($record, $profile, 'OrderedItem.price')); } if (!$shipment) { $shipment = $instance->getShipments()->get(0); } $item->shipment->set($shipment); $item->save(); $instance->finalize(array('customPrice' => true, 'allowRefinalize' => true)); }
<?php include '../application/Initialize.php'; ClassLoader::import('application.LiveCart'); new LiveCart(); ClassLoader::import('application.model.product.Product'); ClassLoader::import('application.model.system.Installer'); $file = ClassLoader::getRealPath('installdata.demo') . '.sql'; //$file = 'alter.sql'; if (!file_exists($file)) { die('File not found: <strong>' . $file . '</strong>'); } set_time_limit(0); Product::getInstanceBySKU('test'); $dump = file_get_contents($file); // newlines $dump = str_replace("\r", '', $dump); // clear comments $dump = preg_replace('/#.*#/', '', $dump); // get queries $queries = preg_split('/;\\n/', $dump); foreach ($queries as $query) { $query = trim($query); if (!empty($query)) { ActiveRecord::executeUpdate($query); } }
public function importInstance($record, CsvImportProfile $profile) { if (array_key_exists('sku', $record)) { $instance = Product::getInstanceBySKU($record['sku']); $id = $instance ? $instance->getID() : 0; if ($this->allowOnly == self::CREATE && $id > 0) { throw new Exception('Record exists'); } if ($this->allowOnly == self::UPDATE && $id == 0) { throw new Exception('Record not found'); } } else { // if identified by smth else what then? } return parent::importInstance($record, $profile); }