/** * Get a list of all featured products. */ protected function getFeaturedProductsList() { $options = array(); // If disabled products are not meant to be included, include just the // enabled ones. $products = Products::getInstance()->getAll(array('featured' => true, 'enabled' => true)); $productList = array(); if (!empty($products)) { $productList = array(); foreach ($products as $product) { // Truncate the description if it is too long. $description = $product->description; if (strlen($description) > 150) { $description = substr($description, 0, 147) . '...'; } $productList[] = array('id' => $product->id, 'name' => $product->name, 'description' => $description, 'price' => $product->price, 'weight' => $product->weight, 'price_kg' => $product->getPricePerKg(), 'enabled' => $product->enabled); } } return $productList; }
public function update(array $data, $isAtomic = true) { // If basket data is present, create a Basket object. if (isset($data['basket']) && !$data['basket'] instanceof Basket) { Logger::get()->debug(sprintf('Filling customer %s\'s basket...', $this->id)); // Fill it up. $products = Products::getInstance(); $basket = new Basket(array()); foreach ($data['basket'] as $productId => $count) { Logger::get()->debug("Dropping {$count} of {$productId} in the basket..."); try { $basket->add($products->findEntity($productId), $count); Logger::get()->debug('Dropped.'); } catch (NotFoundException $exception) { Logger::get()->debug('Not found!'); } } Logger::get()->debug('Basket is filled.'); $data['basket'] = $basket; } // Update the rest of the customer data. parent::update($data, $isAtomic); }
public function getDefaultFactory() { return Products::getInstance(); }
/** * Provides a list of Product objects that reflect the contents of the * basket. * * @return Product[] the returned Product objects have been injected with * a 'count' property which details how many of those products the basket * holds. */ public function getContents() { if (empty($this->contents)) { foreach ($this->products as $id => $count) { // Get the product entity. $product = Products::getInstance()->findEntity($id); $this->contents[$id] = $product->getFieldsData(); // Inject the product metadata in the entity. $this->contents[$id]['count'] = $count; $this->contents[$id]['totalPrice'] = $product->price * $count; if (property_exists($product, 'weight')) { $this->contents[$id]['totalWeight'] = $product->weight * $count; } } } return $this->contents; }
/** * Runs the product import process. * * @return string the ID of the import record. */ private function importProducts() { $file = $this->getRequest()->getParameter('products_file'); $fileName = $file['tmp_name']; if (is_readable($fileName)) { $file = fopen($fileName, 'r'); $importedIds = array(); $count = 0; // Ignore the first line, it's the header. $record = fgetcsv($file); while (!feof($file)) { $record = fgetcsv($file); if (is_array($record)) { Logger::get()->info('Importing record: ' . implode(',', $record)); // Assign the record's fields to their variables. list($id, $name, $description, $price, $weight, $precision, $presentation, $isGroupF, $isGroupV, $isGroupP, $isGroupW, $isGroupE, $imageUrl) = $record; try { // Proceed only if the required fields are present. if ($id && $name && $price && $weight) { $data = array('id' => $id, 'name' => $name, 'description' => $description ?: _('(No description)'), 'price' => $price, 'weight' => $weight, 'presentation' => $presentation ?: _('Unit')); $product = NULL; try { // Get the product from the factory. $product = Products::getInstance()->findEntity($id); // It's there, update it. Logger::get()->info("Updating product {$id}: {$name}..."); $product->update($data); } catch (NotFoundException $exception) { Logger::get()->info("Creating product {$id}: {$name}..."); // It's not there, create a new one. $product = new Product($data); } // Decode groups. $groupIdArray = array(); if ($isGroupF) { $groupIdArray[] = 'f'; } if ($isGroupV) { $groupIdArray[] = 'v'; } if ($isGroupP) { $groupIdArray[] = 'p'; } if ($isGroupW) { $groupIdArray[] = 'w'; } if ($isGroupE) { $groupIdArray[] = 'e'; } $productGroups = ProductGroups::getInstance(); foreach ($groupIdArray as $groupId) { $productGroup = $productGroups->findEntity($groupId); $product->addToGroup($productGroup); } // Store the product. $product->store(); // Open a stream to read the image, in case it's a // URL. if ($imageUrl) { $imageHandle = @fopen($imageUrl, 'r'); if ($imageHandle) { // Store image. $image = new ProductImage(array('id' => $id, 'source' => $imageHandle)); $image->store(); } } // Record the ID of the successfully imported product. $importedIds[] = $product->getId(); } else { throw new \InvalidArgumentException('Missing data.'); } } catch (\Exception $exception) { Logger::get()->warning("Could not import record: {$exception->getMessage()}\n\t" . @join(', ', $record)); } } } // Generate a record of the import. $details = count($importedIds) > 0 ? _('The following products have been imported: ') . join(', ', $importedIds) : _('No products have been imported.'); $importRecord = new Report(array('type' => Report::TYPE_IMPORT, 'details' => $details)); $importRecord->store(); // Return the import log ID. return $importRecord->getId(); } else { throw new BadRequestException('Uploaded file cannot be used: ' . $fileName); } }
protected function getFactory() { return Products::getInstance(); }
/** * Removes the selected product from the basket. * * @param string $productId the ID of the product to remove. */ private function getRemoveFromBasketResponse($productId) { $response = $this->getViewPageResponse(); try { // Remove the selected product from the current customer's basket. Customers::getCurrent()->getBasket()->remove(Products::getInstance()->findEntity($productId)); } catch (NotFoundException $exception) { $response->addErrorMessage(_('There is no such product.')); } catch (\Exception $exception) { $customerId = Customers::getCurrent()->getId(); Logger::get()->error("Failed to remove product {$productId} from customer {$customerId}'s basket."); $response->addErrorMessage(_('The product could not be removed from the basket.')); } return $response; }