Guest User

Untitled

a guest
Jun 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. // function for checking trail products
  2. public function checkTrialProduct($observer)
  3. {
  4. error_log('coming in trail function');
  5. $getValues = array();
  6. $getValues[0] = '';
  7. $getValues[1] = '';
  8. $getValues[2] = '';
  9. $customerId = Mage::getSingleton('customer/session')->getCustomerId();
  10. $salesCollection = Mage::getModel('sales/order')
  11. ->getCollection()
  12. ->addAttributeToSelect('entity_id')
  13. ->addAttributeToSelect('customer_id')
  14. ->addFieldToFilter('customer_id', $customerId)
  15. ->addOrder('created_at', 'desc')
  16. ->addAttributeToSelect('*')
  17. ->load();
  18. // if trial product is exist in previous orders
  19. if(count($salesCollection))
  20. {
  21.  
  22. $order = $observer->getEvent()->getOrder();
  23. foreach ($order->getAllItems() as $item)
  24. {
  25.  
  26. $prdEntyId = $item->getData("product_id");
  27. $product = Mage::getModel('catalog/product')->load($prdEntyId);
  28. $prdAtrSetId = $product->getData("attribute_set_id");
  29. $trailOption = $product->getData("trialsubsription");
  30. $subLength = $product->getData("subscriptionlength");
  31. foreach($salesCollection as $_myorder)
  32. {
  33. $getValues[1]['orderid'] = $_myorder->getId();
  34. $getValues[2]['expiredate'] = $_myorder->getData('expirationdate');
  35. $expdate = $getValues[2]['expiredate'];
  36. foreach ($_myorder->getAllItems() as $itemId => $oldOrderItem)
  37. {
  38. $getValues[0]['productid']=$oldOrderItem->getProductId();
  39. if($prdEntyId == $getValues[0]['productid'] && $trailOption == '1')
  40. {
  41. throw new Exception("You can add the trial product only once");
  42.  
  43. }
  44.  
  45. }
  46. }
  47. }
  48.  
  49. }
  50. else
  51. {
  52. // insert as a new records...
  53. $order = $observer->getEvent()->getOrder();
  54. $k=0;
  55. foreach ($order->getAllItems() as $item)
  56. {
  57.  
  58. $prdEntyId = $item->getData("product_id");
  59. $product = Mage::getModel('catalog/product')->load($prdEntyId);
  60. $prdAtrSetId = $product->getData("attribute_set_id");
  61. $trailOption = $product->getData("trialsubsription");
  62. $subLength = $product->getData("subscriptionlength");
  63. if($prdAtrSetId == '32' && $trailOption == '1')
  64. {
  65. $expiryDate = date('Y-m-d H:i:s', mktime(0, 0, 0, date("m") , date("d") + $subLength , date("Y")));
  66. $observer->getEvent()->getOrder()->setExpirationdate($expiryDate);
  67. $observer->getEvent()->getOrder()->setTrialsubsription($trailOption);
  68. $order->save();
  69. }
  70. }
  71. }
  72.  
  73.  
  74. }
Add Comment
Please, Sign In to add comment