Jump to content

Calling functions from classes in themes/module


TommyS

Recommended Posts

Hi, guys!
I'm an intern at a web development company. With other interns we're working with Prestashop. We've never seen it before and it causes us a lot of problems.
We are usually told to modify something simple within CSS or a .tpl file, but recently we started to get tasks that are a bit too advanced for us. Not because these are hard, I can think of many solutions easily, but we know nothing of Smarty framework and how to use it properly.

So I have to add a new functionality to the newsletter (ps_emailsubscription) module.
After registering to it, the API has to generate a new one-use-only 5% discount voucher only for that specific user (Prestashop only allows to assign vouchers to registered users, so I added the 'email_user_unregistered' column to the 'ps_cart_rule' table).
When that happens it has to either send and email with it to the specific user or display it inside of the JS modal mox that I've created (it shows up after successfull registration to the newsletter).

I've found the functions that can help me accomplish my task:

  • modules\ps_emailsubscription\ps_emailsubscription.php --> getSubscribers()
  • classes\CartRule.php --> add()
  • and others from classes\db as backup

 

But I have no clue on how to call them within themes\PRS01\modules\ps_emailsubscription\views\templates\hook\ps_emailsubscription.tpl, which is where the view part of ps_emailsubscription module is located.

What is the syntax? How should I do it?

I'm a noob when it comes to OOP, I'm still trying to get used to it.

There's the code from ps_emailsubscription.tpl:

<div id="newslatter" class="col-sm-12">
<div class="block_newsletter">
  <div class="row">
	<div class="tt-content col-sm-5">
    <h1 class="tt-title">{l s='Subscribe to our newsletter' d='Shop.Theme.Global'}</h1>
	</div>

    <div class="block_content col-sm-7">
      <form action="{$urls.pages.index}#footer" method="post">
		    <div class="ttinput_newsletter">
            <input
              class="btn btn-primary float-xs-right hidden-xs-down"
              name="submitNewsletter"
              type="submit"
              value="{l s='Subscribe' d='Shop.Theme.Actions'}"
            >
            <input
              class="btn btn-primary float-xs-right hidden-sm-up"
              name="submitNewsletter"
              type="submit"
              value="{l s='OK' d='Shop.Theme.Actions'}"
            >
			 <div class="input-wrapper">
              <input
                name="email"
                type="text"
                value="{$value}"
                placeholder="{l s='Enter your Email' d='Shop.Forms.Labels'}"
                aria-labelledby="block-newsletter-label"
              >
			</div>
            <input type="hidden" name="action" value="0">
            <div class="clearfix"></div>
          </div>
          <div class="col-xs-12">
              {if $conditions}
                <p class="newsletter-desc">{$conditions}</p>
              {/if}
              {if $msg}
                <p class="alert {if $nw_error}alert-danger{else}alert-success{/if}">
                  {$msg}
				
                  <!-- MODAL BOX -->
                  <div id="myModal" class="modal">
                    <div class="modal-content">
                      <div class="modal-header">
                        <span class="close">&times;</span>
                        <h2>Registration successful!</h2>
                      </div>
                      <div class="modal-body">
                        <p>You've been assigned a 5% voucher!</p>
                        <p>Voucher: CODE<b></b></p>
                      </div>
                      <div class="modal-footer">
                        <h3>
                          Thanks!
                        </h3>
                      </div>
                    </div>
                  </div>

                  {if $nw_error}
                    {else}
            			<!-- EXECUTES AFTER SUCCESFFUL REGISTRATION TO THE NEWSLETTER -->
                      <script type="text/javascript">
                        var modal = document.getElementById('myModal');
                        var span = document.getElementsByClassName("close")[0];

                        modal.style.display = "block";
          
                        span.onclick = function() {
                            modal.style.display = "none";
                        }
                        window.onclick = function(event) {
                            if (event.target == modal) {
                                modal.style.display = "none";
                            }
                        }
                      </script>
            
            			<!-- WHERE THE REST OF THE SCRIPT HAS TO BE ADDED (I suppose) -->
                  {/if}
                </p>
              {/if}
          </div>
      </form>
    </div>
	</div>
</div>
</div>

Huge thanks in advance guys!

Link to comment
Share on other sites

Have you spoken with your mentor about this situation?

Honestly, if OOP has never been part of your education then you shouldn't be doing assignments like these. If you can't talk them out of it I would recommend a service such as https://hackhands.com. Students get free credits via the GitHub Education Pack: https://education.github.com

It saved my ass a few times. Good luck!

Link to comment
Share on other sites

7 minutes ago, PrestaRalph said:

Have you spoken with your mentor about this situation?

Honestly, if OOP has never been part of your education then you shouldn't be doing assignments like these. If you can't talk them out of it I would recommend a service such as https://hackhands.com. Students get free credits via the GitHub Education Pack: https://education.github.com

It saved my ass a few times. Good luck!

 

Yes, I have. We won't get any easier tasks, but we have a lot of time to finish them, because he knows that we need to learn the stuff or do a proper research.

I know how to do the assignment from a POP perspective, it's just that I haven't had much experience with proper OOP. However, I want, need and have to learn it to finish my personal projects.

This task is also a good moment to learn the Smarty/Symfony frameworks from Prestashop - it's just that I need to see how would someone more advanced do it and understand it. With help from this community finishing this and next challenges would be much easier. That's why I ask.

But let's simplify my question - how to call functions from these classes/modules in this specific scenario? :D

Link to comment
Share on other sites

You should rather focus on the research aspect then. Heck, I think you might even score some extra points if you mention an alternative such as WooCommerce!

You will not learn decent OOP practices from PresaShop because the code is absolute garbage, which is one of the reasons why the software, community and company are slowly dying. Here's some very useful info from a PS veteran to help you understand the current situation: https://medium.com/@prestashoppoweruser/prestashop-a-sad-story-8b7e0a8795b8

We are looking at moving our site (w/ millions of visits/mo) away from PS ourselves, because of the expiring support for the only working version, 1.6. Just gathering some old info from the forum before the big exodus.

To answer your question: you might better extend the module with a new front controller where you process the data. You should not be calling the functions from the tpl file. It is a view after all.

Hope you can get the help you need from the last remains of this forum. We might have to continue this conversation Reddit because this forum does not have freedom of expression.

Link to comment
Share on other sites

It is not a recommended way to call PHP functions from within Smarty templates, this is the whole point of separating the code from the view.

Therefore, your code should live in a controller which then assigns variables to be used in Smarty.

There are a lot of info about that in the PS forums.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...