Post by alexmath » Sun Oct 08, 2017 1:03 am

Hi all, basically I want an admin to have an extra option for products. this will be a file (pdf, or textfile-may be even image-doesnt matter really). It will show up among the product options. Then the client can see/view it or download it before checking out. How best can I achieve that? what files do i need to modify.
If possible, I prefer to do it using vqmod. otherwise it is fine. thanks alot.
Something like viewtopic.php?t=36625&start=40 but for OC 3.x. as file structure and template management completely changed.
Last edited by alexmath on Sun Oct 08, 2017 5:14 am, edited 1 time in total.

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by yodapt » Sun Oct 08, 2017 1:40 am

alexmath wrote:
Sun Oct 08, 2017 1:03 am
this will be a file (pdf, or textfile-may be even image-doesnt matter really).
Actually, it does.Displaying a download link, a text and a image is different, let alone saving such data.

Regardless, I would create an extra field in _product table for that situation, alter the admin part to save the desired field in each product and just get the field data in catalog. The files involved are :

admin/controller/catalog/product.php
admin/language/en-gb/catalog/product.php
admin/model/catalog/product.php
admin/view/template/catalog/product_form.twig
catalog/controller/product/product.php
catalog/language/en-gb/product/product.php
catalog/model/catalog/product.php
catalog/view/theme/default/template/product/product.twig

This structure assumes you're using english as your main language and the default template.

Opencart Developer - My Extension Showcase
Contact me at aeon.yoda@gmail.com


User avatar
Active Member

Posts

Joined
Fri Jun 17, 2011 6:39 pm


Post by alexmath » Sun Oct 08, 2017 2:19 am

how does it diplay a link? if it does, i can just attach the file to that link. Yes, I am using english as the main language. Do i need to do something with options and any of the twig files, otherwise, or not?

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by yodapt » Sun Oct 08, 2017 2:24 am

There is no easy way of doing it, only with a VQMod or OCMod. Template files are generic, you cant just inject a download link into them.

Opencart Developer - My Extension Showcase
Contact me at aeon.yoda@gmail.com


User avatar
Active Member

Posts

Joined
Fri Jun 17, 2011 6:39 pm


Post by alexmath » Sun Oct 08, 2017 5:13 am

alright, thanks alot. if there's no easy way of doing it, then i better get started learning vqmod scripting and twig. :). Thanks anyways.

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by alexmath » Fri Oct 13, 2017 9:56 pm

Hello yodapt, I think am ready to start messing with VQmod scripts, so let me first make sure we are on the same page here. This thing will be optional, and it will be just an option. meaning that only some products will have this option and and not all. Do I still have to make modifications to all the listed files?

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by yodapt » Fri Oct 13, 2017 11:03 pm

You have to modify the form in admin that creates / edits products, to add that option, and edit the files that show a product, checking if that option is active for the product and if so, displaying the file / image / pdf.

Opencart Developer - My Extension Showcase
Contact me at aeon.yoda@gmail.com


User avatar
Active Member

Posts

Joined
Fri Jun 17, 2011 6:39 pm


Post by straightlight » Sat Oct 14, 2017 7:08 am

Take note that the cart library also gathers the option and product option fields as well as the catalog model and possibly other developed extensions on the Opencart's core. Editing the admin files to accomplish what you need would be insufficient since both ends of the platform needs to gather the additional field from the database as well as the conditional routine of that field you'd like to add on the catalog-end for your customers.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

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

Post by alexmath » Wed Oct 18, 2017 8:25 am

Thanks alot guys, following what you all said, I some how managed to get it working perfectly for most types.
I have one issue though, it is working perfectly for inputs of type text, select, radio ... and so on but I have not managed to get the value of the files such as pdfs and images. for images, just the path is sufficient but nothing is saved in the database for them. How can I resolve this? thanks.
For now am trying to save the value of input with type set to file. How can i do it? or am i approaching it the wrong way?

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by straightlight » Wed Oct 18, 2017 8:45 am

For demonstration purposes, you could use something like this:

Code: Select all

