Is there way to check if ID exist in CI Chart, and just upadte quantity, of that product, if not add new item?
This is what i have so far, but nothing is working :(
if (isset($_POST['sendorder']))
{
$qty=$_POST['productquantity'];
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $heading
);
if (count($this->cart->contents())>0){
foreach ($this->cart->contents() as $item){
if ($item['id']==$id){
$data = array('rowid'=>$item['rowid'],
'qty'=>++$item['qty']);
$this->cart->update($data);
}
else{
$this->cart->insert($data);
}
}
}
}
Try this one
// set $flag default value to true for inserting item to the cart session
$insert_new = TRUE;
$bag = $this->cart->contents();
foreach ($bag as $item) {
// check product id in session, if exist update the quantity
if ( $item['id'] == '1' ) { // Set value to your variable
$data = array('rowid'=>$item['rowid'],'qty'=>++$item['qty']);
$this->cart->update($data);
// set $insert_new value to False
$insert_new = FALSE;
}
}
// if $insert_new value is true, insert the item as new item in the cart
if ($insert_new) {
$data = array(
'id' => 1,
'qty' => 1,
'price' => 40,
'name' => 'Product item'
);
$this->cart->insert($data);
}
Related
I save image in database as array ["product-04.jpg"]. I don't know how to display image to view. I used Crinsane/LaravelShoppingcart and got the following error: "Cannot use object of type Gloudemans\Shoppingcart\CartItem as array". Can everyone help me?
ProductController I saved image in db:
if($request->hasFile('images')){
$files = $request->file('images');
$extension = ['png','jpg','gif','jepg'];
foreach ($files as $key => $item) {
$nameFile = $item->getClientOriginalName();
$exFiles = $item->getClientOriginalExtension();
if(in_array($exFiles, $extension)){
$item->move(public_path().'/upload/images',$nameFile);
$arrNameFile[] = $nameFile;
}
}
}
if($arrNameFile){
$dataInsert = [
'name_product' => $nameProduct,
'categories_id' => json_encode($categories),
'colors_id' => json_encode($colors),
'sizes_id' => json_encode($sizes),
'brands_id' => $brand,
'price' => $price,
'qty' => $qty,
'description' => $description,
'image_product' => json_encode($arrNameFile),
'sale_off' => $sale,
'status' => 1,
'view_product' => 0,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => null
];
if($pd->addDataProduct($dataInsert)){
$request->session()->flash('addPd','success');
return redirect()->route('admin.products');
} else {
$request->session()->flash('addPd','Fail');
return redirect()->route('admin.addProduct');
}
} else {
$request->session()->flash('addPd','Can not upload image');
return redirect()->route('admin.addProduct');
}
}
CartController: I add products and want to show list products in cart
public function addCart(Request $request, $id)
{
$product = Products::select('name_product', 'id', 'price', 'qty', 'image_product')->find($id);
if(!$product) return redirect('/');
Cart::add([
'id' => $id,
'name' => $product->name_product,
'qty' => 1,
'price' => $product->price,
'options' => [
'images' => json_decode($product->image_product, true),
]
]);
return redirect()->back();
}
public function getListCart(){
$products = Cart::content();
return view('frontend.cart.showCart', compact('products'));
}
And view i get image in src : {{ URL::to('/') }}/upload/images/{{ $product->image_product[0] }}
session cart, i need to add same product again if product size is different:
foreach($cart_content as $cart_item)
{
if(($cart_item['id']==$_POST['product_id'])&&($cart_item['options']['size']==$_POST['optsize']))
{
$data = array(
'rowid' => $cart_item['rowid'],
'qty' => ($cart_item['qty']+$_POST['quantity']),
'options' => ($_POST['optsize'])
);
$this->cart->update($data);
unset($cart_item['rowid']);
$upd=1;
break;
}
}
I am working on Magento 1.7 version.
My code is below:-
$categoryArray = Mage::getSingleton('lookbook/category')->getOptionArray();
$catId = Mage::getSingleton('core/session')->getCatId(); //it has value 4
$fieldset->addField('category_id', 'select', array(
'label' => Mage::helper('lookbook')->__('Lookbook'),
'name' => 'category_id[]',
'values' => $categoryArray,
'value' => $catId,
'disabled' => true
));
How to set this value selected in drop-down of admin grid form in Magento?
Take a look # https://magento.stackexchange.com/questions/544/how-to-set-default-value-for-form-fields
protected function _prepareForm()
{
$form_data = new Varien_Object();
$form = new Varien_Data_Form();
$this->setForm($form);
....
if ( Mage::getSingleton('adminhtml/session')->getXyzData() )
{
$form_data = Mage::getSingleton('adminhtml/session')->getXyzData();
Mage::getSingleton('adminhtml/session')->setXyzData(null);
}
else if ( Mage::registry('xyz_data') ) {
$form_data = Mage::registry('xyz_data');
}
$catId = Mage::getSingleton('core/session')->getCatId(); // it has value 4
if( empty($form_data->getData('category_id')) ){
$form_data->setData('category_id', $catId);
}
$fieldset->addField('category_id', 'select', array(
'label' => Mage::helper('lookbook')->__('Lookbook'),
'name' => 'category_id[]',
'values' => $categoryArray,
'disabled' => true
));
....
$form->setValues($form_data);
$this->setForm($form);
}
Add this in the __construct method of your grid.
$this->_defaultFilter = array('category_id'=>Mage::getSingleton('core/session')->getCatId());
I have a page with two tabs, a search-tab and a tab with a grid of the database. After the user edits one of the items in the grid, I'd like to keep them on the grid tab, rather than the form tab which is first in order.
Is there a way to change the active tab on a page via code?
This is the code for the tabs:
protected function _beforeToHtml()
{
$this->addTab('search_string', array(
'label' => Mage::helper('advancedtranslate')->__('Find a string'),
'title' => Mage::helper('advancedtranslate')->__('Find a string'),
'content' => $this->getLayout()->createBlock("advancedtranslate/adminhtml_advancedtranslate")->toHtml(),
'active' => true
));
$this->addTab('list_untranslated', array(
'label' => Mage::helper('advancedtranslate')->__('Untranslated strings'),
'title' => Mage::helper('advancedtranslate')->__('Untranslated strings'),
'content' => $this->getLayout()->createBlock("advancedtranslate/adminhtml_grid")->toHtml(),
'active' => false
));
return parent::_beforeToHtml();
}
And this is the saveAction in my controller that handles the redirect:
public function saveAction(){
//write data away to core_translate table
$resource = Mage::getResourceModel('core/translate_string');
$request = $this->getRequest();
$translate_id = $request->getParam('id');
$original = $request->getParam('original_translation');
$custom = $request->getParam('string');
$locale = $request->getParam('locale');
$storeId = $request->getParam('storeid');
$storeViewSpecific = $request->getParam('storeview_specific');
if($storeId != 0 && $storeViewSpecific != 1){
$storeId = 0;
}
$resource->saveTranslate($original, $custom, $locale, $storeId);
//delete record from phpro table
$advancedTranslateRecord = Mage::getModel('advancedtranslate/advancedtranslate');
$advancedTranslateRecord->setId($translate_id)
->delete();
//clear the cache
Mage::app()->getCache()->clean();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')
->__('Translation was saved.'));
$this->_redirect('*/*/');
}
Why not just
$this->_redirect('*/*/', array('active_tab' => 'list_untranslated'));
Yes you can do so by changing the 'active' => true / false attribute in your _beforeToHtml()
... simply pass a parameter or set a session value in your saveAction()... so when the page gets redirected you check in your beforeToHtml() if the parameter is set you change the order of 'active' => $somevariable.
So basically do,
protected function _beforeToHtml()
{
$active = true;
if(Mage::getSingleton('admin/session')->getData('ActiveTab')) {
$active = false;
}
$this->addTab('search_string', array(
'label' => Mage::helper('advancedtranslate')->__('Find a string'),
'title' => Mage::helper('advancedtranslate')->__('Find a string'),
'content' => $this->getLayout()->createBlock("advancedtranslate/adminhtml_advancedtranslate")->toHtml(),
'active' => $active
));
$this->addTab('list_untranslated', array(
'label' => Mage::helper('advancedtranslate')->__('Untranslated strings'),
'title' => Mage::helper('advancedtranslate')->__('Untranslated strings'),
'content' => $this->getLayout()->createBlock("advancedtranslate/adminhtml_grid")->toHtml(),
'active' => !$active
));
return parent::_beforeToHtml();
}
for example i have Session::instance()->get('orders') which is an array of some arrays:
$first = array('id' = 1, 'name' => 'first', 'price' => 100);
$second = array('id' => 2, 'name' => 'second', 'price' => 200);
$_SESSION['orders'] = array($first, $second);
but if i use this
Session::instance()->set('orders', array(array('id' => 3, 'name' => 'third', 'price' => 300)));
this will erase first orders (id 1, id 2).
so how can i ADD but not ERASE data arrays to session array named 'orders'? array_push or something else?
Edit, didn't see your comment, it's perfect.
Self explanatory.
$session = Session::instance();
// Find existing data
$data = $session->get('orders');
// Add new array
$data[] = array('id' => 3, 'name' => 'new data', 'price' => 300);
// Resave it
$session->set('orders', $data);
As for me, I think that best way:
public function before() {
...
$this->variable = Session::instance()->get('key', array());
...
}
some code...
public function after() {
...
Session::instance()->set('key', $this->variable, Date::MINUTE);
...
}