Change value in session and save it. Laravel - php

I want to update product price when I submit coupon code I named my coupon as ticket. This is my function:
public function order(Request $request)
{
$products = session('cart');
$ticket_code = Input::get('ticket');
$ticket = Ticket::where('ticket', $ticket_code)->first();
foreach($products as $p){
$price = $p['price'];
}
if(count($products) && $ticket['max'] > $ticket['used']) {
$subtotal = [$price*70/100];
$ticket->used += 1;
$ticket->save();
foreach($products as $p){
$p['price'] = $subtotal;
$p->save();
}
flash()->success('Kuponas sÄ—kmingai panaudotas!');
return view('cart.order')->with(array(
'products' => $products,
'subtotal' => $subtotal,
));
}
else {
$finalTotal = 0;
$subtotal = [];
return view('cart.order')->with(array(
'products' => $products,
'subtotal' => $subtotal,
'finalTotal' => $finalTotal
));
}
}
this code doesnt work:
foreach($products as $p){
$p['price'] = $subtotal;
$p->save();
}
I get error: Call to a member function save() on array
Without the foreach the ticket itselfs works. It changes the price when submited, but only in that one page. Then when I go to payment site, the price is still displayed without ticket used.
THE WORKING SOLUTION
foreach($products as &$item) {
$item['price'] = $item['price']*70;
$item['price'] = $item['price'] / 100;
}
Session::put('cart', $products);

THIS WORKED
foreach($products as &$item) {
$item['price'] = $item['price']*70;
$item['price'] = $item['price'] / 100;
}
Session::put('cart', $products);

Related

How to Programatically cancel order in magento 2

If a customer placed an order of 3 different items with different quantities like
shirt 2 qty
Watch 1qty
pant 3qty
and cancels the order item at different times I have to set the state to order cancelled
here's my code can anyone please tell me where I am wrong
if (isset($_POST['order'])) {
$_orderCollectionFactory = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
$collection = $_orderCollectionFactory->create()
->addFieldToSelect('*')
->addFieldToFilter('status', ['neq' => 'cancelled']);
foreach ($collectionFactory as $order)
{
$items = $order->getAllVisibleItems();
foreach ($items as $items) {
$totalitem = $totalitem + ($item['qty_canceled']);
$itemcount = $order->getQtyOrdered();
if ($itemcount == ($totalitem))
{
$order->setState("canceled");
$order->save();
}
}
}
}
Finally, I have Solved the issue for the above code
$_orderCollectionFactory = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
$collection = $_orderCollectionFactory->create()
->addFieldToSelect('*')
->addFieldToFilter('status', ['neq' => 'cancelled']);
foreach ($collection as $order)
{
$items = $order->getAllVisibleItems();
$totalitem=0;
foreach ($items as $item) {
// echo '<pre>';print_r(get_class_methods($item));
// die();
$totalitem = $totalitem + ($item['qty_canceled']);
$itemcount = $order->getQtyOrdered();
if ($itemcount == ($totalitem))
{
echo $order->getIncrementId();
$order->setState("canceled");
$order->setStatus("canceled");
$order->save();
}
}
}

Magento - update cart item by item ID

How to update cart item using item ID ? I have searched a lot but could not success, here is my codes -
require_once '../app/Mage.php';
Mage::app('default');
$qty = $_REQUEST['quantity'];
$item_id = (int) $_REQUEST['item_id'];
$cart = Mage::getSingleton('checkout/cart');
$quoteItem = $cart->getQuote()->getItemById($item_id);
/*$quoteItem = Mage::getModel('sales/quote_item')->getCollection()
->addFieldToFilter('item_id', array('in' => array($item_id)));*/
print_r($quoteItem);
if (!$quoteItem) {
Mage::throwException('Quote item is not found.');
}
if ($qty == 0) {
$cart->removeItem($id);
} else {
$quoteItem->setQty($qty)->save();
}
$cart->save();
I am implementing it in API , however same code is working fine in website.Please help!!!
I used the below code to update cart items and I am using observer to do that. can you please try this.
$quote = $observer->getEvent()->getQuote();
var_dump($quote->getId());
$quote = Mage::getSingleton('checkout/session')->getQuote();
//var_dump($quote->getId());
$cartItems = $quote->getAllItems();
//$storeId = Mage::app()->getStore()->getId();
foreach ($cartItems as $item) {
//echo $item->getSku();
$item->set__($value); // desired quote field from database
$item->save();
}
$quote->save();

Codeigniter how to Subtract the quantity of damaged item from product in stock quantity

Hello please help with this code... I don't know what I am doing wrong....anyways I am new to codeigniter
I am trying to less the quantity of a product instock everytime the product is entered into the damaged-products table
Please check the code i have, it works adding the damaged product but the quantity in the product table remain unchanged.... Help please
public function addDamaged($data = array()) {
if($this->db->insert('damaged', $data)) {
$dam_id = $this->db->insert_id();
foreach ($data as $da) {
$da['dam_id'] = $dam_id;
$product = $this->site->getProductByID($id);
$this->db->update('products',
array('quantity' => ($product->quantity - $da['dam_qty'])),
array('id' => $product->id)
);
return $dam_id;
}
return false;
}
}
public function addDamaged($data = array()) {
if($this->db->insert('damaged', $data)) {
$product = $this->site->getProductByID($data['product_id']);
$this->db->update('products',
array('quantity' => ($product->quantity - $da['dam_qty'])),
array('id' => $product->id)
);
return $dam_id;
}
return false;
}
}

