Post by Hazey » Mon Jun 18, 2012 2:39 am

Hey everyone,
We're doing some heavy development for a client and thanks to the forums we have been able to find answers to everything (almost) with ease! Great development community here. Anyways the one I can't find a solid/working answer to is this:

We have an object that adds products to the cart, well we're facing issues. Firstly the best and easiest way to do this would be to add items to the cart via GET which for WHATEVER reason it was removed in 1.5, I've been trying all the different mods and solutions here on the forums but haven't had success.

I tried to bridge the gap by using the method found in this post: http://forum.opencart.com/viewtopic.php?f=20&t=49872 in which I did the following:

Code: Select all

		if ( ($_GET['route'] == "checkout/cart") && isset($_GET['product_id']) ) {
			$addtocart = true;
			
			$pids = array();
			$pids = $_GET['product_id'];
		}

	if ( $addtocart == true ) {
		echo '<form action="index.php?route=checkout/cart" id="frm" method="post">';
		for ($i=0; $i < count($pids); $i++) {
			echo '<input type="hidden" name="product_id" value="'.$pids[$i].'">';
			echo '<input type="hidden" name="quantity" value="1">';
		}
		echo '</form>';
	}
?>
<body<?
if ( $addtocart == true) {
	echo ' onLoad="document.getElementById("frm").submit()"';
}
?>>

it builds the form perfectly (although I have no tracking variable in there), and it seems to submit the form, but the cart stays empty. The reason I'm using an array is we need to add multiple products at once, I soon realized the form doesn't have a method to handle multiple objects but evenso I tried testing with just ONE product_id and it did the same - did not add it to the cart.

I also tried using the cart.php from here: http://forum.opencart.com/viewtopic.php?f=20&t=54138 but that just broke the add to cart buttons. This may be the solution but I can't figure out how to keep the add to cart buttons working.

I have about 20 threads open for various discussions on it but none seem to be working. ANY help would be appreciated big time, we're tyring to get this done ASAP. Thanks guys!

PS latest version of opencart.
Last edited by Hazey on Mon Jun 18, 2012 2:53 am, edited 1 time in total.

Newbie

Posts

Joined
Mon Jun 18, 2012 2:30 am

Post by jcsmithy » Mon Jun 18, 2012 2:48 am

Adding to cart only accepts one product_id at a time so that's probably one reason why it isn't working.

Code: Select all

		if (isset($this->request->post['product_id'])) {
			$product_id = $this->request->post['product_id'];
		} else {
			$product_id = 0;
		}
		
		$this->load->model('catalog/product');
						
		$product_info = $this->model_catalog_product->getProduct($product_id);
		
		if ($product_info) {			
			if (isset($this->request->post['quantity'])) {
				$quantity = $this->request->post['quantity'];
			} else {
				$quantity = 1;
			}
														
			if (isset($this->request->post['option'])) {
				$option = array_filter($this->request->post['option']);
			} else {
				$option = array();	
			}
			
			$product_options = $this->model_catalog_product->getProductOptions($this->request->post['product_id']);
			
			foreach ($product_options as $product_option) {
				if ($product_option['required'] && empty($option[$product_option['product_option_id']])) {
					$json['error']['option'][$product_option['product_option_id']] = sprintf($this->language->get('error_required'), $product_option['name']);
				}
			}
			
			if (!$json) {
				$this->cart->add($this->request->post['product_id'], $quantity, $option);

				$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
				
				unset($this->session->data['shipping_method']);
				unset($this->session->data['shipping_methods']);
				unset($this->session->data['payment_method']);
				unset($this->session->data['payment_methods']);
				
				// Totals
				$this->load->model('setting/extension');
				
				$total_data = array();					
				$total = 0;
				$taxes = $this->cart->getTaxes();
				
				// Display prices
				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$sort_order = array(); 
					
					$results = $this->model_setting_extension->getExtensions('total');
					
					foreach ($results as $key => $value) {
						$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
					}
					
					array_multisort($sort_order, SORT_ASC, $results);
					
					foreach ($results as $result) {
						if ($this->config->get($result['code'] . '_status')) {
							$this->load->model('total/' . $result['code']);
				
							$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
						}
						
						$sort_order = array(); 
					  
						foreach ($total_data as $key => $value) {
							$sort_order[$key] = $value['sort_order'];
						}
			
						array_multisort($sort_order, SORT_ASC, $total_data);			
					}
				}
				
				$json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total));
			} else {
				$json['redirect'] = str_replace('&', '&', $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']));
			}
		}
The only way your really going to get this working is to modify (or create) your own function that loops the ID's and adds them one after each other...

Code: Select all

