Code Igniter - carts library random works - php

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...

Related

specific php session variables lost in Chrome only,

I lose some session variables in Chrome only
After reading every similar question and answer on this site, I have to note:
It is not a 404 / missing favicon issue
Also not a redirect issue. The session is started and closed properly.
I have a $_SESSION['customer'] and $_SESSION['order'] variable, for instance.
As expected, $_SESSION['customer'] holds an object of customer data, $_SESSION['order'] holds an array of products.
When I add a product to the order, I need to look up any customer discounts for this specific product. Then it turns out in Chrome, the $_SESSION['customer'] variable is missing, and the sql query returns no discounts, since there is no customer number.
Let's give you some code to work with:
I am on the update order page, where I still have my customer and order variable.
I call this function with an ajax request to return a list of products:
public function live_search_product(){
$this->load->model('orders', 'orders');
$qry = $_GET['qry'];
if (strlen($qry)>0) {
$products = $this->orders_model->search_products($qry, false);
if ($products){
foreach ($products as $product){
echo ''.$product['short_title'] . '<br>';
}
}
}
}
I select the product from the dropdown list, and end up in this function, where I suddenly have lost my $_SESSION['customer'] object and $_SESSION['order'] array. Please note that all my other session variables still exist.
public function add_product() {
$this->load->model('orders', 'orders');
$product = $this->orders_model->get_product_by_id($this->router->id);
$discount = $this->discount->get_discount($product->id, $product->product_group_id, $_SESSION['customer']->id, $_SESSION['customer']->group_id );
$_SESSION['order'][$this->router->id] = array('product' => $product ,
'quantity' => 1,
'rental' => 0,
'bruto' => $product->price_ex,
'discount' => $discount->discount_percentage,
'netto' => ((100 - $discount->discount_percentage)/100) * $product->price_ex,
'total' => ((100 - $discount->discount_percentage)/100) * $product->price_ex);
$this->calculate_order_totals();
}
In firefox and iexplorer and edge there is no problem, it works.
I really hope you have any other suggestions.
**** EDIT ****
Every controller has an index function that is called automatically. route orders/orders will call orders/orders/index, and for instance route orders/orders/add_product, will call the function add_product in the orders controller (residing in the orders folder).
It turns out that in Chrome, somehow I get routed to the index function of this orders controller. Which is an overview orders function where I reset all order variables, to make sure nothing is left when I open a new order.
I have changed that, so this specific situation is solved.
However, can not understand why I get redirected to this function, when calling the add_product function directly.
Any ideas?

Displaying Label in opencart based on Category

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

Adding custom option to order line

