简体   繁体   中英

How to add extra field “Dropdown List” in Magento One Page Checkout

I have create a following custom attribute using modules.

This is Dropdown-list attribute for Customer.

It should be displayed on checkout page (Customer As Guest/Existing user) Customer Account Page, Customer Account Create/ Register Page, Admin Customer Edit Page.

Currently I am able see attribute on Admin Section in Customer Address.

I want this attribute to show on Checkout Page also in customer address section.

I have check billing.phtml file, As there other attribute are statically written, But I am not sure How to write custom attribute in billing.phtml as its dropdown-list. and which other file are necessary to update to view this attribute. Please let me know where I am doing wrong or anything else to add in my code.

Any guidance would be appreciated.

Below is my code -

File Name - /app/code/local/PS/PB/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <PS_PB>
      <version>0.1.0</version>
    </PS_PB>
  </modules>
  <global>
    <helpers>
      <pb>
        <class>PS_PB_Helper</class>
      </pb>
    </helpers>
    <models>
      <pb>
        <class>PS_PB_Model</class>
        <resourceModel>pb_mysql4</resourceModel>
      </pb>
    </models>
    <resources>
      <customerattribute1462360584_setup>
        <setup>
          <module>PS_P</module>
          <class>Mage_Customer_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </customerattribute1462360584_setup>
      <customerattribute1462360584_write>
        <connection>
          <use>core_write</use>
        </connection>
      </customerattribute1462360584_write>
      <customerattribute1462360584_read>
        <connection>
          <use>core_read</use>
        </connection>
      </customerattribute1462360584_read>
    </resources>
  </global>
</config> 

File - /app/code/local/PS/PB/Helper/Data.php

<?php
class PS_PB_Helper_Data extends Mage_Core_Helper_Abstract
{
}

File - /app/code/local/PS/PB/Model/Eav/Entity/Attribute/Source/Customeroptions14623605840.php

<?php
class PS_PB_Model_Eav_Entity_Attribute_Source_Customeroptions14623605840 extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    /**
     * Retrieve all options array
     *
     * @return array
     */
    public function getAllOptions()
    {
        if (is_null($this->_options)) {
            $this->_options = array(

                array(
                    "label" => Mage::helper("eav")->__("End Consumer / DIY"),
                    "value" =>  1
                ),

                array(
                    "label" => Mage::helper("eav")->__("Service Technician"),
                    "value" =>  2
                ),

                array(
                    "label" => Mage::helper("eav")->__("Other Professional"),
                    "value" =>  3
                ),

            );
        }
        return $this->_options;
    }

    /**
     * Retrieve option array
     *
     * @return array
     */
    public function getOptionArray()
    {
        $_options = array();
        foreach ($this->getAllOptions() as $option) {
            $_options[$option["value"]] = $option["label"];
        }
        return $_options;
    }

    /**
     * Get a text for option value
     *
     * @param string|integer $value
     * @return string
     */
    public function getOptionText($value)
    {
        $options = $this->getAllOptions();
        foreach ($options as $option) {
            if ($option["value"] == $value) {
                return $option["label"];
            }
        }
        return false;
    }

    /**
     * Retrieve Column(s) for Flat
     *
     * @return array
     */
    public function getFlatColums()
    {
        $columns = array();
        $columns[$this->getAttribute()->getAttributeCode()] = array(
            "type"      => "tinyint(1)",
            "unsigned"  => false,
            "is_null"   => true,
            "default"   => null,
            "extra"     => null
        );

        return $columns;
    }

    /**
     * Retrieve Indexes(s) for Flat
     *
     * @return array
     */
    public function getFlatIndexes()
    {
        $indexes = array();

        $index = "IDX_" . strtoupper($this->getAttribute()->getAttributeCode());
        $indexes[$index] = array(
            "type"      => "index",
            "fields"    => array($this->getAttribute()->getAttributeCode())
        );

        return $indexes;
    }

    /**
     * Retrieve Select For Flat Attribute update
     *
     * @param int $store
     * @return Varien_Db_Select|null
     */
    public function getFlatUpdateSelect($store)
    {
        return Mage::getResourceModel("eav/entity_attribute")
            ->getFlatUpdateSelect($this->getAttribute(), $store);
    }
}

File - /app/code/local/PS/PB/sql/customerattribute1462360584_setup

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


    $installer->addAttribute("customer_address", "cstpb",  array(
        "type"     => "int",
        "backend"  => "",
        "label"    => "Primary Business",
        "input"    => "select",
        "source"   => "pb/eav_entity_attribute_source_customeroptions14623605840",
        "visible"  => true,
        "required" => true,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""

        ));

            $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer_address", "cstpb");


    $used_in_forms=array();
    $used_in_forms[]="adminhtml_customer";
    $used_in_forms[]="checkout_register";
    $used_in_forms[]="customer_account_create";
    $used_in_forms[]="customer_account_edit";
    $used_in_forms[]="adminhtml_checkout";
    $used_in_forms[]="adminhtml_customer_address";
    $used_in_forms[]="customer_register_address";
    $used_in_forms[]="customer_address_edit";
            $attribute->setData("used_in_forms", $used_in_forms)
            ->setData("is_used_for_customer_segment", true)
            ->setData("is_system", 0)
            ->setData("is_user_defined", 1)
            ->setData("is_visible", 1)
            ->setData("sort_order", 100)
            ;
            $attribute->save();



    $installer->endSetup();

XML File - /app/etc/modules/PS_PB.xml

<?xml version="1.0"?>
<config>
  <modules>
    <PS_PB>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </PS_PB>
  </modules>
</config>

Updates

After making the correction in config.xml file its working fine.

To check errors in config.xml open this file in browser after editing in > any editor so it will show error.

Now The attribute is showing checkout page and admin customer page and customer my account section but in this section. I am not able to update it. Which fields needs to be added in config.xml fot customer_account tags.

I have added like this in config.xml

<customer_account>
   <primary_business>
       <create>1</create>
         <update>1</update>
   </primary_business>
</customer_account>

Your install script code appears right to me although I did not run it to test.

First, make sure your install script is executing.

Second,

You can create the "customer attribute" through the admin by navigating to Customers > Attributes > Manage Attributes.

http://awesomescreenshot.com/0b35uq1542

Magento should be displaying your attribute in the configured areas if it is behaving the way it should. I would recommend starting by manually creating the attribute and seeing if it showing up. If it is not, it is possible that your theme or other extensions have overridden the code responsible for rendering those attributes.

Once you know the theme is rendering what you want, where you want, you can create a sandbox script to load your attribute and get the data. Looking at the "Admin" created attribute data normally shows me what I missed.

Something like this:

<?php

require_once('app/Mage.php');
Mage::app()->setCurrentStore('0');
$attribute_id = 9999; // YOU ADMIN CREATED ATTRIBUTE ID
$attributeModel = Mage::getModel('eav/attribute')->load($attribute_id);
var_dump($attributeModel->getData());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM