1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Downloadable_Model_Observer
35: {
36: const XML_PATH_DISABLE_GUEST_CHECKOUT = 'catalog/downloadable/disable_guest_checkout';
37:
38: 39: 40: 41: 42: 43:
44: public function prepareProductSave($observer)
45: {
46: $request = $observer->getEvent()->getRequest();
47: $product = $observer->getEvent()->getProduct();
48:
49: if ($downloadable = $request->getPost('downloadable')) {
50: $product->setDownloadableData($downloadable);
51: }
52:
53: return $this;
54: }
55:
56: 57: 58: 59: 60: 61:
62: public function saveDownloadableOrderItem($observer)
63: {
64: $orderItem = $observer->getEvent()->getItem();
65: if (!$orderItem->getId()) {
66:
67: return $this;
68: }
69: $product = $orderItem->getProduct();
70: if ($product && $product->getTypeId() != Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) {
71: return $this;
72: }
73: if (Mage::getModel('downloadable/link_purchased')->load($orderItem->getId(), 'order_item_id')->getId()) {
74: return $this;
75: }
76: if (!$product) {
77: $product = Mage::getModel('catalog/product')
78: ->setStoreId($orderItem->getOrder()->getStoreId())
79: ->load($orderItem->getProductId());
80: }
81: if ($product->getTypeId() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) {
82: $links = $product->getTypeInstance(true)->getLinks($product);
83: if ($linkIds = $orderItem->getProductOptionByCode('links')) {
84: $linkPurchased = Mage::getModel('downloadable/link_purchased');
85: Mage::helper('core')->copyFieldset(
86: 'downloadable_sales_copy_order',
87: 'to_downloadable',
88: $orderItem->getOrder(),
89: $linkPurchased
90: );
91: Mage::helper('core')->copyFieldset(
92: 'downloadable_sales_copy_order_item',
93: 'to_downloadable',
94: $orderItem,
95: $linkPurchased
96: );
97: $linkSectionTitle = (
98: $product->getLinksTitle()?
99: $product->getLinksTitle():Mage::getStoreConfig(Mage_Downloadable_Model_Link::XML_PATH_LINKS_TITLE)
100: );
101: $linkPurchased->setLinkSectionTitle($linkSectionTitle)
102: ->save();
103: foreach ($linkIds as $linkId) {
104: if (isset($links[$linkId])) {
105: $linkPurchasedItem = Mage::getModel('downloadable/link_purchased_item')
106: ->setPurchasedId($linkPurchased->getId())
107: ->setOrderItemId($orderItem->getId());
108:
109: Mage::helper('core')->copyFieldset(
110: 'downloadable_sales_copy_link',
111: 'to_purchased',
112: $links[$linkId],
113: $linkPurchasedItem
114: );
115: $linkHash = strtr(base64_encode(microtime() . $linkPurchased->getId() . $orderItem->getId()
116: . $product->getId()), '+/=', '-_,');
117: $numberOfDownloads = $links[$linkId]->getNumberOfDownloads()*$orderItem->getQtyOrdered();
118: $linkPurchasedItem->setLinkHash($linkHash)
119: ->setNumberOfDownloadsBought($numberOfDownloads)
120: ->setStatus(Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PENDING)
121: ->setCreatedAt($orderItem->getCreatedAt())
122: ->setUpdatedAt($orderItem->getUpdatedAt())
123: ->save();
124: }
125: }
126: }
127: }
128:
129: return $this;
130: }
131:
132: 133: 134: 135: 136: 137:
138: public function setHasDownloadableProducts($observer)
139: {
140: $session = Mage::getSingleton('checkout/session');
141: if (!$session->getHasDownloadableProducts()) {
142: $order = $observer->getEvent()->getOrder();
143: foreach ($order->getAllItems() as $item) {
144:
145: if ($item->getProductType() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE
146: || $item->getRealProductType() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE
147: || $item->getProductOptionByCode('is_downloadable'))
148: {
149: $session->setHasDownloadableProducts(true);
150: break;
151: }
152: }
153: }
154: return $this;
155: }
156:
157: 158: 159: 160: 161: 162:
163: public function setLinkStatus($observer)
164: {
165: $order = $observer->getEvent()->getOrder();
166:
167: if (!$order->getId()) {
168:
169: return $this;
170: }
171:
172:
173: $status = '';
174: $linkStatuses = array(
175: 'pending' => Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PENDING,
176: 'expired' => Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_EXPIRED,
177: 'avail' => Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_AVAILABLE,
178: 'payment_pending' => Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PENDING_PAYMENT,
179: 'payment_review' => Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PAYMENT_REVIEW
180: );
181:
182: $downloadableItemsStatuses = array();
183: $orderItemStatusToEnable = Mage::getStoreConfig(
184: Mage_Downloadable_Model_Link_Purchased_Item::XML_PATH_ORDER_ITEM_STATUS, $order->getStoreId()
185: );
186:
187: if ($order->getState() == Mage_Sales_Model_Order::STATE_HOLDED) {
188: $status = $linkStatuses['pending'];
189: } elseif ($order->isCanceled()
190: || $order->getState() == Mage_Sales_Model_Order::STATE_CLOSED
191: || $order->getState() == Mage_Sales_Model_Order::STATE_COMPLETE
192: ) {
193: $expiredStatuses = array(
194: Mage_Sales_Model_Order_Item::STATUS_CANCELED,
195: Mage_Sales_Model_Order_Item::STATUS_REFUNDED,
196: );
197: foreach ($order->getAllItems() as $item) {
198: if ($item->getProductType() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE
199: || $item->getRealProductType() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE
200: ) {
201: if (in_array($item->getStatusId(), $expiredStatuses)) {
202: $downloadableItemsStatuses[$item->getId()] = $linkStatuses['expired'];
203: } else {
204: $downloadableItemsStatuses[$item->getId()] = $linkStatuses['avail'];
205: }
206: }
207: }
208: } elseif ($order->getState() == Mage_Sales_Model_Order::STATE_PENDING_PAYMENT) {
209: $status = $linkStatuses['payment_pending'];
210: } elseif ($order->getState() == Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW) {
211: $status = $linkStatuses['payment_review'];
212: } else {
213: $availableStatuses = array($orderItemStatusToEnable, Mage_Sales_Model_Order_Item::STATUS_INVOICED);
214: foreach ($order->getAllItems() as $item) {
215: if ($item->getProductType() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE
216: || $item->getRealProductType() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE
217: ) {
218: if ($item->getStatusId() == Mage_Sales_Model_Order_Item::STATUS_BACKORDERED &&
219: $orderItemStatusToEnable == Mage_Sales_Model_Order_Item::STATUS_PENDING &&
220: !in_array(Mage_Sales_Model_Order_Item::STATUS_BACKORDERED, $availableStatuses, true) ) {
221: $availableStatuses[] = Mage_Sales_Model_Order_Item::STATUS_BACKORDERED;
222: }
223:
224: if (in_array($item->getStatusId(), $availableStatuses)) {
225: $downloadableItemsStatuses[$item->getId()] = $linkStatuses['avail'];
226: }
227: }
228: }
229: }
230: if (!$downloadableItemsStatuses && $status) {
231: foreach ($order->getAllItems() as $item) {
232: if ($item->getProductType() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE
233: || $item->getRealProductType() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE
234: ) {
235: $downloadableItemsStatuses[$item->getId()] = $status;
236: }
237: }
238: }
239:
240: if ($downloadableItemsStatuses) {
241: $linkPurchased = Mage::getResourceModel('downloadable/link_purchased_item_collection')
242: ->addFieldToFilter('order_item_id', array('in' => array_keys($downloadableItemsStatuses)));
243: foreach ($linkPurchased as $link) {
244: if ($link->getStatus() != $linkStatuses['expired']
245: && !empty($downloadableItemsStatuses[$link->getOrderItemId()])
246: ) {
247: $link->setStatus($downloadableItemsStatuses[$link->getOrderItemId()])
248: ->save();
249: }
250: }
251: }
252: return $this;
253: }
254:
255: 256: 257: 258: 259: 260:
261: public function isAllowedGuestCheckout(Varien_Event_Observer $observer)
262: {
263: $quote = $observer->getEvent()->getQuote();
264:
265: $store = $observer->getEvent()->getStore();
266: $result = $observer->getEvent()->getResult();
267:
268: $isContain = false;
269:
270: foreach ($quote->getAllItems() as $item) {
271: if (($product = $item->getProduct()) &&
272: $product->getTypeId() == Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) {
273: $isContain = true;
274: }
275: }
276:
277: if ($isContain && Mage::getStoreConfigFlag(self::XML_PATH_DISABLE_GUEST_CHECKOUT, $store)) {
278: $result->setIsAllowed(false);
279: }
280:
281: return $this;
282: }
283:
284: 285: 286: 287: 288: 289:
290: public function initOptionRenderer(Varien_Event_Observer $observer)
291: {
292: $block = $observer->getBlock();
293: $block->addOptionsRenderCfg('downloadable', 'downloadable/catalog_product_configuration');
294: return $this;
295: }
296: }
297: