Jump to content

[SOLVED] Restrict Access to "My Account" only to Customers who made a purchase


Recommended Posts

Hi everyone

I need to restrict access to "My Account" page. Only users who have already made at least one purchase/payment in the shop will be able to see "My Account" menu.

Eg:

User A registers but does't complete any purchase/payment, when he goes to his "My Account" menu he will just receive a notification "Please make a purchase" with a link to the catalog.

User B registers and  he completes a purchase/payment, he will be able to access "My Account" menu.

 

Is there any condition/variable to verify that the client has completed at least one payment?

Something like: 

{if $user-has-made-a-payment}

    //Show "My Account" menu

  {else}

    //Show Notification: You need to make a Purchase. (Link to Catalog)

  {/if}


I can see that in themes/classic/templates/customer/my-account.tpl "My Account" menu options can be edited, but I cannot figure it out how to make the condition and where to put it.
 

Thanks in advance

 

 

 

Edited by bepot (see edit history)
Link to comment
Share on other sites

hace 1 hora, bepot dijo:

Hi everyone

I need to restrict access to "My Account" page. Only users who have already made at least one purchase/payment in the shop will be able to see "My Account" menu.

Eg:

User A registers but does't complete any purchase/payment, when he goes to his "My Account" menu he will just receive a notification "Please make a purchase" with a link to the catalog.

User B registers and  he completes a purchase/payment, he will be able to access "My Account" menu.

 

Is there any condition/variable to verify that the client has completed at least one payment?

Something like: 


{if $user-has-made-a-payment}

    //Show "My Account" menu

  {else}

    //Show Notification: You need to make a Purchase. (Link to Catalog)

  {/if}


I can see that in themes/classic/templates/customer/my-account.tpl "My Account" menu options can be edited, but I cannot figure it out how to make the condition and where to put it.
 

Thanks in advance

 

 

 

Hi,

You want to disable the link at the top and bottom of the page or all access to the area of my account. ?

If it is only for the area of my account you will have to touch the controller /controllers/front/MyAccountController.php

Check HistoryController.php to know how you get a customer's orders

In the controller you can enable a variable to know if the user has placed an order and then pass the variable to the TPL to use it or directly deny access to the section.

Sorry for my English,

PD: Remember that the optimum is to make an override depends on modifying the original "controller".

Sorry for my English,

Edited by nadie (see edit history)
Link to comment
Share on other sites

Ok, I think it can be accomplished in a easier way:

Using themes/classic/templates/customer/my-account.tpl

You can add/remove "My Account" menu items. Those items are shown/hiden depending on a if statement, for example:

{if $customer.addresses|count}
        <a class="col-lg-4 col-md-6 col-sm-6 col-xs-12" id="addresses-link" href="{$urls.pages.addresses}">
          <span class="link-item">
            <i class="material-icons">&#xE56A;</i>
            {l s='Addresses' d='Shop.Theme.Customeraccount'}
          </span>
        </a>
      {else}
        <a class="col-lg-4 col-md-6 col-sm-6 col-xs-12" id="address-link" href="{$urls.pages.address}">
          <span class="link-item">
            <i class="material-icons">&#xE567;</i>
            {l s='Add first address' d='Shop.Theme.Customeraccount'}
          </span>
        </a>
      {/if}

This (if $customer.addresses|count) checks that the customer has already an address.

So how to check if the customer has already made a payment in the same way? something like: if $customer.order|count but I am afraid customer.order doesn't work.

Link to comment
Share on other sites

In Override 

MyAccountController

Add Use: 

use PrestaShop\PrestaShop\Adapter\Order\OrderPresenter;

-------

Declare variable:

 

      public $order_presenter;

---

after in the function:

 

	  public function initContent() {

add

 $this->order_presenter = new OrderPresenter();
       $orders = $this->getTemplateVarOrders();
 if (count($orders) <= 0) {
            $this->warning[] = $this->trans('Zona solo autorizada para clientes que han realizado al menos un pedido', array(), 'Shop.Notifications.Warning');
        }

With  if (count($orders) <= 0)  shows you an error message

...

If you also want to enable the use of the variable in the tpl add inside the function init:
 $this->context->smarty->assign(array(
            'orders' => $orders,
        ));

After add function:
 

 public function getTemplateVarOrders()
    {
        $orders = array();
        $customer_orders = Order::getCustomerOrders($this->context->customer->id);
        foreach ($customer_orders as $customer_order) {
            $order = new Order((int) $customer_order['id_order']);
            $orders[$customer_order['id_order']] = $this->order_presenter->present($order);
        }

        return $orders;
    }

Now you can use the variable in the TPL:
 

  {if $orders}


 

Edited by nadie (see edit history)
Link to comment
Share on other sites

For Prestashop 1.7.2.4

Unzip file: http://victor-rodenas.com/wp-content/uploads/2018/02/MyAccountController.zip

Upload in /override/controllers/front/

Clear Cache Prestashop in Tab Advanced Parameters ->Perfomance 

Now you can use the variable {if $orders} in my-account.tpl

Sorry for my English,

Edited by nadie (see edit history)
  • Like 1
Link to comment
Share on other sites

The file that I sent you only works for the "My account General" section

It does not let me upload images to the forum, I put them external ..

Attachment Capture: http://victor-rodenas.com/wp-content/uploads/2018/02/SeccionSuCuenta-1024x272.png

With the Override: http://victor-rodenas.com/wp-content/uploads/2018/02/MyAccountController.zip (/override/controllers/front/)

You can do in the file: /themes/classic/templates/customer/my-account.tpl

{if $orders}  
      <a class="col-lg-4 col-md-6 col-sm-6 col-xs-12" id="identity-link" href="{$urls.pages.identity}">
        <span class="link-item">
          <i class="material-icons">&#xE853;</i>
          {l s='Information' d='Shop.Theme.Customeraccount'}
        </span>
      </a>

      {if $customer.addresses|count}
        <a class="col-lg-4 col-md-6 col-sm-6 col-xs-12" id="addresses-link" href="{$urls.pages.addresses}">
          <span class="link-item">
            <i class="material-icons">&#xE56A;</i>
            {l s='Addresses' d='Shop.Theme.Customeraccount'}
          </span>
        </a>
      {else}
        <a class="col-lg-4 col-md-6 col-sm-6 col-xs-12" id="address-link" href="{$urls.pages.address}">
          <span class="link-item">
            <i class="material-icons">&#xE567;</i>
            {l s='Add first address' d='Shop.Theme.Customeraccount'}
          </span>
        </a>
      {/if}

      {if !$configuration.is_catalog}
        <a class="col-lg-4 col-md-6 col-sm-6 col-xs-12" id="history-link" href="{$urls.pages.history}">
          <span class="link-item">
            <i class="material-icons">&#xE916;</i>
            {l s='Order history and details' d='Shop.Theme.Customeraccount'}
          </span>
        </a>
      {/if}

      {if !$configuration.is_catalog}
        <a class="col-lg-4 col-md-6 col-sm-6 col-xs-12" id="order-slips-link" href="{$urls.pages.order_slip}">
          <span class="link-item">
            <i class="material-icons">&#xE8B0;</i>
            {l s='Credit slips' d='Shop.Theme.Customeraccount'}
          </span>
        </a>
      {/if}

      {if $configuration.voucher_enabled && !$configuration.is_catalog}
        <a class="col-lg-4 col-md-6 col-sm-6 col-xs-12" id="discounts-link" href="{$urls.pages.discount}">
          <span class="link-item">
            <i class="material-icons">&#xE54E;</i>
            {l s='Vouchers' d='Shop.Theme.Customeraccount'}
          </span>
        </a>
      {/if}

      {if $configuration.return_enabled && !$configuration.is_catalog}
        <a class="col-lg-4 col-md-6 col-sm-6 col-xs-12" id="returns-link" href="{$urls.pages.order_follow}">
          <span class="link-item">
            <i class="material-icons">&#xE860;</i>
            {l s='Merchandise returns' d='Shop.Theme.Customeraccount'}
          </span>
        </a>
      {/if}

      {block name='display_customer_account'}
        {hook h='displayCustomerAccount'}
      {/block}
{/if}

In this example I hide everything, but you can hide what you want individually

We have used the conditional
 

{if $orders} ...   {/if}

 

To show only when there are orders


This controlator is only for the "general section of my Account".
 

---------

Usually each section has its own controller.

Sorry for my English,

 

Edited by nadie
Updated message to leave it nice (see edit history)
Link to comment
Share on other sites

hace 20 minutos, bepot dijo:

Hola Victor

You are a genius :)

Muchísimas gracias por tu ayuda - Thanks very much for your support

hoho that you have edited the message and I had not noticed.

An honor to help you

Do not forget to add the word "Solved" to the title of the topic.
 

Sorry for my English,

Edited by nadie (see edit history)
Link to comment
Share on other sites

  • 5 years later...

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...