Post by Orikh » Sat Jun 30, 2018 8:24 pm

Hi all

I have button which by <form> and 'method- post' goes to created custom page. But i have several problems.

First i will provide short explanation:
I set in controller access only for logged users and if not logged it should redirect to login page and on succes login go back to action page with product_id

Code: Select all

if (!$this->customer->isLogged()) {
            $this->session->data['redirect'] = $this->url->link('product/kredit&product_id=' . $this->request->post['product_id'], '', true);

            $this->response->redirect($this->url->link('account/login', '', true));
        }
This code after login send product_id by GET method, therefore on logged version i am gettidproduct_id by post, else by get

Code: Select all

if(isset($this->request->get['product_id'])) {
                $product_id = $this->request->get['product_id'];
            }
            else{
                $product_id = $this->request->post['product_id'];
            }
Problem:
1) On page refresh it is losing product_id and give error that not found

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by daniGo » Sat Jun 30, 2018 10:46 pm

Try this:

Code: Select all

if (!$this->customer->isLogged()) {
            $this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . (int)$this->request->post['product_id'], true);

            $this->response->redirect($this->url->link('account/login', '', true));
        }

http://www.gombac.si


Active Member

Posts

Joined
Wed Mar 20, 2013 4:49 pm
Location - Slovenia

Post by Orikh » Sun Jul 01, 2018 12:36 am

daniGo wrote:
Sat Jun 30, 2018 10:46 pm
Try this:

Code: Select all

if (!$this->customer->isLogged()) {
            $this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . (int)$this->request->post['product_id'], true);

            $this->response->redirect($this->url->link('account/login', '', true));
        }
Thank you very much) i lost maybe 4-6 hours for that, God bless you!)

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by Orikh » Sun Jul 01, 2018 5:20 am

daniGo wrote:
Sat Jun 30, 2018 10:46 pm
Try this:

Code: Select all

if (!$this->customer->isLogged()) {
            $this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . (int)$this->request->post['product_id'], true);

            $this->response->redirect($this->url->link('account/login', '', true));
        }
I still have problem with post data send, on language change i gives me error, cuz lose product_id. How to solve it? On case of login and redirect to required page is perfect, but on form submit with method post is problem(

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by straightlight » Sun Jul 01, 2018 6:56 am

Your issue is not detailly explained on the first post. However, here's the included language in the query and a more robust validation between the GET and POST method.

Code: Select all

if (isset($this->request->post['product_id'])) {
	$product_id = $this->request->post['product_id'];
	
} elseif (isset($this->request->get['product_id'])) {
	$product_id = $this->request->get['product_id'];
}

if (!$this->customer->isLogged()) {
	if (isset($this->request->post['product_id'])) {
		$this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . (int)$this->request->post['product_id'] . '&language=' . $this->config->get('config_language'), true);
		
	} elseif (isset($this->request->get['product_id'])) {
		$this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . (int)$this->request->get['product_id'] . '&language=' . $this->config->get('config_language'), true);
	}
	
	$this->response->redirect($this->url->link('account/login', '', true));
}

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.

F. Rules:

- viewtopic.php?f=176&t=200480
- viewtopic.php?f=176&t=200804


Regards,
Straightlight


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Orikh » Sun Jul 01, 2018 11:02 pm

straightlight wrote:
Sun Jul 01, 2018 6:56 am
Your issue is not detailly explained on the first post. However, here's the included language in the query and a more robust validation between the GET and POST method.

Code: Select all

if (isset($this->request->post['product_id'])) {
	$product_id = $this->request->post['product_id'];
	
} elseif (isset($this->request->get['product_id'])) {
	$product_id = $this->request->get['product_id'];
}

if (!$this->customer->isLogged()) {
	if (isset($this->request->post['product_id'])) {
		$this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . (int)$this->request->post['product_id'] . '&language=' . $this->config->get('config_language'), true);
		
	} elseif (isset($this->request->get['product_id'])) {
		$this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . (int)$this->request->get['product_id'] . '&language=' . $this->config->get('config_language'), true);
	}
	
	$this->response->redirect($this->url->link('account/login', '', true));
}
Thank you for reply. I tried your code, but it did not help to me. Still on languege change i lose my post data(product_id)
Error: "Notice: Undefined variable: product_id in MYLOCALDIR\catalog\controller\product\kredit.php on line 59"
even model function is not getting value: ModelKreditKredit->get(NULL, 1). instead of NULL should be product_id
SQL: select b.name, a.image from oc_product a, oc_product_description b where a.product_id = and a.product_id = b.product_id and b.language_id = 2

