codeigniter add same product with different sizes - codeigniter

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;
}
}

Related

How to increment quantity of laravel model without creating a copy

I'm working on trying to create a cart system in laravel, where selecting an item on a menu will add it as a cart item. If the item already exists on the cart, it should just increase the quantity. I have this almost entirely working, except that the solution I've worked out makes it so that in the case of needing to increment the quantity, it will also recreate the cart item, so that there is one both with and without the incremented quantity. I've put a lot of mental energy into struggling with this, and was hoping someone might be able to help. Here is the relevant code, in the controller for creating the cart item.
$cartItems = Cartitem::all();
if($cartItems->isEmpty()){
$cartItem = new Cartitem([
'name' => $request->name,
'image_url' => $request->image_url,
'description' => $request->description,
'price' => $request->price,
'quantity' => 1
]);
$cartItem->save();
} else {
forEach($cartItems as $item){
if($request->name != $item->name){
$cartItem = new Cartitem([
'name' => $request->name,
'image_url' => $request->image_url,
'description' => $request->description,
'price' => $request->price,
'quantity' => 1
]);
$cartItem->save();
} else {
++$item->quantity;
$item->save();
}
}
}
$oldItem = Cartitem::where('name', $request->name)->first();
if ($oldItem) {
$oldItem->increment('quantity');
}
else {
$newItem = new Cartitem([
'name' => $request->name,
'image_url' => $request->image_url,
'description' => $request->description,
'price' => $request->price,
'quantity' => 1
]);
$newItem->save();
}

Add order Laravel

I am adding order to database. It works like this: When ordering is clicked, the order is created in the Order table, at the same time the product items are also added to the OrderItem table, via the order_id foreign key. But I don't know how to get the order_id, because it is added at the same time, and the Order id is increments.
public function save(array $data, int $id = null){
$idCurrent = Auth::id();
$orderItems = $data['orderItems'];
//add to Order
Order::updateOrCreate(
[
'id' => $id
],
[
'user_id' => $idCurrent,
'shipping_fee' => $data['shipping_fee'],
'total' => $data['total'],
'payment' => $data['payment'],
'status_id' => 1,
]
);
//add to OrderItem
foreach($orderItems as $item){
OrderItem::Create([
'order_id' => 222, //=> ?????????????
'product_id' => $item -> product_id,
'quantity' => $item->quantity,
]);
}
return true;
}
$order = Order::updateOrCreate(
[
'id' => $id
],
[
'user_id' => $idCurrent,
'shipping_fee' => $data['shipping_fee'],
'total' => $data['total'],
'payment' => $data['payment'],
'status_id' => 1,
]
);
//add to OrderItem
foreach($orderItems as $item){
OrderItem::Create([
'order_id' =>$order->id
'product_id' => $item -> product_id,
'quantity' => $item->quantity,
]);
}
return true;
Avoid multiple call to databases.
When you create order with
$order = Order::updateOrCreate(
[
'id' => $id
],
[
'user_id' => $idCurrent,
'shipping_fee' => $data['shipping_fee'],
'total' => $data['total'],
'payment' => $data['payment'],
'status_id' => 1,
]
);
next you should is to send just one request to database with
// create batch array
$insertOrderItems = [];
foreach($orderItems as $item){
$insertOrderItems[] = [
'product_id' => $item->product_id,
'quantity' => $item->quantity,
];
}
// insert all at once in batch mode making just one call to database
$order->orderItems()->create($insertOrderItems);
Presuming you have sorted relations in Order and OrderItem models.

How to set default value in dropdown of admin grid form in magento?

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());

How to check if id exist in CodeIgniter Cart?

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);
}

Magento tab change/redirect

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();
}

Resources