0
votes

I want to add possibility for clients to receive orders personally in one of our shops. I tried to find some module which gives possibility to select in which shop they want to receive order but I haven't found anything for free. Because of that I want to create new module for it. What's more I'm totally new in prestashop and I don't know where to start or how to create this module. I spend two-three days reading how to do it and these are my assumptions:

  1. New carrier module can be created by extending CarrierModule class.
  2. I read some articles / documentation about hooks.
  3. I have created my first carrier module by editing module attached in this article http://www.prestashop.com/blog/en/carrier_modules_functions_creation_and_configuration/.
  4. What I achieved is that I installed module and used hook 'BeforeCarrier' to add some layout to page after selecting my carrier.

This is how my carrier should work:

  1. It should be a part of carrier list so customer is able to select it.
  2. If carrier is not selected nothing hapens. If carrier is selected by customer then button 'Choose shop' should be shown.
  3. After pressing button 'Choose shop' new window should be show with addresses of our shops (instead of new window it may be placed somewhere in current page).
  4. Window with shop adresses will contain list of addresses with radiobuttons and button to confirm selection.
  5. After confirmation of selection window will be closed and address should be shown as a part of carreir.
  6. E-mail with confirmation will contain information in which shop customer can collect order.
  7. Suppose that addresses will be hardcoded in php code.

These are my questions:

  1. I created new carrier module so I assume it works correctly (as described here http://www.prestashop.com/blog/en/carrier_modules_functions_creation_and_configuration/).
  2. How to add new button 'Choose shop' near selected carrier?
  3. Can I use hooks to add 'Choose shop' button?
  4. Where should I remember choosen shop address? Has 'Carrier' class place for it?
  5. How to add shop address to e-mails? Should I edit layouts? Does e-mail layout contain place for it or do I need to add new 'placeholder' for it?
  6. How to show chosen address on admin side?

To describe my problem more detail I have created few scenario (see attachment). I will be greatful for any help.

I've posted the same question on prestashop forum.

1

1 Answers

1
votes
  1. These example are usually old and poorly written. They lack structure. But for your purpose I suppose they're ok.
  2. Use hookDisplayCarrierList($args). Check $args to see which carrier has been selected, then return <select> element which you shop addresses. This hook is triggered every time a user selects a carrier and is return via Ajax. Therefore, you may not use ajax here.

    You should include you javascript in a file. Use hookDisplayHeader to detect when to insert this file into your page:

    public function hookDisplayHeader(){
    
    $propExists = property_exists($this->context->controller, 'php_self');
    
    if($propExists){
    
        $controllerName = $this->context->controller->php_self;
        if(in_array($controllerName, array('order', 'order-opc'))){
    
         // $this->context->controller->addJS($this->_path.'js/customcarrier.js');
    

    This Javascript file should check whether a valid shop has been selected before going to the next step;

    Because your Js code is in a file and the hookDisplayCarrierList cannot contain any JavaScript (because it returns Ajax),

    you should also make use of hookDisplayBeforeCarrier. Here you could insert you custom carrier ID - this way you'd know when to check for errors with your JS file.

  3. Same question as #2.

  4. The correct way to save the information would be to add a model. CustomCarrierSelectedAddress - or something like it. It would have these columns: id_cart, id_shop_address;

    The way you implement shop addresses is up to you. You may define them as constants or even make a new model for them.

    Models arent that hard to create, you just need to declare class properties, static variable $definition that's it.

    You may add you own methods. You should also add createTable()/dropTable() methods for convenience.

  5. This is more complicated. You could:

    • Send your own email about selected shop address.
    • Search the controller method which send the email you wish to change. Then you should override that method by copying the file to your module, delete all the other methods and rename the class definition inside -> class AdminAddressesController extends AdminAddressesControllerCore

    There should be an array of email placeholders and their values, which the controllers assigns.

    for example '{order_id}'. You should add your email variable to array {chosen_shop_info} and assign whole paragraph of text to it. Then you may use it in the actual email template which you can edit in BO.

    This is more or less the only way I know to edit the existing templates, because you can't do conditional statements inside email templates.

  6. To add chosen address to order page in BO, you should use another hook - hookDisplayAdminOrder. here you can add your own block to be display in order summary.

To find out which hooks are available, go to Hook.php and look for method exec(). Add this line error_log($hook_name). When you perform a specific action, executed hooks will be logged and you will see what kind of hook you need.