From sql you can see that i lose my post data(product_id), which i sent by post to this page on language change. by button press from any page. Session is not working also, otherwise it could save. But it works for redirect from login page(with GET method).

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by straightlight » Sun Jul 01, 2018 11:33 pm

You'd need to post a more detailed request with your files. Right now, it's only partial codes.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.

F. Rules:

- viewtopic.php?f=176&t=200480
- viewtopic.php?f=176&t=200804


Regards,
Straightlight


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Orikh » Mon Jul 02, 2018 12:20 am

Okay, I provide full codes for my page. Please explain me why it does not save product_id in session?
View:
I sent data by form method post

Code: Select all

<form method="post" action="{{ action_credit }}">
	<input type="hidden" name="product_id" id="product_id" value="{{ product_id }}" />
	<button class="button btn-cart kredit-type" type="submit"  title="{{ kredit_cart }}" ><span>{{ kredit_cart }}</span></button>
</form>
Controller
In "catalog/controller/product/product.php " added link(because i am testing in product page now)

Code: Select all

 $data['action_credit'] = $this->url->link('product/kredit');
Created Controller for page

Code: Select all

class ControllerProductKredit extends Controller {

    public function index() {
       // error_reporting(0);
        if (isset($this->request->post['product_id'])) {
            $product_id = $this->request->post['product_id'];

        } elseif (isset($this->request->get['product_id'])) {
            $product_id = $this->request->get['product_id'];
        }

        if (!$this->customer->isLogged()) {
            if (isset($this->request->post['product_id'])) {
                $this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . (int)$this->request->post['product_id'] . '&language=' . $this->config->get('config_language'), true);

            } elseif (isset($this->request->get['product_id'])) {
                $this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . (int)$this->request->get['product_id'] . '&language=' . $this->config->get('config_language'), true);
            }

            $this->response->redirect($this->url->link('account/login', '', true));
        }
            // Optional. This calls for your language file
            $this->load->language('product/kredit');
            $lccode =  (int)$this->config->get('config_language_id');

            // Optional. Set the title of your web page
            $this->document->setTitle($this->language->get('heading_title'));



            // Breadcrumbs for the page
            $data['breadcrumbs'] = array();

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/home')
            );

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link('information/static')
            );

            // Get "heading_title" from language file
            $data['heading_title'] = $this->language->get('heading_title');

            $data['action_credit_add'] = $this->url->link('product/kredit/add', true);

            // All the necessary page elements
            $data['column_left'] = $this->load->controller('common/column_left');
            $data['column_right'] = $this->load->controller('common/column_right');
            $data['content_top'] = $this->load->controller('common/content_top');
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
            $data['footer'] = $this->load->controller('common/footer');
            $data['header'] = $this->load->controller('common/header');

            $this->load->model('kredit/kredit');
            $data['product_info'] = $this->model_kredit_kredit->get($product_id, $lccode);
            // Load the template file and show output
            $this->response->setOutput($this->load->view('product/kredit', $data));

    }
  }
And MODEL

Code: Select all

<?php
class ModelKreditKredit extends Model{

    public function get($product_id,$lccode){
        $query = $this->db->query("select b.name, a.image from " . DB_PREFIX . "product a, " . DB_PREFIX . "product_description b where a.product_id = $product_id and a.product_id = b.product_id and b.language_id = $lccode");

        return $query->rows;
    }
}

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by straightlight » Mon Jul 02, 2018 1:03 am

Seems to be over-coded there. In your custom controller file, rather than creating your own model to gather the products, you could simply use:

Code: Select all

$this->load->model('catalog/product');

$data['product_info'] = $this->model_catalog_product->getProduct($product_id);
The current language is already associated with the query.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.

F. Rules:

- viewtopic.php?f=176&t=200480
- viewtopic.php?f=176&t=200804


Regards,
Straightlight


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Orikh » Mon Jul 02, 2018 2:21 am

straightlight wrote:
Mon Jul 02, 2018 1:03 am
Seems to be over-coded there. In your custom controller file, rather than creating your own model to gather the products, you could simply use:

Code: Select all

$this->load->model('catalog/product');

$data['product_info'] = $this->model_catalog_product->getProduct($product_id);
The current language is already associated with the query.
Thank you for reply.

It is not returning me anything. What could be wrong?

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by straightlight » Mon Jul 02, 2018 2:36 am

The only part missing from your codes above is after instantiating the redirect session super global, you need to validate it in your custom controller before passing the $product_id. My example was based on the URL but if in your case you are focusing on using that the redirected session super global has captured, you need to validate that variable afterwards from your custom controller file as well.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.

F. Rules:

- viewtopic.php?f=176&t=200480
- viewtopic.php?f=176&t=200804


Regards,
Straightlight


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Orikh » Mon Jul 02, 2018 3:07 am

straightlight wrote:
Mon Jul 02, 2018 2:36 am
The only part missing from your codes above is after instantiating the redirect session super global, you need to validate it in your custom controller before passing the $product_id. My example was based on the URL but if in your case you are focusing on using that the redirected session super global has captured, you need to validate that variable afterwards from your custom controller file as well.
Thank you for reply,

I am sorry for maybe stupid question, i am new in opencart and MVC. What is the right way to validate that variable?
Last edited by Orikh on Mon Jul 02, 2018 4:18 am, edited 1 time in total.

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by straightlight » Mon Jul 02, 2018 3:43 am

No problem on being the newbie. If you'd like, you could submit a new request in the Commercial Support section of the forum in order to get this done professionally.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.

F. Rules:

- viewtopic.php?f=176&t=200480
- viewtopic.php?f=176&t=200804


Regards,
Straightlight


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Orikh » Mon Jul 02, 2018 4:22 am

straightlight wrote:
Mon Jul 02, 2018 3:43 am
No problem on being the newbie. If you'd like, you could submit a new request in the Commercial Support section of the forum in order to get this done professionally.
Thank you very much for your help and advices, I want to learn right way, but i am not ready to pay for each my question and trying to solve it half professional. In future, after i will understand all, i will improove my knowledge by this way( commercial support ), at least i will understand that my way is wrong or to complicated) I hope u understand me, therefore can you please give me small hint and examle how can i do it?

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by sw!tch » Mon Jul 02, 2018 5:36 am

Code: Select all

 if (isset($this->request->post['product_id'])) {
            $product_id = $this->request->post['product_id'];

        } elseif (isset($this->request->get['product_id'])) {
            $product_id = $this->request->get['product_id'];
        }
^ you have an if / elseif conditional statement ...and what happens if product ID is not set? You're going to end up with "Notice: Undefined variable: product_id".

You should take account for a scenario where product_id is not sent or defined.

Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm

Post by Orikh » Mon Jul 02, 2018 2:04 pm

sw!tch wrote:
Mon Jul 02, 2018 5:36 am

Code: Select all

 if (isset($this->request->post['product_id'])) {
            $product_id = $this->request->post['product_id'];

        } elseif (isset($this->request->get['product_id'])) {
            $product_id = $this->request->get['product_id'];
        }
^ you have an if / elseif conditional statement ...and what happens if product ID is not set? You're going to end up with "Notice: Undefined variable: product_id".

You should take account for a scenario where product_id is not sent or defined.
Thank you for reply. I have this error on post method sent . After login it is working perfectly( it works with get method ). Somehow i should set product_id in session, it is done, but it not working and i do not know why

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by daniGo » Mon Jul 02, 2018 6:25 pm

Try this View:

Code: Select all

<form action="{{ action_credit }}" method="post" enctype="multipart/form-data">
  <input type="hidden" name="product_id"  id="input-product-id" value="{{ product_id }}" />
  <button type="submit" class="button btn-cart kredit-type"  title="{{ kredit_cart }}" ><span>{{ kredit_cart }}</span></button>
</form>
Controller:

Code: Select all

class ControllerProductKredit extends Controller {
    public function index() {
        if (isset($this->request->post['product_id'])) {
        $this->request->get['product_id'] = (int)$this->request->post['product_id'];
        }

        if (isset($this->request->get['product_id'])) {
            $product_id = (int)$this->request->get['product_id'];
        } else {
            $product_id = 0;
        }

        if (!$this->customer->isLogged()) {
            $this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . $product_id . '&language=' . $this->config->get('config_language'), true);

            $this->response->redirect($this->url->link('account/login', '', true));
        }

        $this->load->model('catalog/product');

        $product_info = $this->model_catalog_product->getProduct($product_id);
   
        if ($product_info) {
            $this->load->language('product/kredit');

            $this->document->setTitle($this->language->get('heading_title'));

            $data['breadcrumbs'] = array();

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/home')
            );

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link('information/static')
            );

            $data['action_credit_add'] = $this->url->link('product/kredit/add', true);
            
            $data['column_left'] = $this->load->controller('common/column_left');
            $data['column_right'] = $this->load->controller('common/column_right');
            $data['content_top'] = $this->load->controller('common/content_top');
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
            $data['footer'] = $this->load->controller('common/footer');
            $data['header'] = $this->load->controller('common/header');

            $this->response->setOutput($this->load->view('product/kredit', $data));
        } else {
            return new Action('error/not_found');
        }
    }
}

http://www.gombac.si


Active Member

Posts

Joined
Wed Mar 20, 2013 4:49 pm
Location - Slovenia

Post by Orikh » Mon Jul 02, 2018 9:35 pm

daniGo wrote:
Mon Jul 02, 2018 6:25 pm
Try this View:

Code: Select all

<form action="{{ action_credit }}" method="post" enctype="multipart/form-data">
  <input type="hidden" name="product_id"  id="input-product-id" value="{{ product_id }}" />
  <button type="submit" class="button btn-cart kredit-type"  title="{{ kredit_cart }}" ><span>{{ kredit_cart }}</span></button>
</form>
Controller:

Code: Select all

class ControllerProductKredit extends Controller {
    public function index() {
        if (isset($this->request->post['product_id'])) {
        $this->request->get['product_id'] = (int)$this->request->post['product_id'];
        }

        if (isset($this->request->get['product_id'])) {
            $product_id = (int)$this->request->get['product_id'];
        } else {
            $product_id = 0;
        }

        if (!$this->customer->isLogged()) {
            $this->session->data['redirect'] = $this->url->link('product/kredit', 'product_id=' . $product_id . '&language=' . $this->config->get('config_language'), true);

            $this->response->redirect($this->url->link('account/login', '', true));
        }

        $this->load->model('catalog/product');

        $product_info = $this->model_catalog_product->getProduct($product_id);
   
        if ($product_info) {
            $this->load->language('product/kredit');

            $this->document->setTitle($this->language->get('heading_title'));

            $data['breadcrumbs'] = array();

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/home')
            );

            $data['breadcrumbs'][] = array(
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link('information/static')
            );

            $data['action_credit_add'] = $this->url->link('product/kredit/add', true);
            
            $data['column_left'] = $this->load->controller('common/column_left');
            $data['column_right'] = $this->load->controller('common/column_right');
            $data['content_top'] = $this->load->controller('common/content_top');
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
            $data['footer'] = $this->load->controller('common/footer');
            $data['header'] = $this->load->controller('common/header');

            $this->response->setOutput($this->load->view('product/kredit', $data));
        } else {
            return new Action('error/not_found');
        }
    }
}
Thank you for reply.

I tried your code, but on language change it goes to else "The page you requested cannot be found!". Again same error, on language change lose of " product_id" ???

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm

Post by straightlight » Mon Jul 02, 2018 9:50 pm

As indicated on the above from my post, you need to ensure the:

Code: Select all

$this->session->data['redirect']
gets validated from your custom controller since you are passing the product_id and language in the redirected URL. The product ID needs to be extracted from the defined session super global.

Code: Select all

if (!empty($this->session->data['redirect'])) {
    $redirect_url = parse_url($this->session->data['redirect']);
    
    if (!empty($redirect_url['product_id'])) {
        $this->load->model('catalog/product');
        
        $data['product_info'] = $this->model_catalog_product->getProduct($redirect_url['product_id']);
    }
}
In your custom TWIG file, ensure to use the IF statement before beginning to use each product keys from the database since you may be providing the product ID to the users without guarantees in this case:

Code: Select all

{% if product_info %}
… your argument here …
{% endif %}

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.

F. Rules:

- viewtopic.php?f=176&t=200480
- viewtopic.php?f=176&t=200804


Regards,
Straightlight


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Orikh » Tue Jul 03, 2018 12:34 am

straightlight wrote:
Mon Jul 02, 2018 9:50 pm
As indicated on the above from my post, you need to ensure the:

Code: Select all

$this->session->data['redirect']
gets validated from your custom controller since you are passing the product_id and language in the redirected URL. The product ID needs to be extracted from the defined session super global.

Code: Select all

if (!empty($this->session->data['redirect'])) {
    $redirect_url = parse_url($this->session->data['redirect']);
    
    if (!empty($redirect_url['product_id'])) {
        $this->load->model('catalog/product');
        
        $data['product_info'] = $this->model_catalog_product->getProduct($redirect_url['product_id']);
    }
}
In your custom TWIG file, ensure to use the IF statement before beginning to use each product keys from the database since you may be providing the product ID to the users without guarantees in this case:

Code: Select all

{% if product_info %}
… your argument here …
{% endif %}
Thank you for reply.

I tried to var_dump($this->session->data['redirect']); and it gives me error "Undefined index: redirect" It is empty even if it comes threw GET post. OMG i can not understand problem, why from home page->product page it is working, but not working to my page? I think i did same...

Newbie

Posts

Joined
Sat Jun 30, 2018 8:06 pm
Who is online

Users browsing this forum: No registered users and 23 guests