public static function rpc_change(Context $ctx) { if (null === ($id = $ctx->get('id'))) { throw new RuntimeException(t('Не указан id товара.')); } $cart = new Cart($ctx); $items = $cart->getItems(); $items[$id] = $ctx->get('qty', 1); $cart->setItems($items); return $ctx->getRedirect(); }
public function testAddItem() { $item = \App\Models\Commodity::find(1); $item2 = \App\Models\Commodity::find(2); Cart::addItem($item); $this->assertEquals(Cart::getItems()->count(), 1); $this->assertEquals(Cart::getTotalCost(), $item->getPrice()); Cart::removeItemsById($item->getIdentifer()); $this->assertEquals(Cart::getItems()->count(), 0); $this->assertEquals(Cart::getTotalCost(), 0); Cart::addItem($item); Cart::addItem($item2); Cart::addItem($item); $this->assertEquals(Cart::getItems()->count(), 2); $this->assertEquals(Cart::getTotalCost(), $item->getPrice() * 2 + $item2->getPrice()); Cart::removeItems(['name' => $item->getName()]); $this->assertEquals(Cart::getItems()->count(), 1); $this->assertEquals(Cart::getTotalCost(), $item2->getPrice()); }
public function save() { if ($isnew = !$this->id) { $cart = new Cart($ctx = Context::last()); $this->orderdetails = $cart->getItems(); } elseif (array_key_exists('orderdetails', (array) $this->olddata)) { $this->orderdetails = $this->olddata['orderdetails']; } if (empty($this->orderdetails)) { throw new ForbiddenException(t('Не удалось оформить заказ: ' . 'ваша корзина пуста. Возможно, вы его уже оформили?')); } $res = parent::save(); if ($isnew) { $this->sendEmail($this->email, 'invoice'); $this->sendEmail($ctx->config->get('modules/cart/email'), 'notification'); $cart->setItems(array()); } return $res; }
function isItemInCart($accID) { $items = Cart::getItems(); return isset($items[$accID]); }
public function update(Cart $cart) { echo var_export($cart->getItems(), true) . PHP_EOL; }
/** * @return List<Product> */ public function fetchItems() { return $this->cart->getItems(); }
/** * Checkout */ public function checkout() { $output = ''; if ($_SERVER['REQUEST_METHOD'] != 'POST') { $output = $this->_checkout_step_1(); } else { //valid helper include 'classes/valid.class.php'; $valid = new Valid(); //validation class include 'classes/validation.class.php'; $step = $_POST['step']; if ($step == '1') { //step 1 validation $post = new Validation($_POST['order']); $post->add_rules('first_name', 'required'); $post->add_rules('last_name', 'required'); $post->add_rules('company', 'required'); $post->add_rules('address', 'required'); $post->add_rules('city', 'required'); $post->add_rules('state', 'required'); $post->add_rules('country', 'required'); $post->add_rules('zip', 'required'); $post->add_rules('phone', 'required', array($valid, 'phone')); $post->add_rules('email', 'required', array($valid, 'email')); if (!isset($_POST['billing_is_shipping'])) { $post->add_rules('ship_first_name', 'required'); $post->add_rules('ship_last_name', 'required'); $post->add_rules('ship_company', 'required'); $post->add_rules('ship_address', 'required'); $post->add_rules('ship_city', 'required'); $post->add_rules('ship_state', 'required'); $post->add_rules('ship_country', 'required'); $post->add_rules('ship_zip', 'required'); $post->add_rules('ship_phone', 'required', array($valid, 'phone')); } $post->pre_filter('trim'); //success, go to step 2 if ($post->validate()) { //save order data $_SESSION['order'] = $_POST['order']; $output = $this->_checkout_step_2(); } else { $errors = $post->errors(); $output = $this->_checkout_step_1($_POST, $errors); } } elseif ($step == '2') { //step 2 validation $post = new Validation($_POST['order']); $post->add_rules('cc_name', 'required'); $post->add_rules('cc_type', 'required'); $post->add_rules('cc_number', 'required', array($valid, 'credit_card')); $post->add_rules('cc_cvv', 'required', 'length[3,4]', array($valid, 'digit')); $post->add_rules('cc_exp_month', 'required'); $post->add_rules('cc_exp_year', 'required'); if (isset($_POST['order']['cc_exp_month']) && isset($_POST['order']['cc_exp_year'])) { $post->add_callbacks('cc_exp_year', array($this, '_validate_cc_exp_date')); } $post->pre_filter('trim'); if ($post->validate()) { $cart = new Cart('shopping_cart'); //order data array $order_arr = array_merge($_SESSION['order'], $_POST['order']); $full_cc_number = $order_arr['cc_number']; $order_arr['cc_number'] = substr($order_arr['cc_number'], -4); $order_arr['promo_discount'] = $cart->getDiscount($order_arr['promo_code']); $order_arr['subtotal'] = $cart->getTotal(); $order_arr['tax'] = $cart->getTax(); //process payment include 'merchants/firstdata.class.php'; $merchant = new FirstData(); //billing info $merchant->name = $order_arr['first_name'] . ' ' . $order_arr['last_name']; $merchant->company = $order_arr['company']; $merchant->address = $order_arr['address']; $merchant->address2 = $order_arr['address2']; $merchant->city = $order_arr['city']; $merchant->state = $order_arr['state']; $merchant->country = $order_arr['country']; $merchant->phone = $order_arr['phone']; $merchant->fax = $order_arr['fax']; $merchant->email = $order_arr['email']; $merchant->zip = $order_arr['zip']; //shipping info $merchant->ship_name = $order_arr['ship_first_name'] . ' ' . $order_arr['ship_last_name']; $merchant->ship_address = $order_arr['ship_address']; $merchant->ship_saddress2 = $order_arr['ship_address2']; $merchant->ship_city = $order_arr['ship_city']; $merchant->ship_state = $order_arr['ship_state']; $merchant->ship_country = $order_arr['ship_country']; $merchant->ship_zip = $order_arr['ship_zip']; //payment info $merchant->cc_number = $full_cc_number; $merchant->cc_exp_month = $order_arr['cc_exp_month']; $merchant->cc_exp_year = substr($order_arr['cc_exp_year'], -2); $merchant->cc_cvv = $order_arr['cc_cvv']; $merchant->subtotal = $order_arr['subtotal']; $merchant->shipping = 0; $merchant->tax = $order_arr['tax']; $merchant->total = $order_arr['subtotal'] + $order_arr['tax'] - $order_arr['promo_discount']; // set to GOOD for test or LIVE $merchant->result = 'LIVE'; $merchant_success = false; $result = $merchant->sale(); if ($result['r_approved'] == "APPROVED") { $merchant_success = true; } //merchant error if (!$merchant_success) { $errors = $post->errors(); $this->set_flash($result['r_error'], 'error'); $output = $this->_checkout_step_2($_POST, $errors); } else { //save order to database $record = Record::insert('ecommerce_order', $order_arr); $order_id = Record::lastInsertId(); //save order items to database foreach ($cart->getItems() as $variant_id => $quantity) { //get variant data $variant = Record::findByIdFrom('ProductVariant', $variant_id); $variant->order_id = $order_id; $variant->quantity = $quantity; $variant_arr = (array) $variant; //remove unneeded fields unset($variant_arr['id']); unset($variant_arr['created_on']); unset($variant_arr['updated_on']); unset($variant_arr['position']); //insert $record = Record::insert('ecommerce_order_variant', $variant_arr); } //save log $this->_insert_log('Order <a href="' . get_url('plugin/ecommerce/order_show/' . $order_id) . '">' . $order_id . '</a> was placed.'); //send emails to client and buyer $this->_send_order_email('*****@*****.**', $order_id, $order_arr, $variant_arr); $this->_send_order_email($order_arr['email'], $order_id, $order_arr, $variant_arr); //success $this->set_flash('Thank you for your order. You will receive a confirmation email shortly.', 'success'); //clear cart and order session unset($_SESSION['order']); unset($_SESSION['Cart']); } } else { $errors = $post->errors(); $output = $this->_checkout_step_2($_POST, $errors); } } } return $output; }
public function update(Cart $cart) { echo '<pre>'; var_dump($cart->getItems()); echo '</pre>'; }