$this->load->model('tool/image');
		$this->load->model('tool/upload');

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

		foreach ($this->cart->getProducts() as $product) {
			if ($product['image']) {
				$image = $this->model_tool_image->resize($product['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_cart_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_cart_height'));
			} else {
				$image = '';
			}

			$option_data = array();

			foreach ($product['option'] as $option) {
				if ($option['type'] != 'file') {
					$value = $option['value'];
				} else {
					$upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

					if ($upload_info) {
						$value = $upload_info['name'];
					} else {
						$value = '';
					}
				}

				$option_data[] = array(
					'name'  => $option['name'],
					'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value),
					'type'  => $option['type']
				);
			}

			// Display prices
			if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
				$unit_price = $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'));
				
				$price = $this->currency->format($unit_price, $this->session->data['currency']);
				$total = $this->currency->format($unit_price * $product['quantity'], $this->session->data['currency']);
			} else {
				$price = false;
				$total = false;
			}

			$data['products'][] = array(
				'cart_id'   => $product['cart_id'],
				'thumb'     => $image,
				'name'      => $product['name'],
				'model'     => $product['model'],
				'option'    => $option_data,
				'recurring' => ($product['recurring'] ? $product['recurring']['name'] : ''),
				'quantity'  => $product['quantity'],
				'price'     => $price,
				'total'     => $total,
				'href'      => $this->url->link('product/product', 'product_id=' . $product['product_id'])
			);
		}
Which originates from catalog/controller/common/cart.php file. There are also several other files that uses similar approach to accomplish this but each have a different ending result - depending on what needs to be gathered.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

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

Post by alexmath » Wed Oct 18, 2017 7:10 pm

ok this is a bit off now. I have something like this product.php from admin/controller/catalog/

Code: Select all

if (isset($this->request->$_FILES['filename']['name'])){
      		$data['filename'] = $this->request->$_FILES['filename']['name'];
    	} elseif (isset($product_info)) {
			$data['filename'] = $product_info['filename'];
		} else {
      		$data['filename'] = '';
  
The in the template file, I have something equivalent to:

Code: Select all

<input type="file" name="filename" value="<?php echo $filename; ?>" id="filename" />
I also tried something like $_POST['filename'] but that doesnt work either.

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by straightlight » Wed Oct 18, 2017 8:45 pm

The provided code above is incomplete. The $product_info's origin is not provided. By default, there are no filename key in the product info unless being customized. More information would be needed.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

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

Post by alexmath » Wed Oct 18, 2017 10:08 pm

oh sorry, thats from the model. Its got like this:

Code: Select all

if (isset($this->request->get['product_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
		}
what i don't get is why

Code: Select all

<input type="text">
and those i listed above work fine but

Code: Select all

<input type="file" />
does not. it's annoying.

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by straightlight » Wed Oct 18, 2017 10:47 pm

I still don't see where your ['filename'] field comes from originally. As for the $_FILES superglobals above, that would be incorrect. The correct structure with the centralized object of $this->request would be like the following example when validating files:

Code: Select all

if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
instead of:

Code: Select all

if (isset($this->request->$_FILES['filename']['name'])){

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

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

Post by alexmath » Thu Oct 19, 2017 3:32 am

it originally is a column in the database. I added it. But i basically only want to save the path to the file. so I put it as a column of type varchar. The admin uploads the file initially, then it's supposed to get stored into the db - its path more specifically. But thats whats not happening. if i change the type to text, then it gets stored, but the path to the file wont get stored. it always remains blank - NOT null though. which means the else part gets executed,
It is uploaded on the same screen as the screen where uploads are made on the admin dashboard. i wanted to put a screen shot but i dont know how to get the image feature of this editor to work.
Oh actually, I just got another better idea. instead of using this file option which takes it from the user's machine, what type do i have to use to get that image uploader from OC;s storage? I think that will be much easier to debug and even its functionality is better.

Attachments

screenshot.png

screenshot.png (4.26 KiB) Viewed 54696 times


New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by straightlight » Thu Oct 19, 2017 3:46 am

Storing a path into the database would not be the best suggested approach as opposed to the filename only. Using the defined path from your config.php file and admin/config.php file would be the best way.

Code: Select all

DIR_IMAGE . basename($product_info['filename']);
from your controller file, for example.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

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

Post by alexmath » Thu Oct 19, 2017 4:24 am

given the above setup, how would i achieve that?
so I abandon this:

Code: Select all

 if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
and instead apply this: in the admin controller file?

Code: Select all

$data['filename'] = DIR_IMAGE . basename($product_info['filename']);
Am gona give it a try right away.

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by alexmath » Sat Oct 21, 2017 5:41 am

Gratitude guys. mischief managed :).

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am
Who is online

Users browsing this forum: No registered users and 49 guests