Post by hafa » Mon Apr 16, 2018 1:03 pm

I've added the appropriate queries in catalog\controller\product\product.php to determine if a customer has previously purchased a given product and confirmed that they are working correctly. Now, I'd like to conditionally display the "Add to Cart" button, based on the result of said query. Here's the conditional statement in product.php, wherein $rspurchchk is the query that checks for the purchase:

Code: Select all

	 if ($rspurchchk->num_rows > 0) {
		 $this->data['Purchased'] = True;
	}
	else {
		$this->data['Purchased'] = False;
	}
I've also tried:

Code: Select all

 if ($rspurchchk->num_rows > 0) {
		 $data['Purchased'] = True;
	}
	else {
		$data['Purchased'] = False;
	}
In Product/Product.twig, I've added the following:

Code: Select all

              {% if not Purchased %}
              <button type="button" id="button-cart" data-loading-text="{{ text_loading }}" class="btn btn-primary btn-lg btn-block">{{ button_cart }}</button>
            {% endif %}
The variable, "Purchased" in Product.twig is obviously not being populated from product.php; any assistance in achieving this would be appreciated.

Newbie

Posts

Joined
Sat Apr 07, 2018 4:02 am

Post by straightlight » Mon Apr 16, 2018 11:40 pm

Hi. No OC version posted, partial codes have been posted. Please read the forum rules.

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 hafa » Tue Apr 17, 2018 4:53 am

Version 3.0.2.0; full code too long to post; here is the full addition to product.php; hope it's enough:

Code: Select all

//Recordset and variable setting for customer ID from session variable 

// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
	die("Connection failed: " . $mysqli->connect_error);
}
//Set session variable 
$session_idParm = "0";
if (isset($_COOKIE['OCSESSID'])) {
	$session_idParm = $_COOKIE['OCSESSID'];
}

if ($rssessionchk = $mysqli->prepare("SELECT * FROM oc_session WHERE oc_session.session_id=?")) {
 
	// Bind variable to the parameter as a string. 
	$rssessionchk->bind_param("s", $session_idParm);
 
	// Execute the statement.
	$rssessionchk->execute();
	$rssessionchk->store_result();
	// Get the variables from the query.
	$rssessionchk->bind_result($session_idrslt,$datarslt,$expirerslt);
	// Fetch the data.
	$rssessionchk->fetch();
	 if ($rssessionchk->num_rows > 0) {
		//Set the data.
		$str1 = str_replace('"', "", $datarslt);
		$str2 = strstr($str1, 'customer_id:');
		$str3 = str_replace('customer_id:', "", $str2);
		$customerID = substr($str3,0,1);
		//echo "customerID=". $customerID. "<br>";
	}
	else {
		//echo "No customer ID";
	}
	// Close the prepared statement.
	$rssessionchk->close();
}
//Recordset and variable setting for purchase determination 
$product_idParm = "0";
if (isset($_GET['product_id'])) {
	 if ( is_numeric($product_idParm) == true){
		$product_idParm = $_GET['product_id'];
		//echo "productID Parm=". $product_idParm. "<br>";
	 }
}
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
	die("Connection failed: " . $mysqli->connect_error);
}
if ($rspurchchk = $mysqli->prepare("SELECT oc_order.order_id, oc_order_product.product_id FROM oc_order, oc_order_product WHERE oc_order.order_id = oc_order_product.order_id AND oc_order.customer_id = ? AND oc_order_product.product_id = ?")) {
 
	// Bind variable to the parameter as a string. 
	$rspurchchk->bind_param("ss", $customerID,$product_idParm);
 
	// Execute the statement.
	$rspurchchk->execute();
	$rspurchchk->store_result();
	// Get the variables from the query.
	$rspurchchk->bind_result($order_idrslt,$product_idrslt);
	// Fetch the data.
	$rspurchchk->fetch();
	 if ($rspurchchk->num_rows > 0) {
		 $data['Purchased'] = True;
		//echo $Purchased;
	}
	else {
		$data['Purchased'] = False;
	}
	// Close the prepared statement.
	$rspurchchk->close();
}

Newbie

Posts

Joined
Sat Apr 07, 2018 4:02 am

Post by straightlight » Tue Apr 17, 2018 5:10 am

Ah ! I see, you are trying to build an API for Opencart. No problem, refer to this example which will save you a lot of time compared to the code above: viewtopic.php?f=191&t=203344&p=719531#p719531 .

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 hafa » Tue Apr 17, 2018 6:50 am

straightlight wrote:
Tue Apr 17, 2018 5:10 am
Ah ! I see, you are trying to build an API for Opencart. No problem, refer to this example which will save you a lot of time compared to the code above: viewtopic.php?f=191&t=203344&p=719531#p719531 .
Thanks for the link, straightlight. I don't think that I'm trying to create an interface; only to selectively display the "Add to Cart" button on the product page based on whether or not a product has been previously purchased by a logged in customer. The post in the link refers to a later stage in the process; i.e., checkout.

If there's another (read: easier) way to achieve the selective display of the "Add to Cart" button, that would be great; any further advice is greatly appreciated.

Newbie

Posts

Joined
Sat Apr 07, 2018 4:02 am

Post by straightlight » Tue Apr 17, 2018 8:38 pm

Compared to the above codes, it could simply be done as a small extension with either VQMod / OCMod.

In your system/library/cart/cart.php file,

find:

Code: Select all

public function hasProducts() {
add above:

Code: Select all

public function hasProduct($product_id) {
		$product_data = array();
		
		$products = $this->getProducts();

		foreach ($products as $product) {
			$product_data[] = (int)$product['product_id'];
		}
		
		return (in_array((int)$product_id, $product_data));
	}
Then, in your catalog/controller/product/product.php file,

find:

Code: Select all

$this->model_catalog_product->updateViewed($this->request->get['product_id']);
add above:

Code: Select all

$data['product_in_cart'] = $this->cart->hasProduct($this->request->get['product_id']);
Then, in your catalog/view/theme/<your_theme>/template/product/product.twig file, you could add your conditional IF statement:

Code: Select all

{% if product_in_cart %}
    ...
{% else %}
    ...
{% endif %}
The '...' would be your condition statement, in this case, the opposite statement of the add to cart. Else, the add to cart button statement already used.

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 hafa » Fri Apr 20, 2018 1:14 am

Thanks for the option to display based on items purchased in the current session, straightlight. Please note, however, that the goal was to selectively display the button based upon any and all historical purchases for the logged in user (this is for a digital media site).

I got it straightened out, thanks to your advice and this post . The key was to set the variable in the $data array before this line:

Code: Select all

$this->response->setOutput($this->load->view('product/product', $data));

Newbie

Posts

Joined
Sat Apr 07, 2018 4:02 am

Post by straightlight » Fri Apr 20, 2018 2:42 am

As per my example demonstrates on the above, the $data array is still being implied also as per the topic you referred on the above displays before the setOutput is being used. In other words, it could of been another way to accomplish this by knowing if the product is already in the cart or not. From there, in the product.twig file, a simple IF statement or jQuery codes would of sufficed.

Glad you got it working out into your own words.

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
Who is online

Users browsing this forum: No registered users and 157 guests