作者:bcbr
项目:LegacyProductAttribute
/**
* Add fields for attribute values selection in our own way (since the product on has its default PSE, it has
* no attributes as far as Thelia is concerned, but we want it to have all of its template's attributes).
*
* @param TheliaFormEvent $event
*/
public function cartFormAfterBuild(TheliaFormEvent $event)
{
$sessionLocale = null;
$session = $this->request->getSession();
if ($session !== null) {
$sessionLang = $session->getLang();
if ($sessionLang !== null) {
$sessionLocale = $sessionLang->getLocale();
}
}
$product = ProductQuery::create()->findPk($this->request->getProductId());
if ($product === null || $product->getTemplate() === null) {
return;
}
$productAttributes = AttributeQuery::create()->filterByTemplate($product->getTemplate())->find();
/** @var Attribute $productAttribute */
foreach ($productAttributes as $productAttribute) {
$attributeValues = AttributeAvQuery::create()->findByAttributeId($productAttribute->getId());
$choices = [];
/** @var AttributeAv $attributeValue */
foreach ($attributeValues as $attributeValue) {
if ($sessionLocale !== null) {
$attributeValue->setLocale($sessionLocale);
}
$choices[$attributeValue->getId()] = $attributeValue->getTitle();
}
$event->getForm()->getFormBuilder()->add(static::LEGACY_PRODUCT_ATTRIBUTE_FIELD_PREFIX . $productAttribute->getId(), 'choice', ['choices' => $choices, 'required' => true]);
}
}
作者:marger
项目:theli
public function checkDuplicateRef($value, ExecutionContextInterface $context)
{
$count = ProductQuery::create()->filterByRef($value)->count();
if ($count > 0) {
$context->addViolation(Translator::getInstance()->trans("A product with reference %ref already exists. Please choose another reference.", array('%ref' => $value)));
}
}
作者:Mertiozy
项目:DeliveryDela
/**
* @param $productId
* @return array
*/
public function getDelayForProduct($productId)
{
$delay = ProductDelayQuery::create()->filterByProductId($productId)->findOneOrCreate();
$defaultDelay = (new ProductDelay())->getDefaultValue();
$product = ProductQuery::create()->findOneById($productId);
if (!$product) {
return null;
}
$quantity = $this->productHasQuantity($product);
if (true === $quantity) {
$delayMin = $delay->getDeliveryDelayMin() ? $delay->getDeliveryDelayMin() : $defaultDelay->getDeliveryDelayMin();
$delayMax = $delay->getDeliveryDelayMax() ? $delay->getDeliveryDelayMax() : $defaultDelay->getDeliveryDelayMax();
} else {
$delayMin = $delay->getRestockDelayMin() ? $delay->getRestockDelayMin() : $defaultDelay->getRestockDelayMin();
$delayMax = $delay->getRestockDelayMax() ? $delay->getRestockDelayMax() : $defaultDelay->getRestockDelayMax();
}
$startDate = date("Y-m-d");
$delivery["deliveryDateStart"] = null;
if (null !== $delay->getDeliveryDateStart() && time() < strtotime($delay->getDeliveryDateStart())) {
$startDate = $delivery["deliveryDateStart"] = $delay->getDeliveryDateStart();
}
$delivery["deliveryType"] = $delay->getDeliveryType();
$delivery["deliveryMin"] = $this->computeDeliveryDate($startDate, $delayMin);
$delivery["deliveryMax"] = $this->computeDeliveryDate($startDate, $delayMax);
return $delivery;
}
作者:bcbr
项目:LegacyProductAttribute
public function parseResults(LoopResult $loopResult)
{
$loopResult = parent::parseResults($loopResult);
/** @var LoopResultRow $loopResultRow */
foreach ($loopResult as $loopResultRow) {
// do nothing if no count is present (simple mode)
if ($loopResultRow->get('PSE_COUNT') === null) {
continue;
}
$product = ProductQuery::create()->findPk($loopResultRow->get('ID'));
// do nothing if we don't use legacy attributes for this product
$productCheckEvent = new ProductCheckEvent($product->getId());
$this->dispatcher->dispatch(LegacyProductAttributesEvents::PRODUCT_CHECK_LEGACY_ATTRIBUTES_APPLY, $productCheckEvent);
if (!$productCheckEvent->getResult()) {
continue;
}
// nothing to do if the product has no template (and thus no attributes)
if ($product->getTemplate() === null) {
continue;
}
$virtualPseCount = 1;
foreach ($product->getTemplate()->getAttributes() as $attribute) {
$virtualPseCount *= $attribute->countAttributeAvs();
}
$loopResultRow->set('PSE_COUNT', $virtualPseCount);
}
return $loopResult;
}
作者:marger
项目:theli
public function testQuery()
{
new Translator(new Container());
$export = new ProductSEOExport(new Container());
$data = $export->buildData(Lang::getDefaultLanguage());
$keys = ["ref", "visible", "product_title", "url", "page_title", "meta_description", "meta_keywords"];
sort($keys);
$rawData = $data->getData();
$max = count($rawData);
/**
* If there's more that 50 entries,
* just pick 50, it would be faster and as tested as if we test 1000 entries.
*/
if ($max > 50) {
$max = 50;
}
for ($i = 0; $i < $max; ++$i) {
$row = $rawData[$i];
$rowKeys = array_keys($row);
$this->assertTrue(sort($rowKeys));
$this->assertEquals($keys, $rowKeys);
$product = ProductQuery::create()->findOneByRef($row["ref"]);
$this->assertNotNull($product);
$this->assertEquals($product->getVisible(), $row["visible"]);
$this->assertEquals($product->getTitle(), $row["product_title"]);
$this->assertEquals($product->getMetaTitle(), $row["page_title"]);
$this->assertEquals($product->getMetaDescription(), $row["meta_description"]);
$this->assertEquals($product->getMetaKeywords(), $row["meta_keywords"]);
$this->assertEquals($product->getRewrittenUrl("en_US"), $row["url"]);
}
}
作者:alex6353
项目:theli
public function testCreate()
{
// get a product
$product = ProductQuery::create()->findOne();
// simple
$event = new MetaDataCreateOrUpdateEvent();
$event->setMetaKey('test')->setElementKey(get_class($product))->setElementId($product->getId())->setValue('test')->setDispatcher($this->dispatcher);
$action = new MetaData();
$action->createOrUpdate($event);
$created = $event->getMetaData();
$this->assertInstanceOf('Thelia\\Model\\MetaData', $created);
$this->assertFalse($created->isNew());
$this->assertEquals('test', $created->getMetaKey());
$this->assertEquals(get_class($product), $created->getElementKey());
$this->assertEquals($product->getId(), $created->getElementId());
$this->assertEquals('test', $created->getValue());
$this->assertEquals(false, $created->getIsSerialized());
// complex
$event = new MetaDataCreateOrUpdateEvent();
$event->setMetaKey('test2')->setElementKey(get_class($product))->setElementId($product->getId())->setValue(array("fr_FR" => "bonjour", "en_US" => "Hello"))->setDispatcher($this->dispatcher);
$action = new MetaData();
$action->createOrUpdate($event);
$created = $event->getMetaData();
$this->assertInstanceOf('Thelia\\Model\\MetaData', $created);
$this->assertFalse($created->isNew());
$this->assertEquals('test2', $created->getMetaKey());
$this->assertEquals(get_class($product), $created->getElementKey());
$this->assertEquals($product->getId(), $created->getElementId());
$this->assertEquals(array("fr_FR" => "bonjour", "en_US" => "Hello"), $created->getValue());
$this->assertEquals(true, $created->getIsSerialized());
return $product;
}
作者:AnimalDesig
项目:thelia-piwik-analytic
public function onMainBodyBottom(HookRenderEvent $event)
{
$options = array();
switch ($this->getRequest()->get('_view')) {
// Category page viewed
case 'category':
$categoryId = $this->getRequest()->get('category_id');
$defaultCategory = CategoryQuery::create()->findPk($categoryId);
$options[] = array('setEcommerceView', false, false, $defaultCategory->getTitle());
break;
// Product detail page viewed
// Product detail page viewed
case 'product':
$productId = $this->getRequest()->getProductId();
$product = ProductQuery::create()->findPk($productId);
if ($defaultCategoryId = $product->getDefaultCategoryId()) {
$defaultCategory = CategoryQuery::create()->findPk($defaultCategoryId);
}
$options[] = array('setEcommerceView', $product->getRef() ? $product->getRef() : $product->getId(), $product->getTitle(), isset($defaultCategory) ? $defaultCategory->getTitle() : false, false);
break;
}
if ($code = $this->generateTrackingCode($options)) {
$event->add($code);
}
}
作者:marger
项目:theli
public function checkRefDifferent($value, ExecutionContextInterface $context)
{
$originalRef = ProductQuery::create()->filterByRef($value, Criteria::EQUAL)->count();
if ($originalRef !== 0) {
$context->addViolation($this->translator->trans('This product reference is already assigned to another product.'));
}
}
作者:badela
项目:theli
public function checkProduct($value, ExecutionContextInterface $context)
{
$product = ProductQuery::create()->findPk($value);
if (is_null($product) || $product->getVisible() == 0) {
throw new ProductNotFoundException(sprintf(Translator::getInstance()->trans("this product id does not exists : %d"), $value));
}
}
作者:bcbr
项目:LegacyProductAttribute
/**
* Get the untaxed price and, if the product is in sale, promo price of a product.
*
* @param ProductGetPricesEvent $event
*/
public function getPrices(ProductGetPricesEvent $event)
{
$product = ProductQuery::create()->findPk($event->getProductId());
if (null === $product) {
throw new \InvalidArgumentException('No product given');
}
$currency = CurrencyQuery::create()->findPk($event->getCurrencyId());
if (null === $currency) {
$currency = CurrencyQuery::create()->findOneByByDefault(true);
}
// get the base prices given in the event
// or, by default, the prices of the product's default PSE
if (null !== $event->getBasePrices()) {
$price = $event->getBasePrices()->getPrice();
$promoPrice = $event->getBasePrices()->getPromoPrice();
} else {
$prices = $product->getDefaultSaleElements()->getPricesByCurrency($currency);
$price = $prices->getPrice();
$promoPrice = $prices->getPromoPrice();
}
// adjust the prices with the configured price delta from legacy attributes
$legacyProductAttributeValues = LegacyProductAttributeValueQuery::create()->filterByProductId($product->getId())->filterByAttributeAvId(array_values($event->getLegacyProductAttributes()), Criteria::IN)->find();
/** @var LegacyProductAttributeValue $legacyProductAttributeValue */
foreach ($legacyProductAttributeValues as $legacyProductAttributeValue) {
$legacyProductAttributeValuePrice = $legacyProductAttributeValue->getPriceForCurrency($event->getCurrencyId());
if ($legacyProductAttributeValuePrice === null) {
continue;
}
$price += $legacyProductAttributeValuePrice->getDelta();
$promoPrice += $legacyProductAttributeValuePrice->getDelta();
}
$event->setPrices(new ProductPriceTools($price, $promoPrice));
}
作者:roadster3
项目:thelia-modules-Vie
public function find(FindViewEvent $event)
{
$objectType = $event->getObjectType();
$objectId = $event->getObjectId();
// Try to find a direct match. A view is defined for the object.
if (null !== ($viewObj = ViewQuery::create()->filterBySourceId($objectId)->findOneBySource($objectType))) {
$viewName = $viewObj->getView();
if (!empty($viewName)) {
$event->setView($viewName)->setViewObject($viewObj);
return;
}
}
$foundView = $sourceView = null;
if ($objectType == 'category') {
$foundView = $this->searchInParents($objectId, $objectType, CategoryQuery::create(), false, $sourceView);
} elseif ($objectType == 'folder') {
$foundView = $this->searchInParents($objectId, $objectType, FolderQuery::create(), false, $sourceView);
} elseif ($objectType == 'product') {
if (null !== ($product = ProductQuery::create()->findPk($objectId))) {
$foundView = $this->searchInParents($product->getDefaultCategoryId(), 'category', CategoryQuery::create(), true, $sourceView);
}
} elseif ($objectType == 'content') {
if (null !== ($content = ContentQuery::create()->findPk($objectId))) {
$foundView = $this->searchInParents($content->getDefaultFolderId(), 'folder', FolderQuery::create(), true, $sourceView);
}
}
$event->setView($foundView)->setViewObject($sourceView);
}
作者:vigourouxjulie
项目:theli
public function getData()
{
$locale = $this->language->getLocale();
$urlJoin = new Join(ProductTableMap::ID, RewritingUrlTableMap::VIEW_ID, Criteria::LEFT_JOIN);
$productJoin = new Join(ProductTableMap::ID, ProductI18nTableMap::ID, Criteria::LEFT_JOIN);
$query = ProductQuery::create()->addSelfSelectColumns()->addJoinObject($urlJoin, 'rewriting_url_join')->addJoinCondition('rewriting_url_join', RewritingUrlTableMap::VIEW_LOCALE . ' = ?', $locale, null, \PDO::PARAM_STR)->addJoinCondition('rewriting_url_join', RewritingUrlTableMap::VIEW . ' = ?', (new Product())->getRewrittenUrlViewName(), null, \PDO::PARAM_STR)->addJoinCondition('rewriting_url_join', 'ISNULL(' . RewritingUrlTableMap::REDIRECTED . ')')->addJoinObject($productJoin, 'product_join')->addJoinCondition('product_join', ProductI18nTableMap::LOCALE . ' = ?', $locale, null, \PDO::PARAM_STR)->addAsColumn('product_i18n_TITLE', ProductI18nTableMap::TITLE)->addAsColumn('product_seo_TITLE', ProductI18nTableMap::META_TITLE)->addAsColumn('product_seo_META_DESCRIPTION', ProductI18nTableMap::META_DESCRIPTION)->addAsColumn('product_seo_META_KEYWORDS', ProductI18nTableMap::META_KEYWORDS)->addAsColumn('product_URL', RewritingUrlTableMap::URL);
return $query;
}
作者:fachriz
项目:theli
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$currency = CurrencyQuery::create()->filterByCode('EUR')->findOne();
// Find a product
$this->freeProduct = ProductQuery::create()->findOne();
$this->originalPrice = $this->freeProduct->getDefaultSaleElements()->getPricesByCurrency($currency)->getPrice();
$this->originalPromo = $this->freeProduct->getDefaultSaleElements()->getPromo();
$this->freeProduct->getDefaultSaleElements()->setPromo(false)->save();
}
作者:marger
项目:theli
/**
* @param Lang $lang
* @return array|\Propel\Runtime\ActiveQuery\ModelCriteria
*/
public function buildDataSet(Lang $lang)
{
$locale = $this->locale = $lang->getLocale();
$query = ProductQuery::create();
$urlJoin = new Join(ProductTableMap::ID, RewritingUrlTableMap::VIEW_ID, Criteria::LEFT_JOIN);
$productJoin = new Join(ProductTableMap::ID, ProductI18nTableMap::ID, Criteria::LEFT_JOIN);
$query->addJoinObject($urlJoin, "rewriting_url_join")->addJoinCondition("rewriting_url_join", RewritingUrlTableMap::VIEW_LOCALE . " = ?", $locale, null, \PDO::PARAM_STR)->addJoinCondition("rewriting_url_join", RewritingUrlTableMap::VIEW . " = ?", (new Product())->getRewrittenUrlViewName(), null, \PDO::PARAM_STR)->addJoinCondition("rewriting_url_join", "ISNULL(" . RewritingUrlTableMap::REDIRECTED . ")")->addJoinObject($productJoin, "product_join")->addJoinCondition("product_join", ProductI18nTableMap::LOCALE . " = ?", $locale, null, \PDO::PARAM_STR);
$query->addAsColumn("product_i18n_TITLE", ProductI18nTableMap::TITLE)->addAsColumn("product_REF", ProductTableMap::REF)->addAsColumn("product_VISIBLE", ProductTableMap::VISIBLE)->addAsColumn("product_seo_TITLE", ProductI18nTableMap::META_TITLE)->addAsColumn("product_seo_META_DESCRIPTION", ProductI18nTableMap::META_DESCRIPTION)->addAsColumn("product_seo_META_KEYWORDS", ProductI18nTableMap::META_KEYWORDS)->addAsColumn("product_URL", RewritingUrlTableMap::URL)->select(["product_REF", "product_VISIBLE", "product_i18n_TITLE", "product_URL", "product_seo_TITLE", "product_seo_META_DESCRIPTION", "product_seo_META_KEYWORDS"]);
return $query;
}
作者:alex6353
项目:theli
/**
*
* count all products for current category and sub categories
*
* /!\ the number of queries is exponential, use it with caution
*
* @return int
*/
public function countAllProducts()
{
$children = CategoryQuery::findAllChild($this->getId());
array_push($children, $this);
$countProduct = 0;
foreach ($children as $child) {
$countProduct += ProductQuery::create()->filterByCategory($child)->count();
}
return $countProduct;
}
作者:AnimalDesig
项目:thelia-piwik-analytic
public function trackOrder(OrderEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
$order = $event->getPlacedOrder();
$taxTotal = 0;
foreach ($order->getOrderProducts() as $orderProduct) {
$product = ProductQuery::create()->findPk($orderProduct->getVirtualColumn('product_id'));
$defaultCategory = CategoryQuery::create()->findPk($product->getDefaultCategoryId());
$taxTotal += $orderProduct->getVirtualColumn('TOTAL_TAX');
$this->tracker->addEcommerceItem($orderProduct->getProductSaleElementsRef() || $orderProduct->getProductRef() || $orderProduct->getId() || $orderProduct->getProductSaleElementsId(), $orderProduct->getTitle(), $defaultCategory->getTitle(), $orderProduct->getPrice(), $orderProduct->getQuantity());
}
$this->tracker->doTrackEcommerceOrder($order->getRef(), $order->getTotalAmount($taxTotal, true, true), $order->getTotalAmount($taxTotal, false, true), $taxTotal, $order->getPostage() + $order->getPostageTax(), $order->getDiscount());
}
作者:alex6353
项目:theli
/**
* Delete a product template entry
*
* @param \Thelia\Core\Event\Template\TemplateDeleteEvent $event
*/
public function delete(TemplateDeleteEvent $event)
{
if (null !== ($template = TemplateQuery::create()->findPk($event->getTemplateId()))) {
// Check if template is used by a product
$product_count = ProductQuery::create()->findByTemplateId($template->getId())->count();
if ($product_count <= 0) {
$template->setDispatcher($event->getDispatcher())->delete();
}
$event->setTemplate($template);
$event->setProductCount($product_count);
}
}
作者:marger
项目:theli
public function onMainBeforeContent(HookRenderEvent $event)
{
if ($this->securityContext->isGranted(["ADMIN"], [AdminResources::PRODUCT], [], [AccessManager::VIEW])) {
$products = ProductQuery::create()->filterByVirtual(1)->filterByVisible(1)->count();
if ($products > 0) {
$deliveryModule = ModuleQuery::create()->retrieveVirtualProductDelivery();
if (false === $deliveryModule) {
$event->add($this->render('virtual-delivery-warning.html'));
}
}
}
}
作者:NandoKstroNe
项目:theli
public function testGetAction()
{
$client = static::createClient();
$product = ProductQuery::create()->joinProductImage()->findOne();
if (null === $product) {
$this->markTestSkipped("This test can't be run as there is no product that has an image");
}
$productImage = $product->getProductImages()->get(0);
$client->request('GET', '/api/products/' . $product->getId() . '/images/' . $productImage->getId() . '?sign=' . $this->getSignParameter(""), [], [], $this->getServerParameters());
$this->assertEquals(200, $client->getResponse()->getStatusCode(), 'Http status code must be 200');
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertCount(1, $content, 'image get action must retrieve 1 image');
}
作者:alex6353
项目:theli
public function testSearchByIdComplex()
{
$product = ProductQuery::create()->orderById(Criteria::ASC)->findOne();
if (null === $product) {
$product = new \Thelia\Model\Product();
$product->setDefaultCategory(0);
$product->setVisible(1);
$product->setTitle('foo');
$product->save();
}
$otherParameters = array("visible" => "*", "complex" => 1);
$this->baseTestSearchById($product->getId(), $otherParameters);
}