/app/code/local/AW/Autorelated/Block/Blocks/Shoppingcart.php

https://bitbucket.org/spenna/alexoo_produzione · PHP · 203 lines · 154 code · 14 blank · 35 comment · 21 complexity · 2809209852ea17c23494846a5319de9e MD5 · raw file

  1. <?php
  2. /**
  3. * aheadWorks Co.
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the EULA
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://ecommerce.aheadworks.com/AW-LICENSE.txt
  11. *
  12. * =================================================================
  13. * MAGENTO EDITION USAGE NOTICE
  14. * =================================================================
  15. * This package designed for Magento COMMUNITY edition
  16. * aheadWorks does not guarantee correct work of this extension
  17. * on any other Magento edition except Magento COMMUNITY edition.
  18. * aheadWorks does not provide extension support in case of
  19. * incorrect edition usage.
  20. * =================================================================
  21. *
  22. * @category AW
  23. * @package AW_Autorelated
  24. * @version 2.3.0
  25. * @copyright Copyright (c) 2010-2012 aheadWorks Co. (http://www.aheadworks.com)
  26. * @license http://ecommerce.aheadworks.com/AW-LICENSE.txt
  27. */
  28. class AW_Autorelated_Block_Blocks_Shoppingcart extends AW_Autorelated_Block_Blocks_Abstract
  29. {
  30. protected $_canShow = null;
  31. protected $_checkoutCart = null;
  32. protected $_storeId = null;
  33. /**
  34. * @return Mage_Checkout_Model_Cart
  35. */
  36. protected function _getCheckoutCart()
  37. {
  38. if ($this->_checkoutCart === null) {
  39. $checkoutCart = Mage::getSingleton('checkout/cart');
  40. if ($quoteId = $this->getData('_quote_id')) {
  41. /** @var $quote Mage_Sales_Model_Quote */
  42. $quote = Mage::getModel('sales/quote')->loadByIdWithoutStore($quoteId);
  43. if ($quote->getId()) {
  44. $checkoutCart = Mage::getModel('checkout/cart')->setQuote($quote);
  45. }
  46. }
  47. $this->_checkoutCart = $checkoutCart;
  48. }
  49. return $this->_checkoutCart;
  50. }
  51. protected function _getCheckoutCartProductIds()
  52. {
  53. $productIds = array();
  54. if ($this->_getCheckoutCart()) {
  55. foreach ($this->_getCheckoutCart()->getQuote()->getItemsCollection() as $quoteItem) {
  56. /** @var $quoteItem Mage_Sales_Model_Quote_Item */
  57. $productIds[] = $quoteItem->getProductId();
  58. }
  59. }
  60. return $productIds;
  61. }
  62. protected function _getStoreId()
  63. {
  64. if ($this->_storeId === null) {
  65. $this->_storeId = $this->_getCheckoutCart() ? $this->_getCheckoutCart()->getQuote()->getStore()->getId() : parent::_getStoreId();
  66. }
  67. return $this->_storeId;
  68. }
  69. protected function _getConditions($key)
  70. {
  71. return ($conditions = $this->getData($key . '/conditions')) ? $conditions : array();
  72. }
  73. protected function _getWebsiteId()
  74. {
  75. try {
  76. $store = Mage::app()->getStore($this->_getStoreId());
  77. $websiteId = $store->getWebsiteId();
  78. } catch (Exception $ex) {
  79. $websiteId = Mage::app()->getStore()->getWebsiteId();
  80. }
  81. return $websiteId;
  82. }
  83. public function canShow()
  84. {
  85. if ($this->_canShow === null) {
  86. $shoppingCart = $this->_getCheckoutCart();
  87. if ($shoppingCart->getItemsCount()) {
  88. /** @var $model AW_Autorelated_Model_Blocks_Shoppingcart_Ruleviewed */
  89. $model = Mage::getModel('awautorelated/blocks_shoppingcart_ruleviewed');
  90. $model->setWebsiteIds($this->_getWebsiteId());
  91. $model->getConditions()->loadArray($this->_getConditions('currently_viewed'), 'viewed');
  92. $this->_canShow = $model->validate($shoppingCart);
  93. } else {
  94. $this->_canShow = false;
  95. }
  96. }
  97. return $this->_canShow;
  98. }
  99. protected function _setTemplate()
  100. {
  101. if (!$this->getTemplate()) {
  102. switch ($this->getBlockPosition()) {
  103. case AW_Autorelated_Model_Source_Position::REPLACE_CROSSSELS_BLOCK:
  104. $this->setTemplate('aw_autorelated/blocks/shoppingcart/crosssells.phtml');
  105. break;
  106. default:
  107. $this->setTemplate('aw_autorelated/blocks/shoppingcart/block.phtml');
  108. break;
  109. }
  110. }
  111. return $this;
  112. }
  113. protected function _getShoppingCartProductsAttributeConditions($attrName, $attrCondition)
  114. {
  115. $attributeConditions = array();
  116. foreach ($this->_getCheckoutCart()->getQuote()->getItemsCollection() as $quoteItem) {
  117. /** @var $quoteItem Mage_Sales_Model_Quote_Item */
  118. if (($product = $quoteItem->getProduct())
  119. && ($attrValue = $product->getData($attrName))
  120. ) {
  121. if (in_array($attrCondition, array('like', 'nlike'))) {
  122. $attrValue = '%' . $attrValue . '%';
  123. }
  124. $attributeConditions[] = array($attrCondition => $attrValue);
  125. }
  126. }
  127. return $attributeConditions;
  128. }
  129. protected function _getFilteredByOptionsIds()
  130. {
  131. $options = $this->_getRelatedProducts()->getData('options');
  132. if ($options) {
  133. /** @var $productCollection AW_Autorelated_Model_Product_Collection */
  134. $productCollection = Mage::getModel('awautorelated/product_collection');
  135. foreach ($options as $option) {
  136. $attributeConditions = $this->_getShoppingCartProductsAttributeConditions($option['ATTR'], $option['CONDITION']);
  137. if ($attributeConditions) {
  138. switch ($option['ATTR']) {
  139. case 'price':
  140. $productCollection->addPriceAttributeToFilter('final_price', $attributeConditions);
  141. break;
  142. default:
  143. $productCollection->addAttributeToFilter($option['ATTR'], $attributeConditions);
  144. }
  145. }
  146. }
  147. $filteredIds = array_intersect($this->_collection->getAllIds(), $productCollection->getAllIds());
  148. } else {
  149. $filteredIds = $this->_collection->getAllIds();
  150. }
  151. return $filteredIds;
  152. }
  153. protected function _getFilteredIdsByConditions()
  154. {
  155. /** @var $rule AW_Autorelated_Model_Blocks_Shoppingcart_Rulerelated */
  156. $rule = Mage::getModel('awautorelated/blocks_shoppingcart_rulerelated');
  157. $rule->setReturnMode(AW_Autorelated_Model_Blocks_Rule::ALL_IDS_ON_NO_CONDITIONS);
  158. $rule->getConditions()->loadArray($this->_getRelatedProducts()->getData('conditions'), 'related');
  159. $rule->setWebsiteIds(Mage::app()->getWebsite()->getId());
  160. return $rule->getMatchingProductIds();
  161. }
  162. protected function _getRelatedIds()
  163. {
  164. $relatedIds = array();
  165. $filteredByOptionsIds = $this->_getFilteredByOptionsIds();
  166. if ($filteredByOptionsIds) {
  167. $relatedIds = array_intersect($filteredByOptionsIds, $this->_getFilteredIdsByConditions());
  168. }
  169. if ($relatedIds) {
  170. $relatedIds = array_diff($relatedIds, $this->_getCheckoutCartProductIds());
  171. }
  172. return $relatedIds;
  173. }
  174. protected function _renderRelatedProductsFilters()
  175. {
  176. $limit = $this->_getRelatedProducts()->getData('count');
  177. $relatedIds = $this->_getRelatedIds();
  178. if ($relatedIds) {
  179. $relatedIds = $this->_preorderIds($relatedIds);
  180. $this->_initCollectionForIds($relatedIds);
  181. $this->_collection->setPageSize($limit);
  182. $this->_collection->setCurPage(1);
  183. $this->_orderRelatedProductsCollection($this->_collection);
  184. } else {
  185. $this->_collection = null;
  186. }
  187. return $this;
  188. }
  189. }