function updateQuantity() { global $order; if (expJavascript::inAjaxAction()) { $id = str_replace('quantity-', '', $this->params['id']); $item = new orderitem($id); if (!empty($item->id)) { //$newqty = $item->product->updateQuantity($this->params['value']); $newqty = $item->product->updateQuantity($this->params['value']); if ($newqty > $item->product->quantity) { if ($item->product->availability_type == 1) { $diff = $item->product->quantity <= 0 ? $newqty : $newqty - $item->product->quantity; $updates->message = 'Only ' . $item->product->quantity . ' ' . $item->products_name . ' are currently in stock. Shipping may be delayed on the other ' . $diff; } elseif ($item->product->availability_type == 2) { $updates->message = $item->products_name . ' only has ' . $item->product->quantity . ' on hand. You can not add any more than that to your cart.'; $updates->cart_total = '$' . number_format($order->getCartTotal(), 2); $updates->item_total = '$' . number_format($item->getTotal(), 2); $updates->item_id = $id; $updates->quantity = $item->product->quantity; echo json_encode($updates); return true; } } $item->quantity = $newqty; $item->save(); $order->refresh(); $updates->cart_total = '$' . number_format($order->getCartTotal(), 2); $updates->item_total = '$' . number_format($item->getTotal(), 2); $updates->item_id = $id; $updates->quantity = $item->quantity; echo json_encode($updates); } } else { if (!is_numeric($this->params['quantity'])) { flash('error', gt('Please enter a valid quantity.')); expHistory::back(); } $item = new orderitem($this->params['id']); if (!empty($item->id)) { //$newqty = $item->product->updateQuantity($this->params['quantity']); $newqty = $this->params['quantity']; //$oiObj = new orderitem(); //$oi = $oiObj->find('all','product_id='.$item->product->id); $qCheck = 0; //$item->product->quantity; //if (!empty($oi)) //{ foreach ($order->orderitem as $orderItem) { if ($orderItem->product_id == $item->product_id) { $qCheck += $orderItem->quantity; } } //eDebug("Done",true); //} /*eDebug($item->quantity); eDebug($item->product->quantity); eDebug($qCheck); eDebug($newqty,true); */ //check minimum quantity $qtyMessage = ''; if ($newqty < $item->product->minimum_order_quantity) { $qtyMessage = $item->product->title . ' has a minimum order quantity of ' . $item->product->minimum_order_quantity . '. The quantity has been adjusted and added to your cart.<br/><br/>'; $newqty = $item->product->minimum_order_quantity; } $itemMessage = ''; if ($qCheck + ($newqty - $item->quantity) > $item->product->quantity) { if ($item->product->availability_type == 1) { $diff = $item->product->quantity <= 0 ? $newqty : $newqty - $item->product->quantity; $itemMessage = gt('Only') . ' ' . $item->product->quantity . ' ' . $item->products_name . ' ' . gt('are currently in stock. Shipping may be delayed on the other') . ' ' . $diff . "<br/><br/>"; //$updates->message = 'Only '.$item->product->quantity.' '.$item->products_name.' are currently in stock. Shipping may be delayed on the other '.$diff; } elseif ($item->product->availability_type == 2) { flash('error', $item->products_name . ' ' . gt('only has') . ' ' . $item->product->quantity . ' ' . gt('on hand. You can not add any more than that to your cart.')); /*$updates->message = $item->products_name.' only has '.$item->product->quantity.' on hand. You can not add any more to your cart.'; $updates->cart_total = '$'.number_format($order->getCartTotal(), 2); $updates->item_total = '$'.number_format($item->quantity*$item->products_price, 2); $updates->item_id = $id; $updates->quantity = $item->product->quantity; echo json_encode($updates); */ expHistory::back(); } } else { if ($newqty <= 0) { $item->delete(); flash('message', $item->products_name . ' ' . gt('has been removed from your cart.')); expHistory::back(); } } $item->quantity = $newqty; $item->save(); $order->refresh(); /*$updates->cart_total = '$'.number_format($order->getCartTotal(), 2); $updates->item_total = '$'.number_format($item->quantity*$item->products_price, 2); $updates->item_id = $id; $updates->quantity = $item->quantity; */ //echo json_encode($updates); } //redirect_to(array('controller'=>'cart','action'=>'show')); flash('message', $qtyMessage . $itemMessage . $item->products_name . ' ' . gt('quantity has been updated.')); expHistory::back(); } }
function delete_order_item() { $order = new order($this->params['orderid']); if (count($order->orderitem) <= 1) { flash('error', gt('You may not delete the only item on an order. Please edit this item, or add another item before removing this one.')); expHistory::back(); } $oi = new orderitem($this->params['id']); $oi->delete(); $s = array_pop($order->shippingmethods); $sm = new shippingmethod($s->id); $shippingCalc = new shippingcalculator($sm->shippingcalculator_id); $calcName = $shippingCalc->calculator_name; $calculator = new $calcName($shippingCalc->id); $pricelist = $calculator->getRates($order); foreach ($pricelist as $rate) { if ($rate['id'] == $sm->option) { $sm->shipping_cost = $rate['cost']; } } $sm->save(); $order->refresh(); $order->calculateGrandTotal(); $order->save(); flashAndFlow('message', 'Order item removed and order totals updated.'); redirect_to(array('controller' => 'order', 'action' => 'show', 'id' => $this->params['orderid'])); }