I'm currently trying to add a custom option to a specific orderline on add to cart via the following:
public function addToPackageQuote()
{
$cart = Mage::getSingleton("checkout/cart");
$quote = Mage::getSingleton("checkout/session")->getQuote();
$packageId = Mage::getModel('MyTuxedo_OPP/Package')->checkPackageId();
$products = $this->sortArray();
foreach ($products as $productInfo) {
try {
$split = explode(",", $productInfo);
$_product = Mage::getModel('catalog/product')->load($split[0]);
if($_product->isConfigurable()) {
$simpleId = $this->getConfigurableSimple($split[1],$split[3],$split[0]);
} else {
$simpleId = $split[0];
}
$product = Mage::getModel('catalog/product')->load($simpleId);
$options = new Varien_Object(array(
"qty" => 1,
"custom_options" => array(
"package" => $packageId,
"packageName" => Mage::helper('MyTuxedo_OPP')->getPackageName()
)
));
$quote->addProduct($product, $options);
$this->_getSession()->setCartWasUpdated(true);
$quote->save();
} catch (Exception $e) {
echo $e->getMessage();
}
$this->addFreeItems();
}
$cart->save();
unset($_SESSION['products']);
unset($_SESSION['productId']);
$cart->save();
// Let's unset all the package sessions (apart from a few that are needed!).
$this->kill();
}
This method is completely seperate from the generic add to cart handler, and is used purely in a packages system so that it adds simple products exclusively (also breaks down configurables super attribute to find the simple product too).
These simple products have no custom options attached to them in the Magento backend, nor is it a goal to add custom options to the product itself. What I would like to do is attach custom options to the order-line that is then transferred over to the order if a purchase is made. So effectively data that is added at the add to cart method and no where else!
The add to cart method works as expected it's just not including the custom options I am trying to attach. I have also tried defining the options object as simply:
$options = new Varien_Object(array(
"qty" => 1,
"package" => $packageId,
"packageName" => Mage::helper('MyTuxedo_OPP')->getPackageName()
)
The above info, not including qty is not in the orderline object at all, and I can't seem to work out where to move on from here.
Endlessly googling at the moment so some help would be most appreciated!!
I do appreciate I’m instantiating the product model object twice in this, however the plan is to just get it working then optimise! :)
You have to set the custom options for the product before adding it to cart.
$product->setCustomOptions($options);
The in Mage_Sales_Model_Quote::_addCatalogProduct() the custom options will be added to the cart item.
See also here: http://www.magentocommerce.com/boards/viewthread/49659/
By the way: Your code may be pretty slow because you are loading products twice in a foreach loop. You should consider some refactoring by using the product collection instead. Also it looks kind of hackish to directly access the $_SESSION variable here. You could rather use the Checkout Session for that (Mage::getSingleton('checkout/session')).
I have now resolved this, after much headache. You can add a custom option to the cart and not have to instantiate the product object and save a custom option to do this, it can be done via tacking onto an observer, and pulling the quote items.
After tacking onto: sales_quote_add_item
I then used:
public function addCustomData($observer) {
$event = $observer->getEvent();
$quote_item = $event->getQuoteItem();
$quote = $session->getQuote();
$quote_item->addOption(array("product_id" => $quote_item->getProduct()->getId(),
"product" => $quote_item->getProduct(),
"code" => 'PackageId',
"value" => Mage::getModel('MyTuxedo_OPP/Package')->checkPackageId()
));
$quote->save();
}
It is most important to include the product object and id, as the function doesn't use the loaded object for some reason.
You can then get at the object via:
$_item->getOptionByCode('PackageId')->getValue();
Quick piece of handy info, if it dumps a stack trace in front of you it can't find the defined option, lose the getValue() (if using var_dump) function to see if you are getting a null value, otherwise xdebug will give you a ton of hints to get around it.

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

How can I use a database and PHP sessions to store a user's shopping cart?

How can I use a database and PHP sessions to store a user's shopping cart? I am using CodeIgniter, if that helps.
Example code would also be nice.
I would recommend that you look at the CodeIgnitor Session Class.
Additionally, you could look at Chris Shiflett's discussion on this topic.
I would write an add to basket function like this:
function AddToBasket(){
if(is_numeric($_GET["ID"])){
$ProductID=(int)$_GET["ID"];
$_SESSION["Basket"][]=$ProductID;
$sOut.=ShowBasketDetail();
return $sOut;
}
}
In this shopping basket function we save Product IDs in an session array.
Here is what I would have in the show basket function:
function ShowBasket(){
foreach($_SESSION[Basket] as $ProductID){
$sql="select * from products where ProductID=$ProductID";
$result=mysql_query($sql);
$row=mysql_fetch_row($result);
echo "Product: ".$row[0];
}
}
For each ProudctID in our session basket we make a SQL query to output product information.
Now last but not least, a clear basket function:
function ClearBasket(){
unset($_SESSION[Basket]);
}
Don't forget session_start(); before you add any Product IDs to your session basket. Also don't forget the mysql_connect(); function, you need this before you make any queries with the database.
how about this ;
- when guest add one item product in the cart
function addCartItem($item_id, $qty)
{
$basket = $this->session->userdata('basket');
if(!$basket)
{
$this->session->set_userdata('basket', array($item_id => $qty));
}
else
{
## get array from $basket and *merge some new value from input
}
}

Categories