0

I am using Magento 1.9.2.1.

Right now i am developing extension to add an attribute to the checkout page.

I am doing everything by this guide: https://stackoverflow.com/questions/12936470/adding-a-new-attribute-to-order-in-magento You can see the guide as an answer to the question.

So let me show all the files i've created:

1: /app/code/community/VivasIndustries/Vshippingone/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <VivasIndustries_Vshippingone>
            <version>1.1</version>
        </VivasIndustries_Vshippingone>
    </modules>

    <global> 
         <fieldsets>
            <sales_convert_quote>                           
                <my_custom_input_field_one><to_order>*</to_order></my_custom_input_field_one>
            </sales_convert_quote>

            <sales_convert_order>                                              
                <my_custom_input_field_one><to_quote>*</to_quote></my_custom_input_field_one>
            </sales_convert_order>
        </fieldsets>
        <helpers>
            <vshippingone>
                <class>VivasIndustries_Vshippingone_Helper</class>
            </vshippingone>            
        </helpers>

        <models>
            <vshippingone>
                <class>VivasIndustries_Vshippingone_Model</class>
                <resourceModel>vshippingone_mysql4</resourceModel>
            </vshippingone>
        </models>
        <resources>
            <vshippingone_setup>
                <setup>
                    <module>VivasIndustries_Vshippingone</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </vshippingone_setup>
            <vshippingone_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </vshippingone_write>
            <vshippingone_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </vshippingone_read>
        </resources>

        <events>
            <adminhtml_sales_order_create_process_data_before>
                <observers>
                    <vshippingone>
                        <type>singleton</type>
                        <class>vshippingone/observer</class>
                        <method>saveCustomDataOne</method>
                    </vshippingone>
                </observers>
            </adminhtml_sales_order_create_process_data_before>
        </events>

        <blocks>
            <vshippingone>
                <class>VivasIndustries_Vshippingone_Block</class>
            </vshippingone>
        </blocks>
    </global>
</config>

2: /app/code/community/VivasIndustries/Vshippingone/Model/Observer.php:

<?PHP
class VivasIndustries_Vshippingone_Model_Observer 
{
    public function saveCustomDataOne($event)
    {
        $quote = $event->getSession()->getQuote();
        $quote->setData('my_custom_input_field_one', $event->getRequestModel()->getPost('my_custom_input_field_one'));

        return $this;
    }
}

3: /app/code/community/VivasIndustries/Vshippingone/sql/vshippingone_setup/mysql4-install-1.1.php

<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("order", "my_custom_input_field_one", array("type"=>"varchar"));
$installer->addAttribute("quote", "my_custom_input_field_one", array("type"=>"varchar"));
$installer->endSetup();

4: /app/etc/modules/VivasIndustries_Vshippingone.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <VivasIndustries_Vshippingone>
      <active>true</active>
      <codePool>community</codePool>
      <version>1.1</version>
    </VivasIndustries_Vshippingone>
  </modules>
</config>

I have not made any other files aka Helpers or anything else. No other changes i have made to this clean installed Magento. I have not installed any other extensions either.

By my understanding i've added the following:

In: /app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml - I've added the following line so i can see the input field in the checkout page, Search for <input type="text" name="my_custom_input_field_one" id="my_custom_input_field_one"></input>:

<?php /** @var $this Mage_Checkout_Block_Onepage_Shipping_Method_Available */ ?>
<?php $_shippingRateGroups = $this->getShippingRates(); ?>
<?php if (!$_shippingRateGroups): ?>
    <p><?php echo $this->__('Sorry, no quotes are available for this order at this time.') ?></p>
<?php else: ?>
    <dl class="sp-methods">
    <?php $shippingCodePrice = array(); ?>
    <?php $_sole = count($_shippingRateGroups) == 1; foreach ($_shippingRateGroups as $code => $_rates): ?>
        <dt><?php echo $this->escapeHtml($this->getCarrierName($code)) ?></dt>
        <dd>
            <ul>
            <?php $_sole = $_sole && count($_rates) == 1; foreach ($_rates as $_rate): ?>
                <?php $shippingCodePrice[] = "'".$_rate->getCode()."':".(float)$_rate->getPrice(); ?>
                <li>
                   <?php if ($_rate->getErrorMessage()): ?>
                    <ul class="messages"><li class="error-msg"><ul><li><?php echo $this->escapeHtml($_rate->getErrorMessage()) ?></li></ul></li></ul>
                   <?php else: ?>
                        <?php if ($_sole) : ?>
                        <span class="no-display"><input name="shipping_method" type="radio" value="<?php echo $_rate->getCode() ?>" id="s_method_<?php echo $_rate->getCode() ?>" checked="checked" /></span>
                        <?php else: ?>
                        <input name="shipping_method" type="radio" value="<?php echo $_rate->getCode() ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> class="radio"/>

                        <?php if ($_rate->getCode() === $this->getAddressShippingMethod()): ?>
                        <script type="text/javascript">
                            //<![CDATA[
                                lastPrice = <?php echo (float)$_rate->getPrice(); ?>;
                            //]]>
                        </script>
                        <?php endif; ?>

                        <?php endif; ?>
                        <label for="s_method_<?php echo $_rate->getCode() ?>"><?php echo $this->escapeHtml($_rate->getMethodTitle()) ?>
                        <?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
                        <?php $_incl = $this->getShippingPrice($_rate->getPrice(), true); ?>
                        <?php echo $_excl; ?>
                        <?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
                            (<?php echo $this->__('Incl. Tax'); ?> <?php echo $_incl; ?>)
                        <?php endif; ?>
                        </label>
                   <?php endif ?>
                </li>
            <?php endforeach; ?>
            </ul>
        </dd>
    <?php endforeach; ?>
    <input type="text" name="my_custom_input_field_one" id="my_custom_input_field_one"></input>
    </dl>
