I have given enough permissions for a authenticated user to be able to access "admin/commerce/stores/add/store" and create a new store.

I would want to try and restrict only one store per user. I know that now is working as designed so I'm just asking for a little guidance on where I should look to make it custom for my use-case.

In my case a user would be able to create one store, and when is creating a product , his store would be "pre-selected" as the store for that product. I thought that "Entityreference prepopulate" could be of help here..

Thanks

Comments

maciej.zgadzaj’s picture

Category: Task » Feature request

I guess for the moment the easiest way would be to override stores/add access callback to your own custom function, which would check both existing access rights and additionally verify whether user already has a store created.

I have a feeling there might be more people interested in such a feature, so I might add it to the module at one point...

cornelyus’s picture

Thanks!

cornelyus’s picture

Quick question.

I was able to implement this, seems to be working fine.

On another note, my authenticated users can create stores by themselves. But, choosing the enabled payments is not appearing on the form.

Am I missing something?

maciej.zgadzaj’s picture

I was able to implement this, seems to be working fine.

Mind sharing the code?

On another note, my authenticated users can create stores by themselves. But, choosing the enabled payments is not appearing on the form.

This should be fixed in Access to options and payment methods vertical tabs for new stores.

cornelyus’s picture

Mind sharing the code?

Not at all, but maybe this would only work for my case? Never contributed to modules before..

So, I changed the 'access callback' for the menu link to create stores , as found on commerce_store_ui.module file.

On my new access function

  global $user;
  $query = db_select('commerce_store', 'cp');
  $query->condition('cp.uid', $user->uid, '=')
      ->fields('cp', array('store_id'))
      ->range(0, 10);
  $result = $query->execute()->fetchAssoc();

  //Admin can still create stores for others
  if(!$result || in_array('administrator', array_values($user->roles)))
    return commerce_entity_access($op, $store, $account, 'commerce_store');
  else
    return FALSE;

Hope this is helpful for now.

fehin’s picture

Thank you for this great module!
I'm wondering if there is any progress on this. Merchants are only allowed to have one store on my website but they can create many at the moment.

fehin’s picture

I got around it by creating a rule. Whenever a user becomes a Merchant and isn't currently in ex-merchant role (to prevent two stores if old one wasn't deleted), a store is created for them. Now I just need to remove the add store link and filter store form in the body of the stores page.

shasha_s’s picture

Hello,
thank you for this nice module.

Can you please share, explain how you realized it with rules?

Thanks!

fehin’s picture

@shasha_s below is a copy of my rule. Like I said, I created another role called ex-merchant so you need to create one.
Also find the id of your roles and replace them in the rule before you import it.

{ "rules_create_store_for_merchants" : {
    "LABEL" : "Create store for merchants",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_insert" : [], "user_update" : [] },
    "IF" : [
      { "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "4" : "4" } } } },
      { "NOT user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "6" : "6" } } } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "commerce_store",
            "param_type" : "store",
            "param_title" : "[account:name]\u0027 store: Enter business name here",
            "param_creator" : [ "account" ]
          },
          "PROVIDE" : { "entity_created" : { "store_entity_created" : "Store entity created" } }
        }
      }
    ]
  }
}
shasha_s’s picture

fehin,thanks for your response.

I will check it out and will report

hedel’s picture

I did something similar using Profile2, I let the rule here and I'm listening ideas ...
(Adapt to your profiles and roles)

{ "rules_create_new_store" : {
    "LABEL" : "Create new store",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "store" ],
    "REQUIRES" : [ "rules", "profile2" ],
    "ON" : { "profile2_insert" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "profile2:user:profile-merchant:user" ],
          "roles" : { "value" : { "4" : "4" } }
        }
      }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "commerce_store",
            "param_type" : "store",
            "param_title" : "[profile2:user]",
            "param_creator" : [ "profile2:user:profile-merchant:user" ]
          },
          "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
        }
      }
    ]
  }
}