Add exactly 1 product to cart programmatically in Magento

Using Magento 1.8.1, on the checkout page, I'm trying to add a product to the cart in the code. Here is the code I'm using:
$totals = Mage::getSingleton('checkout/cart')->getQuote()->getTotals();
$subtotal = $totals["subtotal"]->getValue();
$free_samples_prod_id = 1285;
if ( $subtotal >= 50 ) {
$this->addProduct($free_samples_prod_id, 1);
}
else{
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $free_samples_prod_id) {
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
break;
}
}
}
The code is in: app\code\core\Mage\Checkout\Model\Cart.php under public function init()
The product does get added sucessfully, however, everytime somebody visits their cart page, the quantity increases by one. How can I modify that code so the quantity is always 1?
Thank you
Axel makes a very good point, but to answer your immediate question, why not test for the product's presence before you add it
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
$subtotal = $totals["subtotal"]->getValue();
$free_samples_prod_id = 1285;
if ( $subtotal >= 50 ) {
$alreadyAdded = false;
foreach ($items as $item) {
if($item->getId() == $free_samples_prod_id) { $alreadyAdded = true; break; }
}
if(!$alreadyAdded) { $this->addProduct($free_samples_prod_id, 1); }
}

How to update the value of array in Yii session

I am using "ajaxSubmitButton" to send some values of 3 fields: registrant, product id and quantity to controller (controller name: actionCart). After submitting the button I receive those values in session array what I did successfully. At this point, if I submit same product id but different quantity, I want to update the quantity key with new value. I have done this by php global $_SESSION but can't using Yii session variable.
public function actionCart()
{
if(isset($_POST["Order"])){
$item = $_POST["Order"];
$registration_id = $item["registration_id"];
$this->productId = $item["item"];
$quantity = $item["quantity"];
$quantity = $item["quantity"]=='' ? 1 : $item["quantity"];
$productInfo = Products::model()->findByPk(array('id'=>$this->productId));
$totalPrice = $productInfo->price * $quantity;
$newItem = array("product_id" => $this->productId , "product_name" => $productInfo->name, "quantity" => $quantity,"price" => $productInfo->price,"totalPrice" => $totalPrice);
$session = Yii::app()->session;
$cartArray = $session['cart'];
$searchArrResult = $this->searchSubArray($session['cart'],'product_id',$this->productId);
if (!empty($searchArrResult)) {
/***** this works *****/
foreach ( $_SESSION['cart'] as $key=>$cart ) {
if ( $cart["product_id"] == $this->productId ) {
$_SESSION['cart'][$key]['quantity']=$quantity;
$_SESSION['cart'][$key]['totalPrice']=$totalPrice;
}
}
/***** following commented code does not work *****
*
foreach($session['cart'] as $key=>$cart){
if ($cart["product_id"] == $this->productId){
$session['cart'][$key]['quantity'] == $quantity;
$session['cart'][$key]['totalPrice'] == $totalPrice;
}
}*/
}
else {
$cartArray[] = $newItem;
$session['cart'] = $cartArray;
}
print_r($session['cart']);
//unset(Yii::app()->session['cart']);
}
}
In the above code I marked by commenting where I want to update session values. Please help me someone if it is possible do in yii.
Try this:
$carts = $session['cart'];
foreach($carts as $key=>&$cart){
if ($cart["product_id"] == $this->productId){
$cart['quantity'] == $quantity;
$cart['totalPrice'] == $totalPrice;
}
}
$session['cart'] = $carts;
Yii::app()->session return object of CHttpSession, not reference to $_SESSION.
$carts = $session['cart'] equals operation $carts = $session->get('cart'); (by means magic method __get in CHttpSession) and $session['cart'] = $carts; equals to $session->set('cart', $carts); (by __set)
That's why you can't setting by $session['cart'][$key]['quantity'] = $quantity;
UPDATED full solution (I change logic of saving products - $key = product_id)
public function actionCart()
{
if(isset($_POST["Order"])){
$item = $_POST["Order"];
$registration_id = $item["registration_id"];
$this->productId = $item["item"];
$quantity = empty(intval($item["quantity"])) ? 1 : intval($item["quantity"]);
$productInfo = Products::model()->findByPk(array('id'=>$this->productId));.
if(empty($productInfo))
return false; // or other action
$newItem = array(
"product_id" => $this->productId ,
"product_name" => $productInfo->name,
"quantity" => $quantity,
"price" => $productInfo->price,
"totalPrice" => ($productInfo->price * $quantity)
);
$cartArray = Yii::app()->session['cart'];
$cartArray[$this->productId] = $newItem;
Yii::app()->session['cart'] = $cartArray;
}
}

Categories