Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // function for checking trail products
- public function checkTrialProduct($observer)
- {
- error_log('coming in trail function');
- $getValues = array();
- $getValues[0] = '';
- $getValues[1] = '';
- $getValues[2] = '';
- $customerId = Mage::getSingleton('customer/session')->getCustomerId();
- $salesCollection = Mage::getModel('sales/order')
- ->getCollection()
- ->addAttributeToSelect('entity_id')
- ->addAttributeToSelect('customer_id')
- ->addFieldToFilter('customer_id', $customerId)
- ->addOrder('created_at', 'desc')
- ->addAttributeToSelect('*')
- ->load();
- // if trial product is exist in previous orders
- if(count($salesCollection))
- {
- $order = $observer->getEvent()->getOrder();
- foreach ($order->getAllItems() as $item)
- {
- $prdEntyId = $item->getData("product_id");
- $product = Mage::getModel('catalog/product')->load($prdEntyId);
- $prdAtrSetId = $product->getData("attribute_set_id");
- $trailOption = $product->getData("trialsubsription");
- $subLength = $product->getData("subscriptionlength");
- foreach($salesCollection as $_myorder)
- {
- $getValues[1]['orderid'] = $_myorder->getId();
- $getValues[2]['expiredate'] = $_myorder->getData('expirationdate');
- $expdate = $getValues[2]['expiredate'];
- foreach ($_myorder->getAllItems() as $itemId => $oldOrderItem)
- {
- $getValues[0]['productid']=$oldOrderItem->getProductId();
- if($prdEntyId == $getValues[0]['productid'] && $trailOption == '1')
- {
- throw new Exception("You can add the trial product only once");
- }
- }
- }
- }
- }
- else
- {
- // insert as a new records...
- $order = $observer->getEvent()->getOrder();
- $k=0;
- foreach ($order->getAllItems() as $item)
- {
- $prdEntyId = $item->getData("product_id");
- $product = Mage::getModel('catalog/product')->load($prdEntyId);
- $prdAtrSetId = $product->getData("attribute_set_id");
- $trailOption = $product->getData("trialsubsription");
- $subLength = $product->getData("subscriptionlength");
- if($prdAtrSetId == '32' && $trailOption == '1')
- {
- $expiryDate = date('Y-m-d H:i:s', mktime(0, 0, 0, date("m") , date("d") + $subLength , date("Y")));
- $observer->getEvent()->getOrder()->setExpirationdate($expiryDate);
- $observer->getEvent()->getOrder()->setTrialsubsription($trailOption);
- $order->save();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment