Jump to:

2088 Posts in 1008 Topics by 455 members

E-Commerce Modules

SilverStripe Forums » E-Commerce Modules » Ecommerce 0.8.1 - add new member field

Discuss about the various e-commerce modules available:
Ecommerce, SS Shop, SilverCart and SwipeStripe
Alternatively, have a look the shared mailinglist.

Moderators: martimiz, Nicolaas, Sean, frankmullenger, biapar, Willr, Ingo, Jedateach, swaiba, simon_w

Page: 1
Go to End
Author Topic: 775 Views
  • Kiwipearls
    Avatar
    Community Member
    34 Posts

    Ecommerce 0.8.1 - add new member field Link to this post

    How do I go about adding a new field to the member account page.

    I just need to add Company - so we can match orders/customers to current/existing customers - as we won't accept orders from anyone else.

    I found instructions for older version, but nothing for the current version.

    I eagerly look forward to hearing from somebody on this.

  • Kiwipearls
    Avatar
    Community Member
    34 Posts

    Re: Ecommerce 0.8.1 - add new member field Link to this post

    I was really hoping to get support on this question seeing this module is supported.

    I still do not know how to add the Company field to the account form.

  • Kiwipearls
    Avatar
    Community Member
    34 Posts

    Re: Ecommerce 0.8.1 - add new member field Link to this post

    It seems, I have this in mysite/_config.php

    Object::add_extension('Member', 'EcommerceRole');

    But it is not adding the company field to the account page.

    I cannot figure out why?

    I would really love it if someone could please give me some support on this? Or is Silverstripe and the e-commerce module a dead project?

  • Kiwipearls
    Avatar
    Community Member
    34 Posts

    Re: Ecommerce 0.8.1 - add new member field Link to this post

    I solved it.

    I had to add the company filed to ecommercerole.php

    I have shared this in case anyone else needs to know what to do.

    file didn't upload...so here is the code

    <?php
    /**
    * EcommerceRole provides customisations to the {@link Member}
    * class specifically for this ecommerce module.
    *
    * @package ecommerce
    */
    class EcommerceRole extends DataObjectDecorator {

       protected static $group_name = "Shop Customers";
          static function set_group_name($v) {self::$group_name = $v;}
          static function get_group_name(){return self::$group_name;}

       function extraStatics() {
          return array(
             'db' => array(
              'Company' => 'Varchar(100)',
                'Address' => 'Varchar(255)',
                'AddressLine2' => 'Varchar(255)',
                'City' => 'Varchar(100)',
                'PostalCode' => 'Varchar(30)',
                'State' => 'Varchar(100)',
                'Country' => 'Varchar',
                'HomePhone' => 'Varchar(100)',
                'MobilePhone' => 'Varchar(100)',
                'Notes' => 'HTMLText'
                
             )
          );
       }

       static function findCountryTitle($code) {
          user_error("depreciated, please use EcommerceRole::find_country_title", E_USER_NOTICE);
          return self::find_country_title($code);
       }

       static function find_country_title($code) {
          $countries = Geoip::getCountryDropDown();
          // check if code was provided, and is found in the country array
          if($code && $countries[$code]) {
             return $countries[$code];
          } else {
             return false;
          }
       }

       /**
        * Find the member's country.
        *
        * If there is no member logged in, try to resolve
        * their IP address to a country.
        *
        * @return string Found country of member
        */
       static function findCountry() {
          user_error("depreciated, please use EcommerceRole::find_country", E_USER_NOTICE);
          return self::find_country();
       }
       static function find_country() {
          $member = Member::currentUser();

          if($member && $member->Country) {
             $country = $member->Country;
          } else {
             if($country = ShoppingCart::get_country())
                return $country;
             
             // HACK Avoid CLI tests from breaking (GeoIP gets in the way of unbiased tests!)
             // @todo Introduce a better way of disabling GeoIP as needed (Geoip::disable() ?)
             if(Director::is_cli()) {
                $country = null;
             } else {
                $country = Geoip::visitor_country();
             }
          }

          return $country;
       }

       static function add_members_to_customer_group() {
          $gp = DataObject::get_one("Group", "\"Title\" = '".self::get_group_name()."'");
          if(!$gp) {
             $gp = new Group();
             $gp->Title = self::get_group_name();
             $gp->Sort = 999998;
             $gp->write();
          }
          $allCombos = DB::query("
             SELECT \"Group_Members\".\"ID\", \"Group_Members\".\"MemberID\", \"Group_Members\".\"GroupID\"
             FROM \"Group_Members\"
             WHERE \"Group_Members\".\"GroupID\" = ".$gp->ID.";"
          );
          //make an array of all combos
          $alreadyAdded = array();
          $alreadyAdded[-1] = -1;
          if($allCombos) {
             foreach($allCombos as $combo) {
                $alreadyAdded[$combo["MemberID"]] = $combo["MemberID"];
             }
          }
          $unlistedMembers = DataObject::get(
             "Member",
             $where = "\"Member\".\"ID\" NOT IN (".implode(",",$alreadyAdded).")",
             $sort = null,
             $join = "INNER JOIN \"Order\" ON \"Order\".\"MemberID\" = \"Member\".\"ID\""
          );

          //add combos
          if($unlistedMembers) {
             $existingMembers = $gp->Members();
             foreach($unlistedMembers as $member) {
                $existingMembers->add($member);
             }
          }
       }

       /**
        * Create a new member with given data for a new member,
        * or merge the data into the logged in member.
        *
        * IMPORTANT: Before creating a new Member record, we first
        * check that the request email address doesn't already exist.
        *
        * @param array $data Form request data to update the member with
        * @return boolean|object Member object or boolean FALSE
        */
       public static function createOrMerge($data) {
          user_error("depreciated, please use EcommerceRole::ecommerce_create_or_merge", E_USER_NOTICE);
          return self::ecommerce_create_or_merge($data);

       }
       public static function ecommerce_create_or_merge($data) {
          // Because we are using a ConfirmedPasswordField, the password will
          // be an array of two fields
          if(isset($data['Password']) && is_array($data['Password'])) {
             $data['Password'] = $data['Password']['_Password'];
          }
          // We need to ensure that the unique field is never overwritten
          $uniqueField = Member::get_unique_identifier_field();
          if(isset($data[$uniqueField])) {
             $SQL_unique = Convert::raw2xml($data[$uniqueField]);
             // TODO review - should $uniqueField be quoted by Member::get_unique_identifier_field() already? (this would be sapphire bug)
             $existingUniqueMember = DataObject::get_one('Member', "\"$uniqueField\" = '{$SQL_unique}'");
             if($existingUniqueMember && $existingUniqueMember->exists()) {
                if(Member::currentUserID() != $existingUniqueMember->ID) {
                   return false;
                }
             }
          }
          if(!$member = Member::currentUser()) {
             $member = new Member();
          }
          $member->update($data);
          return $member;
       }

       function updateCMSFields($fields) {
          $fields->removeByName('Country');
          $fields->addFieldToTab('Root.Main', new DropdownField('Country', 'Country', Geoip::getCountryDropDown()));
       }

       /**
        * Give the two letter code to resolve the title of the country.
        *
        * @param string $code Country code
        * @return string|boolean String if country found, boolean FALSE if nothing found
        */
       function getEcommerceFields() {
          $fields = new FieldSet(
             new HeaderField(_t('EcommerceRole.PERSONALINFORMATION','Personal Information'), 3),
             new TextField('Company', _t('EcommerceRole.COMPANY','Company')),
             new TextField('FirstName', _t('EcommerceRole.FIRSTNAME','First Name')),
             new TextField('Surname', _t('EcommerceRole.SURNAME','Surname')),
             new TextField('HomePhone', _t('EcommerceRole.HOMEPHONE','Phone')),
             new TextField('MobilePhone', _t('EcommerceRole.MOBILEPHONE','Mobile')),
             new EmailField('Email', _t('EcommerceRole.EMAIL','Email')),
             new TextField('Address', _t('EcommerceRole.ADDRESS','Address')),
             new TextField('AddressLine2', _t('EcommerceRole.ADDRESSLINE2','&nbsp;')),
             new TextField('City', _t('EcommerceRole.CITY','City')),
             new TextField('PostalCode', _t('EcommerceRole.POSTALCODE','Postal Code')),
             new DropdownField('Country', _t('EcommerceRole.COUNTRY','Country'), Geoip::getCountryDropDown(), self::find_country())
          );
          $this->owner->extend('augmentEcommerceFields', $fields);
          return $fields;
       }

       /**
        * Return which member fields should be required on {@link OrderForm}
        * and {@link ShopAccountForm}.
        *
        * @return array
        */
       function getEcommerceRequiredFields() {
          $fields = array(
             'Company',
             'FirstName',
             'Surname',
             'Email',
             'Address',
             'City',
             'Country'
          );
          $this->owner->extend('augmentEcommerceRequiredFields', $fields);
          return $fields;
       }

       public function CountryTitle() {
          return self::find_country_title($this->owner->Country);
       }

       //this method needs to be tested!
       public function onAfterWrite() {
          parent::onAfterWrite();
          self::add_members_to_customer_group();
       }

    }

  • Yataka
    Avatar
    Community Member
    6 Posts

    Re: Ecommerce 0.8.1 - add new member field Link to this post

    that's great!

  • Jedateach
    Avatar
    Forum Moderator
    227 Posts

    Re: Ecommerce 0.8.1 - add new member field Link to this post

    Hi Kiwipearls,

    My apologies for not answering your questions sooner. I'm not aware of a subscribe-to-all feature so that I can instantly get problems to my inbox.

    I've created a tutorial/recipe in the shop module docs:
    http://demo.ss-shop.org/docs/developer/en/Recipes/CustomisingFields

    regards,
    Jeremy

    775 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.