My client is using an Opencart v2.1.0.2. The problem is when I remove a single item from the cart, all other items also gets removed. Yes, it's using a custom template and here's the line that calls for the delete function:
<td class="text-center"><button type="button" onclick="cart.remove('<?php echo $product['cart_id']; ?>');" title="<?php echo $button_remove; ?>" class="btn btn-danger btn-xs"><i class="fa fa-times"></i></button></td>
I also need to find out what js file is being called when this button is clicked. If it'll help, this I think this is the function from the cart.php library that opencart runs when deleting an item from the cart:
public function remove($cart_id) {
$this->db->query("DELETE FROM " . DB_PREFIX . "cart WHERE cart_id = '" . (int)$cart_id . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'");
}
DELETE FROM
removes everything. It's missing the product id of what it's trying to remove.
WHERE only specifies: cart_id, customer_id and session_id - so this query removes everything matching that criteria. It doesn't hone in on a specific product, so it'll take them all.
Without knowing what variable it uses for the product ID, a proper query would be something like:
public function remove($cart_id) {
$this->db->query("DELETE '".(int)$this->request->post['product_id']."' FROM " . DB_PREFIX . "cart WHERE cart_id = '" . (int)$cart_id . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "'");
}
(int)$this->request->get['product_id'] being the $_GET request for most product id calls.
Another instance could be:
(int)$this->request->post['product_id'] for a $_POST call
The template may also do something like $product_id so check what variable its using if the post/get doesn't work.
Once you got the query tightened up, the javascript button only specifies a cart id (not a product). Get your product ID in there if you want my example to work. You'll need to pass both the product_id and cart_id; else, without cart_id, if other shoppers have the same item at the same time, one person removing it would remove for them all.
Related
I have a specific information page (page id is 12) and from admin panel i have assigned this page ID to particular product. For example, Information-id 12 is assigned to to Product1. When a user buys that product, and when the order status is set to 'Complete', user should have access to that page(information-id). I mean , to access this page, user should buy that product and order status is to be complete and user should logged in.
I am not getting how to do it. In order.tpl, i tried doing like this, but if i copy the URL and paste , it can be accessible without logging in.
here is my order_list.tpl
$end_date= date('Y-m-d', strtotime($start_date. ' + '.$date1. 'days'));
echo date('d/m/Y',strtotime($end_date));
?></td>
</tr>
<tr>
<td class="history">Product Name</td>
<?php $today = date('Y-m-d'); ?>
<?php if(($order['status']='Complete') AND ($today > $end_date)) { ?>
<td><strong><h4><font color="#FF0000">Either Your Course date is Expired or Order is not completed!!</font></h4></strong> </td>
<?php }
else { ?>
<td><strong><?php echo $order['pname']; ?></strong></td>
<?php } ?>
</tr>
Here i am also checking the expiry date, which am capturing through admin.
If i put customer_islogged(), in information.php(controller), i cannot access any other pages.
Create a function where you can pass list of products, any of that needs to be purchased to access that particular page
//catalog/model/account/order.php
public function checkForAccess($product_id_list){
$exists = false;
$query = $this->db->query("SELECT product_id FROM ".DB_PREFIX."order o LEFT JOIN ".DB_PREFIX."order_product op ON (o.order_id=op.order_id) WHERE o.order_status_id=5 AND o.customer_id = '" . (int)$this->customer->getId() ."'");
foreach($query->rows as $product){
if(in_array($product['product_id'],$product_id_list)){
$exists = true;
break;
}
}
return $exists;
}
Now in that particular page you can check if the customer has completed purchased or not
$this->load->model('account/order');
//Eg Customer will have access who have purchased product 20 and 22
$product_list = array(20,22);
$results = $this->model_account_order->checkForAccess($product_list);
if(!$results){
//where to redirect if not valid
$this->response->redirect($this->url->link('common/home'));
}
How can I show the the weight (with units) in product page in 1.5.6.
Example: Weight - 2.5 kg; Weight - 250 mg ...
I've been reading some articles, but nothing working correctly.
You can format weight with Opencart's weight class format() method like this:
$this->weight->format($weight,$weight_class_id);
The getProduct() method already supplies both values you need so you can easily call it from the product controller like so:
$this->data['product_weight'] = $this->weight->format($product_info['weight'],$product_info['weight_class_id']);
Then display $product_weight wherever you want in the tpl as you would any other variable. This will give you exactly what you want but with no space (i.e., '250mg').
If you want more control over the formatting, you can also get only the unit abbreviation (e.g., 'kg', 'oz', 'lb', etc) with the getUnit() method like this:
$this->weight->getUnit($product_info['weight_class_id']);
You could then put them together however you want. If you want a space for instance:
$this->data['product_weight'] = $product_info['weight'] . ' ' . $this->weight->getUnit($product_info['weight_class_id']);
go to catalog/language/english/product/product.php,Find:
$_['text_model'] = 'Product Code:';
Add below code after it
$_['text_weight'] = 'Weight:';
open catalog/controller/product/product.php, Find:
$data['text_stock'] = $this->language->get('text_stock');
Add below code after it:
$data['text_weight'] = $this->language->get('text_weight');
In same file search for the code:
$data['model'] = $product_info['model'];
Paste below code after it:
$data['weight'] = $product_info['weight'];
$tablewunit = $this->db->query("SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE (wcd.weight_class_id = " . $product_info['weight_class_id'] . ") AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
$data['weight_unit'] = $tablewunit->row['unit'];
Now open catalog/view/theme/---yourtheme---/template/product/product.tpl, and find:
<li><?php echo $text_stock; ?> <?php echo $stock; ?></li>
Add below code after it:
<td><b><?php echo $text_weight; ?></b></td>
<td><?php echo round ($weight, 2) . ' ' . $weight_unit; ?></td>
<tr>
<?php if ($weight) { ?>
<tr>
<?php } ?>
Thats all
Hello fellow Bulgarian i assume by your name.
If you want to display weight on products it's very simple.
Open your FTP and go to catalog/view/theme/YOURTHEME/template/product and download product.tpl
find this line
<span class="reward"><small><?php echo $text_points; ?> <?php echo $points; ?></small></span><br />
<?php } ?>
Right after it paste this
<?php
echo round($weight,2). "kg";
?>
(Or paste it where ever you want to see it on the product page.)
after that go to /catalog/controller/product and open product.php
find this line
$this->data['model'] = $product_info['model'];
and right after it paste this line:
$this->data['weight'] = $product_info['weight'];
and you are pretty much done with that.
Hopefully i understood your question correctly.
How to add a new input field in opencart admin categories similar to name or description fields?
Getting error on header and not work
Notice: Undefined index: title in C:\wamp\www\opencart\admin\model\catalog\category.php on line 245
I tried but not work?
1.Add a 'title' column to the database table 'category_description' .
ALTER TABLE `category_description` ADD `title` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
2.In admin/view/template/catalog/category_form.php add after the input for name .
<tr>
<td>Title</td>
<td><input name="category_description[<?php echo $language['language_id']; ?>][title]" size="100" value="<?php echo isset($category_description[$language['language_id']]) ? $category_description[$language['language_id']]['title'] : ''; ?>" /></td>
</tr>
3.In admin/model/catalog/category.php
After
description = ' " . $this->db->escape($value['description']) . " '
Add
,title = ' " . $this->db->escape($value['title']) . " '
After
'description' => $result['description']
Add
,'title' => $result['title'],
Anyone help?
First go to the admin language folder. You will find category.php,you have to assign tittle value there first, then you fetch that value form the category controller file and assign it on data[] array after that you will get that tittle value in the template file.
I am using this module which allows me to run a simple blog / news feed in OC which does what I need apart from one thing, I need to display the first 4 articles on the homepage.
I have got the following so far:
<?php
$sql = "SELECT * FROM " . DB_PREFIX . "blog b LEFT JOIN " . DB_PREFIX . "blog_description bd ON (b.blog_id = bd.blog_id) WHERE b.status = 1 AND b.date <= NOW() AND bd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
$query = $this->db->query($sql);
$blogs = array();
?>
<div class="box" id="news">
<div class="title">
<p>Latest News</p>
</div>
<div class="content">
<?php
foreach($query->rows as $result){
$blogs[] = $result;
}
?>
</div>
</div>
I have been developing OC templates for a while but modules are a whole new ball game for me, any help would be appreciated
First of all You should learn and understand how the MVC (or whatever it tends to be) is implemented in OpenCart - we have controllers, models and view templates.
Your approach is mixing all the controller and model part into a view template which is completely wrong.
So what should go where:
SQL query should go into the model
A new method for retrieving the data from the model and preparing it for the view template should go into the controller
Only HTML markup should be present in the view template
Let's say the extension You have downloaded has a controller here: catalog/controller/information/news.php - You should extend it's index() (or other appropriate) method (or even create a new one if needed) so that it calls the news model where You place Your new method getLastFourArticles() which should look like:
public function getLastFourArticles() {
$sql = "
SELECT *
FROM " . DB_PREFIX . "blog b
LEFT JOIN " . DB_PREFIX . "blog_description bd ON b.blog_id = bd.blog_id
WHERE b.status = 1
AND b.date <= NOW()
AND bd.language_id = " . (int)$this->config->get('config_language_id') . "
ORDER BY b.date DESC
LIMIT 4";
return $this->db->query($sql);
}
ORDER BY part will sort the blog entries from the newest to the latest and the LIMIT 4 part will make sure we only receive 4 rows maximally.
Now in the controller You should do something like:
$this->data['latest_entries'] = $this->model_information_news->getLastFourArticles();
while expecting the model to be catalog/model/information/news.php and that it is loaded ($this->load->model('information/news');).
Now in Your template only this part is needed:
<div class="box" id="news">
<div class="title">
<p>Latest News</p>
</div>
<div class="content">
<?php foreach($latest_entries as $entry) { ?>
<span class="date"><?php echo $entry['date']; ?></span>
<span class="entry"><?php echo $entry['text']; ?></span>
<?php } ?>
</div>
</div>
Keep in mind this is only instruction-like answer and You should pass in the right names and variables (and indices for the blog entry in the template).
I have an ebook item in osCommerce that includes product attributes. When I click the Add to Cart button from the index page it should go directly to the shopping_cart.php page. However, it only keeps redirecting the customer back to the product_info.php page.
Why is it doing that?
If the product has attributes osCommerce expects that you select one of them before adding the product to the cart. That's why it is redirecting you to the product detail page.
try this code:
find if ($col === 0) {$new_prods_content .= '';} (around line 29)
below add this code:
$new_prods_content .= '<td width="30%" align="center" valign="top">
<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' .
$new_products['products_id']) . '">' . tep_image(DIR_WS_IMAGES .
$new_products['products_image'], $new_products['products_name'],
SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br /><a href="' .
tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' .
$new_products['products_id']) . '">' . $new_products['products_name'] .
'</a><br />' . $currencies->display_price($new_products['products_price'],
tep_get_tax_rate($new_products['products_tax_class_id'])) .
'   ' . tep_draw_button(IMAGE_BUTTON_IN_CART, null ,
tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) .
'action=buy_now&products_id=' . $new_products['products_id'])) .'</td>';
i have added this code for new products in index.php page and when customer clicks add to cart button, it will add the item to cart and show shopping_cart.php page.