Displaying Label in opencart based on Category - php

I am trying to add labels on product list page based on special conditions. I have buy one get one free sale, so When user visits buy 1 get 1 free category, he should be able to see the label on the products(in my case, I have bogo.png image). Everything worked fine with the modifications I did until I searched on the store front for a product, I get undefined variable error.
2015-01-09 18:26:58 - PHP Notice: Undefined index: bogo in catalog/view/theme/YourTheme/template/product/product_collection.tpl on line 26
I did google search for the problem and browsed opencart forums for days without any luck. So here is what I did until now. On Category.php in catalog/controller file, I added under this array$this->data['products'][]= array(
'bogo' => $bogo,
And added this condition under getProducts specifying if the category id is the category id for the buy 1 get 1 free category set bogo to true.$results = $this->model_catalog_product->getProducts($data);
if($category_id==977){
$bogo = true;
}
else{
$bogo = false;
}
and on product_collection.tpl file, I did this change.
<pre><code>
<?php if( $product['bogo'] ) { ?>
<span class="product-label-bogo2"><img src='bogo.png'></span>
<?php } else if ($product['special']) { ?>
<span class="product-label-special"><span><?php echo $this->language->get( 'text_sale' ); ?></span></span>
<?php } ?>
</code></pre>
Everything is fine if I go to that category it displays the label perfectly, the problem is I get that above error only when I search for anything on store front.
Please note that before rating the question negatively, I am not familiar at all with php and I did my best with research for hours to solve this.

you need to add that piece of code
if($category_id==977){$bogo = true;}else{$bogo = false;}
$this->data['products'][]= array('bogo' => $bogo,
to controller/product/search.php => ControllerProductSearch#index
BTW: you will need the add the above code in every controller file that makes use of product/product_collection.tpl

Related

Get only first element from database

I want to show bought "products names" in prestashop orders page.
For prestashop 1.6 i already have a code that works, but using the same code on prestashop 1.7 give me duplicate orders ...
public function getProductsName($id_order, $tr)
{
$products=Db::getInstance()->executeS(
'SELECT product_name FROM '._DB_PREFIX_.'order_detail
WHERE id_order='.(int)$id_order);
$str='';
foreach($products as $val) {
$str.=substr($val['product_name'],0,35).'| ';
}
$str=trim($str,'|');
return $str;
}
Ok i found a sulution. Apart from the script i posted above i added to AdminOrdersController.php this piece of sql:
a.id_order AS product_name,
and added to product_name array:
'callback' => 'getProductsName',
and retouched the script to remove the last |:
see updated script in first post.
all changes are in AdminOrdersController.php
Thanks for all.
Final result: http://shrani.si/f/28/pH/2jxmMrf7/qq-photo20190704132806.jpg

how to show “Minimum Qty Allowed in Shopping Cart" in product listing page

I am using Magento 1.9. I want to show "Minimum Qty Allowed in Shopping Cart" in list.phtml page. For example I have set minimum quantity "6" in Product attribute then it should show "6" in front end.
I am trying to write this code but it is throwing an error. Maybe I am writing a wrong code.
<?php
$productQuantity = Mage::getModel("cataloginvetory/stock_item")->loadByProduct($_product->getId());
echo $productQuantity->getMinSaleQty(); ?>
I am getting following error
Fatal error: Call to a member function loadByProduct() on a non-object in //list.phtml
Here some logic
<?php
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
echo $stockItem->getMinSaleQty() && $stockItem->getMinSaleQty() > 0 ? $stockItem->getMinSaleQty() * 1 : null;
?>
Mage::getModel("cataloginvetory/stock_item") is not returning an object. Use var_dump() to find out what Mage's static function is returning.
echo var_dump(Mage::getModel("cataloginvetory/stock_item"));
I presume it will return with NULL or false. If that is the case something is going wrong in getModel() (probably incorrect parameters)
There are two mistakes that I see right off the bat.
First, "cataloginvetory/stock_item" is a typo. It should be "cataloginventory/stock_item."
Second, loadByProduct takes a product - not a productId.
It should look like:
<?php
$productQuantity = Mage::getModel("cataloginventory/stock_item")->loadByProduct($_product);
echo $productQuantity->getMinSaleQty(); ?>
I wasn't able to test the getMinSaleQty() function because I don't have that enabled on my site.

Drupal/Ubercart custom price php code for roles

Im building an e-commerce site for wholesale foods and the pricing for products change depending on the user logged in. Ive looked at member pricing and basically every module i could find to do with altering the price but they are either for drupal 6 or not really what im after. Im using Drupal 7 with ubercart 3.
Ive found this module http://drupal.org/project/uc_custom_price. It adds a field within product creation that allows custom php code to be added to each individual product which is exactly what im after. however im not that good with php which is why ive been hunting modules instead of changing code.
What ive got at the moment is:
if ([roles] == 'test company') {
$item->price = $item->price*0.8;
}
Except the [roles] part is the wrong thing to use there and it just throws errors. Ive tried using things like $users->uid =='1' to try to hook onto a user like that but that didnt work either.
what would be the correct variable to put there?
thanks
try this Drupal 7 global $user object
global $user; // access the global user object
if(in_array("administrator",$user->roles)){ // if its administrator
$item->price = $item->price*0.8;
}elseif(in_array("vip",$user->roles)){ // if its a vip
//..
}elseif(in_array("UserCompanyX",$user->roles)){ // if its a user from company X
//..
}
or
if($user->roles[OFFSET] == "ROLE"){
// price calculation
}
$user->roles is an array of the roles assigned to the user.
hope it helped
Make your own module with UC Price API:
http://www.ubercart.org/docs/developer/11375/price_api
function example_uc_price_handler() {
return array(
'alter' => array(
'title' => t('Reseller price handler'),
'description' => t('Handles price markups by customer roles.'),
'callback' => 'example_price_alterer',
),
);
}
function example_price_alterer(&$price_info, $context, $options = array()){
global $user;
if (in_array("reseller", $user->roles)) { //Apply 30% reseller discount
$price_info["price"] = $context["subject"]["node"]->sell_price - (
$context["subject"]["node"]->sell_price * 0.30) ;
}
return;
}
See also: http://www.ubercart.org/forum/development/14381/price_alteration_hook

Code Igniter - carts library random works

I’m using a cart library to get orders in my web bookstore. But when I call a addCart function on one of my book it’s works, but not all the time. Please, help
There is my model function:
function get_books_by_ID($id)
{
$this->db->where('BOOK_ID', $id);
$query = $this->db->get('books');
return $query;
echo vardump($query);
}
Controller:
function addCards($id=1)
{
$query = $this->Kategorie_model->get_books_by_ID($id);
if($query->num_rows() > 0)
{
$item = $query->row();
$data = array(
'id' => $item->BOOK_ID,
'qty' => 1,
'price' => $item->BOOK_Price,
'name' => $item->BOOK_Title
);
$this->cart->insert($data);
}
}
View:
<tr>
<td class="color"><b>Cena: </b><?php echo $data->BOOK_Price;?>zł</td>
<td class="border" id="koszyk" ><?php echo anchor('ksiegarnia/addCards/'.$data->BOOK_ID, 'Koszyk'); ?></td>
</tr>
UPDATE:
vardump is nothing necessary. I want to use var_dump. But the problem is related with adding items to the session with carts library. I have a bookstore, and when I call a addCarts function, sometimes items is added to Carts, and cart function total() and total_items displaying it, but sometimes when I call function, nothing is happened. The items not add into carts. I don't now why this thing have a place. Why the carts library works randomly?
I just ran into this issue and it seems that in the codeigniter cart library the insert function checks the product(s) id and name against a regex only allow alpha-numeric, dashes, underscores and periods
Adjust the regex to what may come up:
$this->cart->product_id_rules = '.a-z0-9_';
$this->cart->product_name_rules = '.\:-_ a-z0-9';
In my case it would randomly add thing to carts too. If you turn on logging and check you'll be able to see that the name or id may contain invalid chars
i am doing as you did, and like you said, the cart is randomly works,
it seems can only contain 3 product in cart when i add another product, the product i added wont get into cart..
i am so hopeless, i get mind to change cart libraries with 3rd party
it is because i did not setting session to save to database, like in the tutorial (cart & session) the cart need session to write to database in order to work.
when i set session write to database, the problem solved
you can try...

Codeigniter Pagination - I'm stumped

Ok, I followed the instructions in the example perfectly. Ultimately, pagination works, kind of.
I get all of the pages listed: 1 | 2 | > | Last. Etc.
The first one is active, like it should be. I did the querying correctly as well, because each link will result in the correct query.
However, when I click on number 2, it will show me the next set of products correctly, but it will display the pagination from the first page.
Whatever pagination button I click on, will return the main pagination set: 1 (selected) | 2 | > | Last. It never changes! I'm loosing my patience, can someone help?
I think I might know whats going on. You need to tell the pagination library which segment of the URL holds the offset.
For example, if your URL is /products/browse/all/20, you need to tell CodeIgniter that the 4th segment holds the offset
$config['uri_segment'] = 4;
The default for the library is URL segment #3. If the offset in your URL is not in position 3 and you forget to tell the pagination library this, it will interpret the wrong segment as being the offset. This can lead to the kind of behaviour you describe above where the pagination does not appear to change.
I also came across same error and finally was able to fix it. Just thought to share the code script, may be someone will be able to use it.
=====> Controller
// Default function
function index()
{
// Display listing
$this->listing();
}
function listing($argDataArr = array())
{
// Initialize pagination
$pageArr['base_url'] = $this->config->item('categoryBeAction')."/listing";
$pageArr['total_rows'] = 15; //assume
$pageArr['per_page'] = 5; //assume
//You need to tell the pagination library which segment of the URL holds the offset.
$pageArr['uri_segment'] = 4; //URL eg: http://localhost/myproject/index.php/backend/category/listing/5
$this->pagination->initialize($pageArr);
// Get list of categories
// Create data array and pass data to get function
$dataArr['limitRows'] = $pageArr['per_page'];
$dataArr['limitOffset'] = $this->uri->segment(4); //the dynamic value from this segment will be used as offSet
$viewArr['listArr'] = $this->category_model->get($dataArr);
//rest of the code...
}
======> Model
function get($argDataArr = array())
{
//Select the fields required
$this->db->select('id, name, parent_id, status');
$this->db->from($this->config->item('tbl_category','dbtables'));
$this->db->where('parent_id', $parentId);
$this->db->limit($argDataArr['limitRows'], $argDataArr['limitOffset']);
$this->db->order_by("name", "asc");
$query_result = $this->db->get();
return $query_result;
}
======> View page
<!-- Pagination -->
<tr>
<td align="right">
<?php echo $this->pagination->create_links(); ?>
</td>
</tr>
Which example?
echo $this->pagination->create_links();
^^Is this in your view?

Categories