private function importNewsletterDemoData() { $this->modelManager = Shopware()->Container()->get('models'); $newsletterGroup = $this->modelManager->find(Group::class, 1); for ($addressAmount = 0; $addressAmount < 25; $addressAmount++) { $address = new Address(); $address->setEmail('test_' . $addressAmount . '@example.com'); $address->setAdded(new \DateTime()); $address->setNewsletterGroup($newsletterGroup); $address->setIsCustomer(false); $this->modelManager->persist($address); } $this->modelManager->flush(); }
public function test_write_should_update_an_existing_article() { $expectedModifiedArticle = ['orderNumber' => 'SW10002', 'mainNumber' => 'SW10002', 'additionalText' => '', 'supplierName' => 'Rolinck', 'name' => 'Beer', 'tax' => '19.00', 'active' => '0', 'lastStock' => '1', 'inStock' => '45', 'stockMin' => '0', 'description' => 'This is the description of a very good drink. The description should be updated.', 'descriptionLong' => 'This is the description of a very good drink. This description is longeeeer. And should be updated!', 'unitId' => '1']; $articleWriterResult = $this->articleWriter->write($expectedModifiedArticle, []); /** @var Article $updatedArticle */ $updatedArticle = $this->modelManager->find(Article::class, $articleWriterResult->getArticleId()); $this->assertNotNull($updatedArticle, "Could not find updated article"); $this->assertEquals($expectedModifiedArticle['orderNumber'], $updatedArticle->getMainDetail()->getNumber(), "Could not update field ordernumber."); $this->assertEquals($expectedModifiedArticle['description'], $updatedArticle->getDescription(), "Could not update field description."); $this->assertEquals($expectedModifiedArticle['descriptionLong'], $updatedArticle->getDescriptionLong(), "Could not update field description long."); $this->assertEquals($expectedModifiedArticle['inStock'], $updatedArticle->getMainDetail()->getInStock(), "Could not update field instock."); $this->assertEquals($expectedModifiedArticle['lastStock'], $updatedArticle->getLastStock(), "Could not update field last stock."); $this->assertFalse($updatedArticle->getActive(), "Could not update field active."); $this->assertEquals($expectedModifiedArticle['supplierName'], $updatedArticle->getSupplier()->getName(), "Could not update field supplier name."); }
/** * @param int $id * @return \Shopware\Models\Shop\Shop $shop * @throws AdapterException * @throws \Doctrine\ORM\ORMException * @throws \Doctrine\ORM\OptimisticLockException * @throws \Doctrine\ORM\TransactionRequiredException */ public function getShop($id) { $shop = $this->modelManager->find('Shopware\\Models\\Shop\\Shop', $id); if (!$shop) { $message = $this->snippetHelper->getNamespace()->get('adapters/articles/no_shop_id', 'Shop by id %s not found'); throw new AdapterException(sprintf($message, $id)); } return $shop; }
private function removePriceGroup() { $ids = $this->db->fetchCol("SELECT id FROM s_core_pricegroups WHERE description = 'TEST-GROUP'"); foreach ($ids as $id) { $group = $this->entityManager->find('Shopware\\Models\\Price\\Group', $id); $this->entityManager->remove($group); $this->entityManager->flush(); $this->entityManager->clear(); } }
/** * Try to get the Tax object * * @return bool|Tax * @throws ORMException * @throws OptimisticLockException * @throws TransactionRequiredException */ private function getTax() { $taxId = $this->getTaxId(); /** @var Tax $tax */ $tax = $this->em->find('Shopware\\Models\\Tax\\Tax', $taxId); if (!$tax instanceof Tax) { echo json_encode(array("success" => false, "message" => "Tax with id: {$taxId} does not exist!")); return false; } return $tax; }
/** * Assigns the passed template id to the passed sub shop. * * @param $shopId * @param $templateId * @throws \Exception */ public function assignShopTemplate($shopId, $templateId) { /**@var $shop Shop\Shop */ $shop = $this->entityManager->find('Shopware\\Models\\Shop\\Shop', $shopId); if (!$shop instanceof Shop\Shop) { throw new \Exception(); } /**@var $template Shop\Template */ $template = $this->entityManager->find('Shopware\\Models\\Shop\\Template', $templateId); if (!$template instanceof Shop\Template) { throw new \Exception(); } $shop->setTemplate($template); $this->entityManager->flush(); }
/** * @param array $records * @throws \Enlight_Event_Exception * @throws \Exception * @throws \Zend_Db_Adapter_Exception */ public function write($records) { $customerCount = 0; if (empty($records)) { $message = SnippetsHelper::getNamespace()->get('adapters/customer/no_records', 'No customer records were found.'); throw new \Exception($message); } $records = $this->eventManager->filter('Shopware_Components_SwagImportExport_DbAdapters_CustomerDbAdapter_Write', $records, ['subject' => $this]); $defaultValues = $this->getDefaultValues(); foreach ($records['default'] as $record) { try { $customerCount++; $record = $this->validator->filterEmptyString($record); $customer = $this->findExistingEntries($record); $createNewCustomer = false; if (!$customer instanceof Customer) { $createNewCustomer = true; $record = $this->dataManager->setDefaultFieldsForCreate($record, $defaultValues); $this->validator->checkRequiredFieldsForCreate($record); $customer = new Customer(); } $this->preparePassword($record); $this->validator->checkRequiredFields($record); $this->validator->validate($record, CustomerDataType::$mapper); $customerData = $this->prepareCustomer($record); $customerData['billing'] = $this->prepareBilling($record); $customerData['shipping'] = $this->prepareShipping($record, $createNewCustomer, $customerData['billing']); $customer->fromArray($customerData); if (isset($customerData['subshopID'])) { $shop = $this->manager->getRepository(Shop::class)->find($customerData['subshopID']); if (!$shop) { $message = SnippetsHelper::getNamespace()->get('adapters/shop_not_found', 'Shop with id %s was not found'); throw new AdapterException(sprintf($message, $customerData['subshopID'])); } $customer->setShop($shop); } if (isset($customerData['languageId'])) { $languageSubShop = $this->manager->getRepository(Shop::class)->find($customerData['languageId']); if (!$languageSubShop) { $message = SnippetsHelper::getNamespace()->get('adapters/language_shop_not_found', 'Language-Shop with id %s was not found'); throw new AdapterException(sprintf($message, $customerData['languageId'])); } $customer->setLanguageSubShop($languageSubShop); } $billing = $customer->getDefaultBillingAddress(); if (!$billing instanceof Address) { $billing = new Address(); $billing->setCustomer($customer); } if (isset($customerData['billing']['countryId'])) { $customerData['billing']['country'] = $this->manager->find(Country::class, $customerData['billing']['countryId']); } if (isset($customerData['billing']['stateId'])) { $customerData['billing']['state'] = $this->manager->find(State::class, $customerData['billing']['stateId']); } $billing->fromArray($customerData['billing']); $shipping = $customer->getDefaultShippingAddress(); if (!$shipping instanceof Address) { $shipping = new Address(); $shipping->setCustomer($customer); } if (isset($customerData['shipping']['countryId'])) { $customerData['shipping']['country'] = $this->manager->find(Country::class, $customerData['shipping']['countryId']); } if (isset($customerData['shipping']['stateId'])) { $customerData['shipping']['state'] = $this->manager->find(State::class, $customerData['shipping']['stateId']); } $shipping->fromArray($customerData['shipping']); $customer->setFirstname($billing->getFirstname()); $customer->setLastname($billing->getLastname()); $customer->setSalutation($billing->getSalutation()); $customer->setTitle($billing->getTitle()); $violations = $this->manager->validate($customer); if ($violations->count() > 0) { $message = SnippetsHelper::getNamespace()->get('adapters/customer/no_valid_customer_entity', 'No valid user entity for email %s'); $message = sprintf($message, $customer->getEmail()); foreach ($violations as $violation) { $message .= "\n" . $violation->getPropertyPath() . ': ' . $violation->getMessage(); } throw new AdapterException($message); } $this->manager->persist($customer); if ($createNewCustomer) { $this->manager->flush(); } $customer->setDefaultBillingAddress($billing); $this->manager->persist($billing); $customer->setDefaultShippingAddress($shipping); $this->manager->persist($shipping); $this->insertCustomerAttributes($customerData, $customer->getId(), $createNewCustomer); if ($customerCount % 20 === 0) { $this->manager->flush(); } } catch (AdapterException $e) { $message = $e->getMessage(); $this->saveMessage($message); } } $this->manager->flush(); }