I am using Codeigniter 3.1.9, and added items to my shopping cart, items are added and showing but when i put my system to hibernate mode with browser open, then after turning on my sytem, the cart shows 0 products. It should contain the item that i have added. Please help to sort out my issue.
My add to cart code snippet is:
$data = array(
'id' => $product['id'],
'qty' => 1,
'size' => $size,
'name' => $product['title'],
'image' => $product['image']
);
$this->cart->insert($data);
this is config file:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Codeigniter cart library save data in session. after hibernate, the session got destroy and your cart got cleared.
Related
I am trying to start the session with CodeIgniter, but it does not show any error either. I tried to change the session's driver to database and also I tried to specify the session path, but I didn't work.
$session_data = array(
'is_login' => TRUE,
'username' => $user->username,
'id' => $user->id,
'type' => $user->type,
'name' => $user->name,
);
$this->session->set_userdata($session_data);
From Comment
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = sys_get_temp_dir();
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
First open the file located at application/config/autoload.php
Find the following section.
The $autoload['libraries'] = array(); array holds the list of libraries that need to be autoloaded.
As per our requirement, let's change it to look like this:
$autoload['libraries'] = array('session');
Also, there's another way you could have achieved that. You can use the following code somewhere in your controller file to load the session library.
$this->load->library('session');
I use codeigniter for a project, my session is configured to use the mysql database :
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
In my controller i have a code like this :
$params = array(
"type" => $data['activite_name'],
"activite" => $data['activite'],
"nom" => $data['name'],
"prenom" => $data['fname'],
"tel" => $data['tel'],
"email" => $data['email'],
"adresse" => $data['adr'],
"cp" => $data['cp'],
"ville" => $data['ville']
);
....
#Load session library and save $params as session
$this->load->library('session');
$this->session->set_userdata($params);
#Test session
$sess_name = $this->session->userdata('name');
echo $sess_name;
#Load view
$this->load->view("templates/header_interne", $data);
$this->load->view("pages/payment_view", $data);
$this->load->view("templates/footer", $data);
So when payment_view is loaded i can see my $sess_name value, but when i refresh the page i lost this session, and so on for all my sessions.
Anyone has an idea please?
Thank you.
I am trying to add a product to the cart programmatically with some custom options. The item gets added to the cart correctly but none of the options ever get added. My code is:
require_once '../../app/Mage.php';
umask(0);
/* not Mage::run(); */
Mage::app('default');
Mage::getSingleton("core/session", array("name" => "frontend"));
$product_id = 2364;
$id_opt_value = 6072;
$final_opt_value = 6074;
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
'product' => $product_id,
'qty' => 1,
'options' => array(
$id_opt_value => '123456',
$final_opt_value => 'black gloss finish',
)
);
$cart->addProduct($product, $params);
$cart->save();
I have double checked and the option values are correct. I am using magento ce-1.9.0.0
Try to use Varien Object:
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $request);
Should work as well.
How to add the child product to cart programmatically?
I've a child product SKU, I want to
Fetch the supper attributes like size and color id and values from the products,
Then fetch the parent product id,
Pass the super attributes on param with quantity, to add to function cart.
//$cart->addProduct($product1, $options);
Here, how to pass the supper attributes on the $option variable? Please help me!!!!
Try this. I think it may need some improvements but it works good in simple cases.
$_product = $this->getProduct();
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($_product->getId());
// check if something is returned
if (!empty(array_filter($parentIds))) {
$pid = $parentIds[0];
// Collect options applicable to the configurable product
$configurableProduct = Mage::getModel('catalog/product')->load($pid);
$productAttributeOptions = $configurableProduct->getTypeInstance(true)->getConfigurableAttributesAsArray($configurableProduct);
$options = array();
foreach ($productAttributeOptions as $productAttribute) {
$allValues = array_column($productAttribute['values'], 'value_index');
$currentProductValue = $_product->getData($productAttribute['attribute_code']);
if (in_array($currentProductValue, $allValues)) {
$options[$productAttribute['attribute_id']] = $currentProductValue;
}
}
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
// Add a product with custom options
$params = array(
'product' => $configurableProduct->getId(),
'qty' => 1,
'super_attribute' => $options
);
$request = new Varien_Object();
$request->setData($params);
$cart->addProduct($configurableProduct, $request);
$session = Mage::getSingleton('customer/session');
$session->setCartWasUpdated(true);
$cart->save();
}
U can try.
$cart = Mage::getModel('checkout/cart');
$cart->init();
$productCollection = Mage::getModel('catalog/product')->load($productId);
$cart->addProduct($productCollection ,
array( 'product_id' => $productId,
'qty' => 1,
'options' => array( $optionId => $optionValue));
$cart->save();
I want to export a custom XML feed from Magento, and this is the following code I use:
<?php
header('Content-Type: text/xml'); // XML's a handy dandy format
include '../app/Mage.php'; // Include the magento core
include 'ArrayXml.php';
Mage::app(); //And start up the Magento app
$_result = array(); // Make sure we have a result array to store our products
$_products = Mage::getModel('catalog/product')->getCollection();
foreach($_products as $_product) {
$_result['produs'][] = array(
'denumire' => $_product->getName(),
'descriere_scurta' => $_product->getShortDescription(), //product's short description
'descriere_lunga' => $_product->getDescription(), // product's long description
'pret_intreg' => $_product->getPrice(), //product's regular Price
'pret_redus' => $_product->getSpecialPrice(), //product's special Price
'url_produs' => $_product->getProductUrl(), //product url
'fotografie_produs' => $_product->getImageUrl() //product's image url
);
}
$_converter = new ArrayXML();
echo $_converter->toXML($_result);
However, only the Product URL and the Image URL are giving me correct values. The rest are empty.
What gives?
'name' and others are an attributes, so you should call addAttributeToSelect:
$_products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect(array(
'name',
'short_description',
'description'
))
->addPriceData();