<script type="text/javascript">
//<![CDATA[
    <?php if (!empty($shippingCodePrice)): ?>
        var shippingCodePrice = {<?php echo implode(',',$shippingCodePrice); ?>};
    <?php endif; ?>

    $$('input[type="radio"][name="shipping_method"]').each(function(el){
        Event.observe(el, 'click', function(){
            if (el.checked == true) {
                var getShippingCode = el.getValue();
                <?php if (!empty($shippingCodePrice)): ?>
                    var newPrice = shippingCodePrice[getShippingCode];
                    if (!lastPrice) {
                        lastPrice = newPrice;
                        quoteBaseGrandTotal += newPrice;
                    }
                    if (newPrice != lastPrice) {
                        quoteBaseGrandTotal += (newPrice-lastPrice);
                        lastPrice = newPrice;
                    }
                <?php endif; ?>
                checkQuoteBaseGrandTotal = quoteBaseGrandTotal;
                return false;
            }
       });
    });
//]]>
</script>
<?php endif; ?>

Then in: /app/design/adminhtml/default/default/template/sales/order/view/tab/info.phtml:

I've added the following:

<?php if($_order->getMyCustomInputFieldOne()): ?>
<tr>
    <td class="label"><label><?php echo Mage::helper('sales')->__('My Custom Input Field One') ?></label></td>
    <td class="value"><strong><?php echo $_order->getMyCustomInputFieldOne() ?></strong></td>
</tr>
<?php endif; ?>         

There is a very strange thing:

When i open sales_flat_order with PHPMyAdmin and i edit for example the last or any other order i can not modify the column my_custom_input_field_one. Whatever i post to the column and hit Go to make the edit everything goes fine without any errors displayed, but however the edit is not done.

Whatever i post to my_custom_input_field_one and after submitting the edit the column still holds NULL as a result. Somehow i can not edit that.

Checkout the structure: enter image description here

When i remove the Null setting from the structure of my_custom_input_field_one i receive the following alert when trying to finish an order:

enter image description here

Here you can see the error i receive in exception.log: http://pastebin.com/93XWbLxy

I don't know where is my mistake and why i can not save the data inserted in the custom input.

Can you help me out resolve this problem and make the information inserted in the custom input to be saved in the order details ?

Thanks in advance!

  • Please check the Magento logs for errors. Also make sure they're turned on – Sander Mangel Sep 15 '15 at 17:31
  • From where i can turn them on ? – Venelin Vasilev Sep 15 '15 at 17:32
  • The post that you link to is base off "how to add a field to the order admin" and not frontend. Therefore you are using the wrong observer (adminhtml_sales_order_create_process_data_before) – Renon Stewart Sep 15 '15 at 17:35
  • Here you can see the error i receive in exception.log: pastebin.com/93XWbLxy – Venelin Vasilev Sep 15 '15 at 17:35
  • @R.S Can you post an answer with the correction which i have to make so i can fix it ? I am stuck on this like a whole day. Thanks! – Venelin Vasilev Sep 15 '15 at 17:37
1

You are using the wrong observer, which will never been trigger. You need to use a frontend observer

<events>
    <adminhtml_sales_order_create_process_data_before>
         <observers>
             <vshippingone>
                  <type>singleton</type>
                  <class>vshippingone/observer</class>
                  <method>saveCustomDataOne</method>
             </vshippingone>
             ....

Try

In config.xml

<events>
    <checkout_controller_onepage_save_shipping_method>
         <observers>
             <vshippingone>
                  <type>singleton</type>
                  <class>vshippingone/observer</class>
                  <method>saveCustomDataOne</method>
             </vshippingone>
         </observers>
    </checkout_controller_onepage_save_shipping_method>
    ...

Update your observer to

<?PHP
class VivasIndustries_Vshippingone_Model_Observer 
{
    public function saveCustomDataOne($event)
    {
        $quote = $event->getQuote();
        $quote->setData('my_custom_input_field_one', $event->getRequest()->getPost('my_custom_input_field_one'));

        return $this;
    }
}
  • With this edit when i try to add an item to the cart i receive: i.imgur.com/LmNKPBi.png Somehow now i can not add any item to the cart. Here is my config.xml after the suggested by you edit: pastebin.com/t9ceNnAa – Venelin Vasilev Sep 15 '15 at 17:50
  • It's closed. Checkout the pastebin link for the current config.xml :) – Venelin Vasilev Sep 15 '15 at 17:56
  • Take a look at my update and undo the changes to the observer – Renon Stewart Sep 15 '15 at 17:56
  • Same problem is still here. I can not add any item to the cart :) – Venelin Vasilev Sep 15 '15 at 18:00
  • That issue can not be cause by that change unless you have xml format issue – Renon Stewart Sep 15 '15 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.