// Form Structure
// <form post="checkout/cart/multiadd" method="POST">
//     <input type="hidden" name="pids[0][product_id]" value="23" />
//     <input type="hidden" name="pids[0][quantity]" value="1" />
//     <input type="hidden" name="pids[1][product_id]" value="34" />
//     <input type="hidden" name="pids[2][quantity]" value="2" />
//</form>

// Posting array //
// $this->request->post['pids'] = array(
      [0] = array(
              'product_id' => 23,
              'quantity'     => 1
      ),
      [1] = array(
              'product_id' => 34,
              'quantity'     => 2
      )
// );
You can then add the products to the cart like so...

Code: Select all

public function multiAdd() {
    foreach ($pids as $pid) {
      /// Product Validation 
      $this->cart->add($pid['product_id'], $pid['quantity'], $options);
    }
}
All above is an example and wouldn't actually work as it is, but the logic is there explaining.

Active Member

Posts

Joined
Fri Oct 01, 2010 9:54 pm

Post by Hazey » Mon Jun 18, 2012 2:52 am

Thanks for your reply, as I posted this I ended up solving the problem ha. If anyone has this issue simply add the following:

Code: Select all

		if ($this->request->server['REQUEST_METHOD'] == 'GET' && isset($this->request->get['product_id'][0])) {
                $myresult = count($_GET["product_id"]);

                for ($i = 0; $i < $myresult; $i++) {
					if (isset($this->request->get['option'][$i])) {
						$option = $this->request->get['option'][$i];
					} else {
						$option = array();
					}

				if (isset($this->request->get['quantity'][$i])) {
					$quantity = $this->request->get['quantity'][$i];
				} else {
					$quantity = 1;
				}

				unset($this->session->data['shipping_methods']);
				unset($this->session->data['shipping_method']);
				unset($this->session->data['payment_methods']);
				unset($this->session->data['payment_method']);

				$this->cart->add($this->request->get['product_id'][$i], $quantity, $option);
				}
				$this->redirect(HTTPS_SERVER . 'index.php?route=checkout/cart');

				}
Add that to controller/checkout/cart.php BELOW

Code: Select all

...
	public function index() {
		$this->language->load('checkout/cart');

		if (!isset($this->session->data['vouchers'])) {
			$this->session->data['vouchers'] = array();
		}
and ABOVE

Code: Select all

		// Update
		if (!empty($this->request->post['quantity'])) {
			foreach ($this->request->post['quantity'] as $key => $value) {
				$this->cart->update($key, $value);
			}
...
It works flawlessly and doesn't interfere with the POST method at all, and therefore add to cart works as it should but if you wish you can use the get method with the url being (for example) index.php?route=checkout/cart&product_id[0]=1102&product_id[1]=279&product_id[2]=121

You can also specify quantity[0]=x etc...
Last edited by Hazey on Mon Jun 18, 2012 3:52 am, edited 1 time in total.

Newbie

Posts

Joined
Mon Jun 18, 2012 2:30 am

Post by jcsmithy » Mon Jun 18, 2012 3:01 am

One thing you have to be careful about btw is all the other product validation you've missed out - There's nothing actually checking to see if the product has required options for instance..... but it depends if you need that function I guess.

Active Member

Posts

Joined
Fri Oct 01, 2010 9:54 pm

Post by Hazey » Mon Jun 18, 2012 3:49 am

Absolutely, in this specific case we only require the productid, but for others dependent upon the situation they'll have to add some verification functions to ensure the options they require are added as well :)

Newbie

Posts

Joined
Mon Jun 18, 2012 2:30 am

Post by DefconRhall » Tue Dec 04, 2012 4:02 pm

Hazey wrote:Hey everyone,

Code: Select all


		echo '<form action="index.php?route=checkout/cart" id="frm" method="post">';
		for ($i=0; $i < count($pids); $i++) {
			echo '<input type="hidden" name="product_id" value="'.$pids[$i].'">';
			echo '<input type="hidden" name="quantity" value="1">';
		}
		echo '</form>';

We are running OC 1.5.4 and we are making a landing page to help customers select a specific product so they will only need to add 1 item to their cart from the page. If I make a html form like that with the ID matching the product ID of what I want to add I would assume this would work, but it does not seem to.

Does this not work with 1.5.4 or is there another way I should be building links to add products outside of open cart itself?

New member

Posts

Joined
Thu Aug 30, 2012 11:00 am

Post by cpuricelli » Mon Dec 10, 2012 9:01 pm

Hi, I am trying to solve this issue by creating a .php file which receives the parameters in GET and then set the cart:

Code: Select all

<?php 

require_once('config.php');
require_once(DIR_SYSTEM . 'engine/registry.php');
require_once(DIR_SYSTEM . 'engine/model.php');
require_once(DIR_SYSTEM . 'library/session.php');
require_once(DIR_SYSTEM . 'library/cart.php');
require_once(DIR_SYSTEM . 'library/db.php');
require_once(DIR_SYSTEM . 'library/request.php');
	

// Registry
$registry = new Registry();

// Session
$session = new Session();
$registry->set('session', $session);

// Request
$request = new Request();
$registry->set('request', $request);

// Database
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);

// Cart
$cart = new Cart($registry);

// LANGUAGE
if (isset($_GET["lang"])) { 
	if ($_GET["lang"]=="it") { $language = "it"; } else { $language = "en"; }
} else { $language = "en"; }

if (isset($_GET["pid"])) { $product_id = $_GET["pid"]; } else { $product_id = 0; }
if (isset($_GET["qty"])) { $qty = $_GET["qty"]; } else { $qty = 0; }

// product options: id|value-id|value-id|value....
$product_options = array();
$popts = array();
if (isset($_GET["popts"])) { 
	//prendo le singole coppie id|valore
	$popts = explode("-", $_GET["popts"]);
	foreach ($popts as $popt) {
		$option = explode("|", $popt);
		//valorizzo l'array product_options con le coppie chiave-valore
		$product_options[$option[0]] = $option[1];
	}
}

$cart->add($product_id, $qty, $product_options);

header("Location:http://www.mysite.com/index.php?route=checkout/cart&language=".$language);

?>
I call it using an url like this:
http://www.mysite.com/iosapp.php?lang=i ... 7-423|5342

It works fine if this function is called by a flash app inside the site, but is is not working if it is called by an external app...
Any ideas?
Last edited by cpuricelli on Mon Dec 10, 2012 10:26 pm, edited 1 time in total.

Newbie

Posts

Joined
Tue Aug 09, 2011 8:56 pm

Post by DefconRhall » Mon Dec 10, 2012 9:51 pm

cpuricelli wrote:
It works fine if this function is called by a flash app inside the site, but is is not working if it is called by an external app...
Any ideas?
This could be an issue with flash where if I remember right you have to put a file in the site root to allow remote requests to be made. I can't remember the name of the file.

But I'll take some of your code here and put it into the project I am working on to initialize the cart.

New member

Posts

Joined
Thu Aug 30, 2012 11:00 am

Post by byens » Mon Jul 29, 2013 4:39 pm

Hazey wrote:Thanks for your reply, as I posted this I ended up solving the problem ha. If anyone has this issue simply add the following:

Code: Select all

		if ($this->request->server['REQUEST_METHOD'] == 'GET' && isset($this->request->get['product_id'][0])) {
                $myresult = count($_GET["product_id"]);

                for ($i = 0; $i < $myresult; $i++) {
					if (isset($this->request->get['option'][$i])) {
						$option = $this->request->get['option'][$i];
					} else {
						$option = array();
					}

				if (isset($this->request->get['quantity'][$i])) {
					$quantity = $this->request->get['quantity'][$i];
				} else {
					$quantity = 1;
				}

				unset($this->session->data['shipping_methods']);
				unset($this->session->data['shipping_method']);
				unset($this->session->data['payment_methods']);
				unset($this->session->data['payment_method']);

				$this->cart->add($this->request->get['product_id'][$i], $quantity, $option);
				}
				$this->redirect(HTTPS_SERVER . 'index.php?route=checkout/cart');

				}
Add that to controller/checkout/cart.php BELOW

Code: Select all

...
	public function index() {
		$this->language->load('checkout/cart');

		if (!isset($this->session->data['vouchers'])) {
			$this->session->data['vouchers'] = array();
		}
and ABOVE

Code: Select all

		// Update
		if (!empty($this->request->post['quantity'])) {
			foreach ($this->request->post['quantity'] as $key => $value) {
				$this->cart->update($key, $value);
			}
...
It works flawlessly and doesn't interfere with the POST method at all, and therefore add to cart works as it should but if you wish you can use the get method with the url being (for example) index.php?route=checkout/cart&product_id[0]=1102&product_id[1]=279&product_id[2]=121

You can also specify quantity[0]=x etc...
Any vqmod for this?

Selling Kristik - Jasa Foto Aura - Jual Lingerie - supplier baju anak - jasa seo


Active Member

Posts

Joined
Sat Dec 11, 2010 12:29 pm
Location - Surabaya

Post by sudheerpal2 » Wed Jan 27, 2016 8:09 pm

Hi

Do any body know any tweak of adding product directly to cart a& checkout. I means using GET method.

I am using Opencart V 2.1.0.1.

The omethod which is discussed here is not working on this version of Opencart.

Newbie

Posts

Joined
Wed Jan 27, 2016 8:06 pm
Who is online

Users browsing this forum: No registered users and 4 guests