/** * @param ListProduct $listProduct * @return Product */ public static function createFromListProduct(ListProduct $listProduct) { $product = new self($listProduct->getId(), $listProduct->getVariantId(), $listProduct->getNumber()); foreach ($listProduct as $key => $value) { $product->{$key} = $value; } return $product; }
private function createSelection(ListProduct $listProduct, array $optionNames) { $options = $this->helper->getProductOptionsByName($listProduct->getId(), $optionNames); $selection = array(); foreach ($options as $option) { $groupId = $option['group_id']; $selection[$groupId] = $option['id']; } return $selection; }
/** * Calculates the cheapest price considering the variant min purchase * @param Struct\ListProduct $product * @param Struct\Product\PriceRule $priceRule * @param Struct\ProductContextInterface $context * @return Struct\Product\Price */ private function calculateCheapestAvailablePrice(Struct\ListProduct $product, Struct\Product\PriceRule $priceRule, Struct\ProductContextInterface $context) { $priceRule->setPrice($priceRule->getUnit()->getMinPurchase() * $priceRule->getPrice()); $priceRule->getUnit()->setPurchaseUnit($priceRule->getUnit()->getMinPurchase() * $priceRule->getUnit()->getPurchaseUnit()); $priceRule->setPseudoPrice($priceRule->getUnit()->getMinPurchase() * $priceRule->getPseudoPrice()); $tax = $context->getTaxRule($product->getTax()->getId()); return $this->calculatePriceStruct($priceRule, $tax, $context); }
/** * @inheritdoc */ public function getProductAttribute(Struct\ListProduct $product) { $attribute = new Struct\Product\MarketingAttribute(); $today = new \DateTime(); $diff = $today->diff($product->getCreatedAt()); $marker = (int) $this->config->get('markAsNew'); $attribute->setIsNew($diff->days <= $marker || $product->getCreatedAt() > $today); $attribute->setComingSoon($product->getReleaseDate() && $product->getReleaseDate() > $today); $attribute->setIsTopSeller($product->getSales() >= $this->config->get('markAsTopSeller')); return $attribute; }
/** * Add premium products to cart * Used internally in sBasket and in CheckoutController * * @return bool|int */ public function sInsertPremium() { static $lastPremium; $sBasketAmount = $this->sGetAmount(); $sBasketAmount = empty($sBasketAmount["totalAmount"]) ? 0 : $sBasketAmount["totalAmount"]; $sBasketAmount = (double) $sBasketAmount; $addPremium = $this->front->Request()->getQuery('sAddPremium'); if (empty($addPremium)) { $deletePremium = $this->db->fetchCol('SELECT basket.id FROM s_order_basket basket LEFT JOIN s_addon_premiums premium ON premium.ordernumber_export = basket.ordernumber AND premium.startprice <= ? WHERE basket.modus = 1 AND premium.id IS NULL AND basket.sessionID = ?', array($sBasketAmount, $this->session->get('sessionId'))); if (empty($deletePremium)) { return true; } $this->db->delete('s_order_basket', array('id IN (?)' => $deletePremium)); return true; } if (isset($lastPremium) && $lastPremium == $addPremium) { return false; } $lastPremium = $addPremium; $this->db->delete('s_order_basket', array('sessionID = ?' => $this->session->get('sessionId'), 'modus = 1')); $premium = $this->db->fetchRow(' SELECT premium.id, detail.ordernumber, article.id as articleID, article.name as articleName, article.main_detail_id, detail.id as variantID, detail.additionaltext, premium.ordernumber_export, article.configurator_set_id FROM s_addon_premiums premium, s_articles_details detail, s_articles article, s_articles_details detail2 WHERE detail.ordernumber = ? AND premium.startprice <= ? AND premium.ordernumber = detail2.ordernumber AND detail2.articleID = detail.articleID AND detail.articleID = article.id', array($addPremium, $sBasketAmount)); if (!$premium) { return false; } // Load translations for article or variant if ($premium['main_detail_id'] != $premium['variantID']) { $premium = $this->moduleManager->Articles()->sGetTranslation($premium, $premium['variantID'], "variant"); } else { $premium = $this->moduleManager->Articles()->sGetTranslation($premium, $premium['articleID'], "article"); } if ($premium['configurator_set_id'] > 0) { $premium = $this->moduleManager->Articles()->sGetTranslation($premium, $premium["variantID"], "variant"); $product = new StoreFrontBundle\Struct\ListProduct($premium['articleID'], $premium["variantID"], $premium['ordernumber']); $product->setAdditional($premium['additionaltext']); $context = $this->contextService->getShopContext(); $product = $this->additionalTextService->buildAdditionalText($product, $context); $premium['additionaltext'] = $product->getAdditional(); } if (!empty($premium['configurator_set_id'])) { $number = $premium['ordernumber']; } else { $number = $premium['ordernumber_export']; } return $this->db->insert('s_order_basket', array('sessionID' => $this->session->get('sessionId'), 'articlename' => trim($premium["articleName"] . " " . $premium["additionaltext"]), 'articleID' => $premium['articleID'], 'ordernumber' => $number, 'quantity' => 1, 'price' => 0, 'netprice' => 0, 'tax_rate' => 0, 'datum' => new Zend_Date(), 'modus' => 1, 'currencyFactor' => $this->sSYSTEM->sCurrency["factor"])); }
/** * Internal function which converts only the data of a list product. * Associated data won't converted. * * @param StoreFrontBundle\Struct\ListProduct $product * @return array */ private function getListProductData(StoreFrontBundle\Struct\ListProduct $product) { $createDate = null; if ($product->getCreatedAt()) { $createDate = $product->getCreatedAt()->format('Y-m-d'); } $data = array('articleID' => $product->getId(), 'articleDetailsID' => $product->getVariantId(), 'ordernumber' => $product->getNumber(), 'highlight' => $product->highlight(), 'description' => $product->getShortDescription(), 'description_long' => $product->getLongDescription(), 'esd' => $product->hasEsd(), 'articleName' => $product->getName(), 'taxID' => $product->getTax()->getId(), 'tax' => $product->getTax()->getTax(), 'instock' => $product->getStock(), 'isAvailable' => $product->isAvailable(), 'weight' => $product->getWeight(), 'shippingtime' => $product->getShippingTime(), 'pricegroupActive' => false, 'pricegroupID' => null, 'length' => $product->getLength(), 'height' => $product->getHeight(), 'width' => $product->getWidth(), 'laststock' => $product->isCloseouts(), 'additionaltext' => $product->getAdditional(), 'datum' => $createDate, 'sales' => $product->getSales(), 'filtergroupID' => null, 'priceStartingFrom' => null, 'pseudopricePercent' => null, 'sVariantArticle' => null, 'sConfigurator' => $product->hasConfigurator(), 'metaTitle' => $product->getMetaTitle(), 'shippingfree' => $product->isShippingFree(), 'suppliernumber' => $product->getManufacturerNumber(), 'notification' => $product->allowsNotification(), 'ean' => $product->getEan(), 'keywords' => $product->getKeywords(), 'sReleasedate' => $this->dateToString($product->getReleaseDate()), 'template' => $product->getTemplate()); if ($product->hasAttribute('core')) { $attributes = $product->getAttribute('core')->toArray(); unset($attributes['id'], $attributes['articleID'], $attributes['articledetailsID']); $data = array_merge($data, $attributes); } $data['attributes'] = $product->getAttributes(); if ($product->getManufacturer()) { $manufacturer = array('supplierName' => $product->getManufacturer()->getName(), 'supplierImg' => $product->getManufacturer()->getCoverFile(), 'supplierID' => $product->getManufacturer()->getId(), 'supplierDescription' => $product->getManufacturer()->getDescription()); if (!empty($manufacturer['supplierImg'])) { $manufacturer['supplierImg'] = $this->mediaService->getUrl($manufacturer['supplierImg']); } $data = array_merge($data, $manufacturer); $data['supplier_attributes'] = $product->getManufacturer()->getAttributes(); } if ($product->hasAttribute('marketing')) { /**@var $marketing StoreFrontBundle\Struct\Product\MarketingAttribute */ $marketing = $product->getAttribute('marketing'); $data['newArticle'] = $marketing->isNew(); $data['sUpcoming'] = $marketing->comingSoon(); $data['topseller'] = $marketing->isTopSeller(); } $today = new \DateTime(); if ($product->getReleaseDate() && $product->getReleaseDate() > $today) { $data['sReleasedate'] = $product->getReleaseDate()->format('Y-m-d'); } return $data; }
/** * @param ListProduct $listProduct * @param ListProduct $product */ public function assignProductData(ListProduct $listProduct, ListProduct $product) { $product->setShippingFree($listProduct->isShippingFree()); $product->setMainVariantId($listProduct->getMainVariantId()); $product->setAllowsNotification($listProduct->allowsNotification()); $product->setHighlight($listProduct->highlight()); $product->setUnit($listProduct->getUnit()); $product->setTax($listProduct->getTax()); $product->setPrices($listProduct->getPrices()); $product->setManufacturer($listProduct->getManufacturer()); $product->setCover($listProduct->getCover()); $product->setCheapestPrice($listProduct->getCheapestPrice()); $product->setName($listProduct->getName()); $product->setAdditional($listProduct->getAdditional()); $product->setCloseouts($listProduct->isCloseouts()); $product->setEan($listProduct->getEan()); $product->setHeight($listProduct->getHeight()); $product->setKeywords($listProduct->getKeywords()); $product->setLength($listProduct->getLength()); $product->setLongDescription($listProduct->getLongDescription()); $product->setMinStock($listProduct->getMinStock()); $product->setReleaseDate($listProduct->getReleaseDate()); $product->setShippingTime($listProduct->getShippingTime()); $product->setShortDescription($listProduct->getShortDescription()); $product->setStock($listProduct->getStock()); $product->setWeight($listProduct->getWeight()); $product->setWidth($listProduct->getWidth()); $product->setPriceGroup($listProduct->getPriceGroup()); $product->setCreatedAt($listProduct->getCreatedAt()); $product->setPriceRules($listProduct->getPriceRules()); $product->setCheapestPriceRule($listProduct->getCheapestPriceRule()); $product->setManufacturerNumber($listProduct->getManufacturerNumber()); $product->setMetaTitle($listProduct->getMetaTitle()); $product->setTemplate($listProduct->getTemplate()); $product->setHasConfigurator($listProduct->hasConfigurator()); $product->setSales($listProduct->getSales()); $product->setHasEsd($listProduct->hasEsd()); $product->setEsd($listProduct->getEsd()); $product->setIsPriceGroupActive($listProduct->isPriceGroupActive()); $product->setBlockedCustomerGroupIds($listProduct->getBlockedCustomerGroupIds()); $product->setVoteAverage($listProduct->getVoteAverage()); $product->setHasAvailableVariant($listProduct->hasAvailableVariant()); $product->setCheapestUnitPrice($listProduct->getCheapestUnitPrice()); $product->setFallbackPriceCount($listProduct->getFallbackPriceCount()); $product->setCustomerPriceCount($listProduct->getCustomerPriceCount()); foreach ($listProduct->getAttributes() as $name => $attribute) { $product->addAttribute($name, $attribute); } foreach ($listProduct->getStates() as $state) { $product->addState($state); } }
/** * Checks if the provided product is allowed to display in the store front for * the provided context. * * @param Struct\ListProduct $product * @param Struct\ProductContextInterface $context * @return bool */ private function isProductValid(Struct\ListProduct $product, Struct\ProductContextInterface $context) { if (in_array($context->getCurrentCustomerGroup()->getId(), $product->getBlockedCustomerGroupIds())) { return false; } $prices = $product->getPrices(); if (empty($prices)) { return false; } if (!$product->hasAvailableVariant()) { return false; } return true; }
/** * executes the current product export * * @param resource $handleResource used as a file or the stdout to fetch the smarty output */ public function executeExport($handleResource) { fwrite($handleResource, $this->sSmarty->fetch('string:' . $this->sSettings['header'], $this->sFeedID)); $context = $this->contextService->getShopContext(); $sql = $this->sCreateSql(); $result = $this->db->query($sql); if ($result === false) { return; } // Update db with the latest values $count = (int) $result->rowCount(); $this->db->update('s_export', array('last_export' => new Zend_Date(), 'cache_refreshed' => new Zend_Date(), 'count_articles' => $count), array('id = ?' => $this->sFeedID)); // fetches all required data to smarty $rows = array(); for ($rowIndex = 1; $row = $result->fetch(); $rowIndex++) { if (!empty($row['group_ordernumber_2'])) { $row['group_ordernumber'] = $this->_decode_line($row['group_ordernumber_2']); $row['group_pricenet'] = explode(';', $row['group_pricenet_2']); $row['group_price'] = explode(';', $row['group_price_2']); $row['group_instock'] = explode(';', $row['group_instock_2']); $row['group_active'] = explode(';', $row['group_active_2']); unset($row['group_ordernumber_2'], $row['group_pricenet_2']); unset($row['group_price_2'], $row['group_instock_2'], $row['group_active_2']); for ($i = 1; $i <= 10; $i++) { if (!empty($row['group_group' . $i])) { $row['group_group' . $i] = $this->_decode_line($row['group_group' . $i]); } else { unset($row['group_group' . $i]); } if (!empty($row['group_option' . $i])) { $row['group_option' . $i] = $this->_decode_line($row['group_option' . $i]); } else { unset($row['group_option' . $i]); } } unset($row['group_additionaltext']); } elseif (!empty($row['group_ordernumber'])) { $row['group_ordernumber'] = $this->_decode_line($row['group_ordernumber']); $row['group_additionaltext'] = $this->_decode_line($row['group_additionaltext']); $row['group_pricenet'] = explode(';', $row['group_pricenet']); $row['group_price'] = explode(';', $row['group_price']); $row['group_instock'] = explode(';', $row['group_instock']); $row['group_active'] = explode(';', $row['group_active']); } if (!empty($row['article_translation_fallback'])) { $translation = $this->sMapTranslation('article', $row['article_translation_fallback']); if ($row['main_detail_id'] != $row['articledetailsID']) { unset($translation['additionaltext']); } $row = array_merge($row, $translation); } if (!empty($row['article_translation'])) { $translation = $this->sMapTranslation('article', $row['article_translation']); if ($row['main_detail_id'] != $row['articledetailsID']) { unset($translation['additionaltext']); } $row = array_merge($row, $translation); } if (!empty($row['detail_translation_fallback'])) { $translation = $this->sMapTranslation('detail', $row['detail_translation_fallback']); $row = array_merge($row, $translation); } if (!empty($row['detail_translation'])) { $translation = $this->sMapTranslation('detail', $row['detail_translation']); $row = array_merge($row, $translation); } $row['name'] = htmlspecialchars_decode($row['name']); $row['supplier'] = htmlspecialchars_decode($row['supplier']); //cast it to float to prevent the division by zero warning $row['purchaseunit'] = floatval($row['purchaseunit']); $row['referenceunit'] = floatval($row['referenceunit']); if (!empty($row['purchaseunit']) && !empty($row['referenceunit'])) { $row['referenceprice'] = Shopware()->Modules()->Articles()->calculateReferencePrice($row['price'], $row['purchaseunit'], $row['referenceunit']); } if ($row['configurator'] > 0) { if (empty($this->sSettings["variant_export"]) || $this->sSettings["variant_export"] == 1) { $row['group_additionaltext'] = array(); if (!empty($row['group_ordernumber'])) { foreach ($row['group_ordernumber'] as $orderNumber) { $product = new StoreFrontBundle\Struct\ListProduct((int) $row['articleID'], (int) $row["articledetailsID"], $orderNumber); $product->setAdditional($row['additionaltext']); $product = $this->additionalTextService->buildAdditionalText($product, $context); if (array_key_exists($orderNumber, $row['group_additionaltext'])) { $row['group_additionaltext'][$orderNumber] = $product->getAdditional(); } if ($orderNumber == $row['ordernumber']) { $row['additionaltext'] = $product->getAdditional(); } } } } $product = new StoreFrontBundle\Struct\ListProduct((int) $row['articleID'], (int) $row["articledetailsID"], $row['ordernumber']); $product->setAdditional($row['additionaltext']); $product = $this->additionalTextService->buildAdditionalText($product, $context); $row['additionaltext'] = $product->getAdditional(); } $rows[] = $row; if ($rowIndex == $count || count($rows) >= 50) { @set_time_limit(30); $this->sSmarty->assign('sArticles', $rows); $rows = array(); $template = 'string:{foreach $sArticles as $sArticle}' . $this->sSettings['body'] . '{/foreach}'; fwrite($handleResource, $this->sSmarty->fetch($template, $this->sFeedID)); } } fwrite($handleResource, $this->sSmarty->fetch('string:' . $this->sSettings['footer'], $this->sFeedID)); fclose($handleResource); }
/** * Creates different links for the product like `add to basket`, `add to note`, `view detail page`, ... * * @param StoreFrontBundle\Struct\ListProduct $product * @param null $categoryId * @return array */ private function getLinksOfProduct(StoreFrontBundle\Struct\ListProduct $product, $categoryId = null) { $baseFile = $this->config->get('baseFile'); $context = $this->contextService->getShopContext(); $detail = $baseFile . "?sViewport=detail&sArticle=" . $product->getId(); if ($categoryId) { $detail .= '&sCategory=' . $categoryId; } $rewrite = Shopware()->Modules()->Core()->sRewriteLink($detail, $product->getName()); $basket = $baseFile . "?sViewport=basket&sAdd=" . $product->getNumber(); $note = $baseFile . "?sViewport=note&sAdd=" . $product->getNumber(); $friend = $baseFile . "?sViewport=tellafriend&sDetails=" . $product->getId(); $pdf = $baseFile . "?sViewport=detail&sDetails=" . $product->getId() . "&sLanguage=" . $context->getShop()->getId() . "&sPDF=1"; return array('linkBasket' => $basket, 'linkDetails' => $detail, 'linkDetailsRewrited' => $rewrite, 'linkNote' => $note, 'linkTellAFriend' => $friend, 'linkPDF' => $pdf); }
/** * Get article data for sAddArticle * * @param int $id Article ordernumber * @return array|false Article data, or false if none found */ private function getArticleForAddArticle($id) { $sql = "\n SELECT s_articles.id AS articleID, s_articles.main_detail_id, name AS articleName, taxID,\n additionaltext, s_articles_details.shippingfree, laststock, instock,\n s_articles_details.id as articledetailsID, ordernumber,\n s_articles.configurator_set_id\n FROM s_articles, s_articles_details\n WHERE s_articles_details.ordernumber = ?\n AND s_articles_details.articleID = s_articles.id\n AND s_articles.active = 1\n AND (\n SELECT articleID\n FROM s_articles_avoid_customergroups\n WHERE articleID = s_articles.id AND customergroupID = ?\n ) IS NULL\n "; $article = $this->db->fetchRow($sql, array($id, $this->sSYSTEM->sUSERGROUPDATA["id"])); $article = $this->eventManager->filter('Shopware_Modules_Basket_getArticleForAddArticle_FilterArticle', $article, array("id" => $id, 'subject' => $this, "partner" => $this->sSYSTEM->_SESSION["sPartner"])); if (!$article) { return false; } $article = $this->moduleManager->Articles()->sGetTranslation($article, $article['articleID'], "article"); $article = $this->moduleManager->Articles()->sGetTranslation($article, $article['articledetailsID'], "variant"); if ($article['configurator_set_id'] > 0) { $product = new StoreFrontBundle\Struct\ListProduct((int) $article['articleID'], (int) $article["articledetailsID"], $article['ordernumber']); $product->setAdditional($article['additionaltext']); $context = $this->contextService->getShopContext(); $product = $this->additionalTextService->buildAdditionalText($product, $context); $article['additionaltext'] = $product->getAdditional(); } return $article; }
/** * Returns the highest price group discount for the provided product. * * The price groups are stored in the provided context object. * If the product has no configured price group or the price group has no discount defined for the * current customer group, the function returns null. * * @param Struct\ListProduct $product * @param Struct\ProductContextInterface $context * @param $quantity * @return null|Struct\Product\PriceDiscount */ private function getHighestQuantityDiscount(Struct\ListProduct $product, Struct\ProductContextInterface $context, $quantity) { $priceGroups = $context->getPriceGroups(); if (empty($priceGroups)) { return null; } $id = $product->getPriceGroup()->getId(); if (!isset($priceGroups[$id])) { return null; } $priceGroup = $priceGroups[$id]; /**@var $highest Struct\Product\PriceDiscount*/ $highest = null; foreach ($priceGroup->getDiscounts() as $discount) { if ($discount->getQuantity() > $quantity) { continue; } if (!$highest) { $highest = $discount; continue; } if ($highest->getPercent() < $discount->getPercent()) { $highest = $discount; } } return $highest; }
/** * Checks if the provided product is allowed to display in the store front for * the provided context. * * @param Struct\ListProduct $product * @param Struct\ProductContextInterface $context * @return bool */ private function isProductValid(Struct\ListProduct $product, Struct\ProductContextInterface $context) { if (in_array($context->getCurrentCustomerGroup()->getId(), $product->getBlockedCustomerGroupIds())) { return false; } $prices = $product->getPrices(); if (empty($prices)) { return false; } if (!$product->hasAvailableVariant()) { return false; } $ids = array_map(function (Struct\Category $category) { return $category->getId(); }, $product->getCategories()); return in_array($context->getShop()->getCategory()->getId(), $ids); }
/** * Iterates the attribute data and assigns the attribute struct to the product. * * @param Struct\ListProduct $product * @param $data */ private function assignAttributeData(Struct\ListProduct $product, $data) { $translation = $this->getProductTranslation($data); $attribute = $this->attributeHydrator->hydrate($this->extractFields('__productAttribute_', $data)); foreach ($translation as $key => $value) { if ($attribute->exists($key)) { $attribute->set($key, $value); } } $product->addAttribute('core', $attribute); }
/** * @param array $result * @return ListProduct[] */ private function buildListProducts(array $result) { $products = []; foreach ($result as $item) { $number = $item['number']; $product = new ListProduct($item['articleId'], $item['articleDetailId'], $item['number']); if ($item['additionalText']) { $product->setAdditional($item['additionalText']); } $products[$number] = $product; } return $products; }
/** * For the provided article id, returns the associated variant numbers and additional texts * * @param $articleId * @param $mainDetailId * @return array */ private function getVariantDetailsForPremiumArticles($articleId, $mainDetailId) { $context = $this->contextService->getShopContext(); $sql = "SELECT id, ordernumber, additionaltext\n FROM s_articles_details\n WHERE articleID = :articleId AND kind != 3"; $variantsData = Shopware()->Db()->fetchAll($sql, array('articleId' => $articleId)); foreach ($variantsData as $variantData) { $product = new StoreFrontBundle\Struct\ListProduct($articleId, $variantData['id'], $variantData['ordernumber']); if ($variantData['id'] == $mainDetailId) { $variantData = Shopware()->Modules()->Articles()->sGetTranslation($variantData, $articleId, "article"); } else { $variantData = Shopware()->Modules()->Articles()->sGetTranslation($variantData, $variantData['id'], "variant"); } $product->setAdditional($variantData['additionaltext']); $products[$variantData['ordernumber']] = $product; } $products = $this->additionalTextService->buildAdditionalTextLists($products, $context); return array_map(function (StoreFrontBundle\Struct\ListProduct $elem) { return array('ordernumber' => $elem->getNumber(), 'additionaltext' => $elem->getAdditional()); }, $products); }