Search code examples
magentodateusing

Start using time for product (License type)


In checkout cart mornitor, i added a datepicker for earch product in cart to allow cusommer selecting start using date (within 1 month from now). I want to save this datepicker to onepage when click place-order. It will save to order. I already create attribute in eav_attribute. In config.xml i use this code:

<events>

            <controller_action_predispatch_checkout_cart_index >
                <observers>
                    <licensetime_observer>
                        <class>licensetime/observer</class>
                        <method>saveLicensetime</method>
                    </licensetime_observer>
                </observers>
            </controller_action_predispatch_checkout_cart_index>

        </events>

And observer i was try to var_dump but start_date is null

public function saveLicensetime($observer)
    {

        $event = $observer->getEvent();
        $product = $event->getProduct();
        $quote = Mage::getSingleton('checkout/type_onepage')->getQuote();
        $licenseStartDate = $quote->getLicense_start_date();
        if (!$licenseStartDate) {
            $licenseStartDate = date ("Y-m-d H:i:s", floor(time()/86400)*86400);
        }
        //var_dump($quote); die("aaaaaaaaaaa");

    }

In cart/item/defaul.phtml datepicker code:

<label for="license_start_date"><?php echo $this->__('Start Date') ?> :</label>
    <input name="cart[<?php echo $_item->getId() ?>][license_start_date]" readonly="true" id="license_start_date<?php echo $_item->getProductId(); ?>" value="<?php echo $this->getLicenseStartTime($_item->getId()) ?>" class="date-picker" />
    <label for="license_end_date"><?php echo $this->__('End Date') ?> :</label>
    <input readonly="true" name="cart[<?php echo $_item->getId() ?>][license_end_date]" id="license_end_date<?php echo $_item->getProductId(); ?>" value="<?php echo $this->getLicenseEndTime($_item->getId()) ?>"></input>

I'm trying this article but no luck!

Magento change Custom Option value before adding it to cart

Sorry, my E not well!


Solution

  • After few days ago, i'm hard-working and know that: - Override app/code/core/Mage/Checkout/Model/Cart.php and edit function like that:

        public function updateItems($data)
     {
         Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));
    
         foreach ($data as $itemId => $itemInfo) {
    
             $item = $this->getQuote()->getItemById($itemId);
             if (!$item) {
                 continue;
             }
    
             if (!empty($itemInfo['remove']) || (isset($itemInfo['qty']) && $itemInfo['qty']=='0')) {
                 $this->removeItem($itemId);
                 continue;
             }
    
             $qty = isset($itemInfo['qty']) ? (float) $itemInfo['qty'] : false;
             if ($qty > 0) {
                 $item->setQty($qty);
             }
    
         /* Start: Custom code added for license start date */
         if(!empty($itemInfo['license_start_date'])) {
    
            $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    
            # make the frame_queue active
            $query = "UPDATE `sales_flat_quote_item` SET license_start_date = '".$itemInfo['license_start_date']."' where item_id = $itemId";
    $write->query($query);
    
            $item->setLicense_start_date($itemInfo['license_start_date']);
         }
         /* End: Custom code added for licensee start date */
    
         }
    
         Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
         return $this;
     }
    

    and copy app/code/core/Mage/Adminhtml/Block/Sales/Order/Items/Abstract.php to local (app/code/local/Mage/Adminhtml/Block/Sales/Order/Items/Abstract.php) and add this function:

    public function getLicense_start_date($item) {
            $itemId = $item->getId();
    
            $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    
            $query = "SELECT q.* FROM `sales_flat_order_item` o
            LEFT JOIN `sales_flat_quote_item` q on o.quote_item_id = q.item_id
            WHERE o.item_id = $itemId";
    
            # For older versions of Magento
    /*      $query = "SELECT q.* FROM `sales_order_entity_int` o
            LEFT JOIN `sales_flat_quote_item` q on o.value = q.entity_id
            WHERE o.entity_id = $itemId AND o.attribute_id = 343";       */     
    
            $res = $write->query($query);
    
            while ($row = $res->fetch() ) {
                if(key_exists('itemcomment',$row)) {
                    echo nl2br($row['itemcomment']);
                }
            }
        }    
    

    To add the license time column to the items edit the .phtml file below: app/design/adminhtml/default/default/template/sales/order/view/items.phtml (you can add you theme in adminhtml to edit)

    Adding header for items to make it look like below:

    <tr class="headings">
    <th><?php echo $this->helper('sales')->__('Product') ?></th>
    <th><?php echo $this->helper('sales')->__('Licens Time') ?></th>
    <th><?php echo $this->helper('sales')->__('Item Status') ?></th>
    

    And adding Column with comments. app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml Add a column for item comments juts before status columns to make it look a like below.

    <td><?php echo $this->getLicense_start_date($_item) ?></td> <!-- New column added for item comments -->
    <td class="a-center"><?php echo $_item->getStatus() ?></td>
    

    Note that: In your theme, for the file: template/checkout/cart.phtml Add the new heading along with other heading for cart items and in the file: template/checkout/cart/item/default.phtml use datepicker to selected date with code like below:

    <td class="a-center">
    <input type="text" name="cart[<?php echo $_item->getId() ?>][license_start_date]" rows="3" cols="20"><?php echo $_item->getLicense_start_date() ?></input>
    </td>