1

How can I change the default group id when certain condition occurs?

For instance, the default group id is 1 which is general. But I have a new group which is 4 (memebership).

And I want to set the customer to this group when they are purchasing a club item, I have this code in onepage/billing.phtml

<?php if($orderHasWineClub === true):?>
    <?php Mage::getSingleton('customer/session')->setCustomerGroupId(4); //4 being the customer group id ?>
<?php endif;?>

after this,

<?php if(!$this->isCustomerLoggedIn()): ?>
                <li class="control">
                    <div class="form-group checkbox">
                        <label for="billing:create_account" class="required">
                            <input type="checkbox" name="billing[create_account]" id="billing:create_account" value="1" title="<?php echo  $this->__('Create your account for later use') ?>"<?php if($orderHasWineClub === true):?> class="required-entry"<?php endif;?>/>
                            <a href="#" class="button-cart-simple"><?php echo  $this->__('Create your account for later use') ?></a>
                        </label>
                    </div> 
                </li>

<!-- the code of setCustomerGroupId(4) -->
....
....

But I still get general as my customer group. so I think the code is not working.

Any idea?

I am suing IWD_OPC onestep checkout by they way.

1
  • are you want set this group to after order save or during checkout process – Amit Bera Aug 29 '14 at 1:49
1

You need set the customer group and save the user:

$customer = Mage::getSingleton('customer/session')->getCustomer();
$customer->setGroupId(4);
$customer->save();
4
  • actually i get an error a:5:{i:0;s:26:"Customer email is required";i:1;s:4775:"#0 C:\wamp\www\mysite\app\code\core\Mage\Customer\Model\Resource\Customer.php(76): Mage::exception('Mage_Customer', 'Customer email ...') – laukok Aug 28 '14 at 23:17
  • 1
    Customer is loggedin? When you make: echo $customer->getId(); you obtain a number? If user not loggedin you can't set group before logged – osrecio Aug 28 '14 at 23:26
  • yes the customer is not logged in. i want to let the customer to register on checkout and set a group for them depending what is in their order. – laukok Aug 29 '14 at 9:27
  • 1
    You can set a Magento's Registry Pattern to set global variable. alanstorm.com/magento_registry_singleton_tutorial. When your user not loggedin you set the registry, after registration get registri and set groupId and finally unregister the registry. – osrecio Aug 29 '14 at 9:31
1

Tealou,you code is work only for loggin customer.... else it not work

<?php if($orderHasWineClub === true && Mage::getSingleton("customer/session")->isLoggedIn()):?>
        <?php $customer = Mage::getSingleton('customer/session')->getCustomer();
$customer->setGroupId(4);
$customer->save(); ?>
    <?php endif;?>

you need to this code at community\IWD\Opc\controllers\JsonController.php function saveBillingAction in if (isset($data['create_account']) && $data['create_account']==1){

If you want for all type customer guest/register then need to more customization checkout

Most suitable resolution: goto saveBillingAction

add below before $this->checkNewslatter(); at jsoncontroller.php

$orderHasWineClub==yourvalue;

          if (!Mage::getSingleton('customer/session')->isLoggedIn()){
                    if (isset($data['create_account']) && $data['create_account']==1){
                        if($orderHasWineClub === true):
                            $data['customer_group_id']=4;
                        endif;  
                    }else{
                        //used for guest customer
                    }
                }else{
                        if($orderHasWineClub === true):
                            $customer = Mage::getSingleton('customer/session')->getCustomer();
$customer->setGroupId(4);
$customer->save();
                            $data['customer_group_id']=4;
                        endif;  

                }
7
  • thanks Amit. but how can I send the data of $orderHasWineClub into public function saveBillingAction()?? – laukok Aug 29 '14 at 9:34
  • can you tell $orderHasWineClub some value then $orderHasWineClub=your value – Amit Bera Aug 29 '14 at 9:38
  • yes I can. the problem is not about setting the value for $orderHasWineClub but how to send data into saveBillingAction() – laukok Aug 29 '14 at 9:45
  • 1
    you did not need send value to $orderHasWineClub to saveBiiling – Amit Bera Aug 29 '14 at 9:48
  • 1
    i will check.... – Amit Bera Aug 29 '14 at 9:59

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.