Exemple #1
0
 /**
  * @throws CHttpException
  */
 public function actionAdd()
 {
     if (!Yii::app()->getRequest()->getIsPostRequest()) {
         throw new CHttpException(404);
     }
     $product = Yii::app()->getRequest()->getPost('Product');
     if (empty($product['id'])) {
         throw new CHttpException(404);
     }
     $model = CartProduct::model()->findByPk((int) $product['id']);
     if (null === $model) {
         throw new CHttpException(404);
     }
     $variantsId = Yii::app()->getRequest()->getPost('ProductVariant', []);
     $variants = [];
     foreach ((array) $variantsId as $var) {
         if (!$var) {
             continue;
         }
         $variant = ProductVariant::model()->findByPk($var);
         if ($variant && $variant->product_id == $model->id) {
             $variants[] = $variant;
         }
     }
     $model->selectedVariants = $variants;
     $quantity = empty($product['quantity']) ? 1 : (int) $product['quantity'];
     Yii::app()->cart->put($model, $quantity);
     Yii::app()->ajax->success(Yii::t("CartModule.cart", 'Product successfully added to your basket'));
 }
 public static function create(Product $oProduct)
 {
     $oCartProduct = new CartProduct();
     $oCartProduct->setImage($oProduct->getImage());
     $oCartProduct->setId($oProduct->getId());
     $oCartProduct->setDescription($oProduct->getDescription());
     $oCartProduct->setName($oProduct->getName());
     $oCartProduct->setPrice($oProduct->getPrice());
     return $oCartProduct;
 }
 public function checkout_step3()
 {
     // Processing Cart
     $cart_id = $this->cart_id;
     if (!$cart_id) {
         $cart_id = Input::get('cart_id');
     }
     $address_id = Input::get('address_id');
     $payment_id = Input::get('payment_id');
     $cart_products = DB::table('cart_products')->where('cart_id', $cart_id)->leftJoin('products', 'cart_products.product_id', '=', 'products.id')->leftJoin('users', 'cart_products.added_by', '=', 'users.id')->select('products.*', 'cart_products.quantity as cart_quantity', 'users.first_name as added_by')->orderBy('store_id')->get();
     $cart_users = DB::table('cart_users')->leftJoin('users', 'cart_users.user_id', '=', 'users.id')->select('users.*')->get();
     $response_array['cart'] = Cart::find($cart_id)->toArray();
     $response_array['cart']['total_amount'] = 0;
     $response_array['cart']['total_quantity'] = 0;
     $response_array['cart']['users'] = array();
     $response_array['cart']['stores'] = array();
     $store_id = 0;
     if ($cart_products) {
         $response_array['cart']['is_minimum_reached'] = 1;
         foreach ($cart_products as $cart_product) {
             if ($store_id != $cart_product->store_id) {
                 $store_id = $cart_product->store_id;
                 // Retriving Store
                 $store = Store::find($store_id)->toArray();
                 // Retriving Delivery Slot
                 $time = date("Y-m-d H:i:s");
                 $slot = Slot::where('store_id', $store_id)->where('start_time', '>', $time)->where('is_available', 1)->orderBy('start_time')->first();
                 if ($slot) {
                     $next_slot_start = date("D, jS M, H a", strtotime($slot->start_time));
                     $next_slot_end = date("h a", strtotime($slot->end_time));
                     $next_slot = $next_slot_start . " - " . $next_slot_end;
                 } else {
                     $next_slot = "No Slots available";
                 }
                 $store['slot'] = $next_slot;
                 $store['count'] = 1;
                 $store['sub_total'] = 0;
                 $response_array['cart']['stores'][$store_id] = $store;
                 $response_array['cart']['stores'][$store_id]['products'] = array();
             } else {
                 $response_array['cart']['stores'][$store_id]['count'] += 1;
             }
             $product = json_decode(json_encode($cart_product), true);
             $product['sub_total'] = $product['cart_quantity'] * $product['price'];
             $response_array['cart']['stores'][$store_id]['sub_total'] += $product['sub_total'];
             $response_array['cart']['total_amount'] += $product['sub_total'];
             $response_array['cart']['total_quantity']++;
             array_push($response_array['cart']['stores'][$store_id]['products'], $product);
         }
     }
     if ($cart_users) {
         foreach ($cart_users as $cart_user) {
             $product = json_decode(json_encode($cart_user), true);
             array_push($response_array['cart']['users'], $cart_user);
         }
     }
     foreach ($response_array['cart']['stores'] as $st) {
         if ($st['sub_total'] < $st['minimum_order_amount']) {
             $response_array['cart']['is_minimum_reached'] = 0;
         }
     }
     if (isset($response_array['cart']['is_minimum_reached']) && $response_array['cart']['is_minimum_reached'] == 1) {
         $order = new Order();
         $order->user_id = $this->user_id;
         $order->date = date("Y-m-d H:i:s");
         $order->total_products = $response_array['cart']['total_quantity'];
         $order->total_amount = $response_array['cart']['total_amount'];
         $order->payment_id = $payment_id;
         $order->address_id = $address_id;
         $payment = Payment::find($payment_id);
         $order->payment_customer_id = $payment->customer_id;
         $address = UserAddress::find($address_id);
         $order->address = $address->address . " " . $address->zipcode;
         $order->status = "Initiated";
         $order->save();
         foreach ($cart_products as $cart_product) {
             $order_product = new OrderProduct();
             $order_product->order_id = $order->id;
             $product = Product::find($cart_product->id);
             $product->total_sale += $cart_product->cart_quantity;
             $product->save();
             $order_product->price = $cart_product->price;
             $order_product->quantity = $cart_product->cart_quantity;
             $order_product->fulfilment_status = "Pending";
             $order_product->type = "Product";
             $order_product->parent_id = 0;
             $order_product->product_name = $product->name;
             $order_product->product_image_url = $product->image_url;
             $order_product->product_quantity = $product->quantity;
             $order_product->product_unit = $product->unit;
             $order_product->store_id = $product->store_id;
             $order_product->save();
         }
         // removing products from cart
         CartProduct::where('cart_id', $cart_id)->delete();
         // Order Placement Mail
         $user = User::find($this->user_id);
         Mail::send('emails.order_success', array('user' => $user, 'order' => $order), function ($message) use($user) {
             $message->to($user->email, $user->first_name)->subject('Thanks for placing the order!');
         });
         // Order Placement Mail to Admin
         $admin_email = Config::get('app.admin_email');
         Mail::send('emails.admin_order_success', array('user' => $user, 'order' => $order), function ($message) use($admin_email) {
             $message->to($admin_email, "Admin")->subject('New Order!');
         });
     } else {
         $message = "Minimum order amount not reached";
         if (Request::format() == 'html') {
             return View::make('checkout_step3')->with('store', $this->store)->with('departments', $this->departments)->with('zipcode', $this->zipcode)->with('city', $this->city)->with('stores1', $this->stores)->with('message', $message);
         } else {
             $response_array = array('success' => false, 'error_code' => 411, 'error' => $message);
             $response_code = 200;
             $response = Response::json($response_array, $response_code);
             return $response;
         }
     }
     if (Request::format() == 'html') {
         return View::make('checkout_step3')->with('store', $this->store)->with('departments', $this->departments)->with('zipcode', $this->zipcode)->with('city', $this->city)->with('stores1', $this->stores)->with('message', 'Thanks for placing your Order!!');
     } else {
         $response_array = array('success' => true);
         $response_code = 200;
         $response = Response::json($response_array, $response_code);
         return $response;
     }
 }
Exemple #4
0
 public static function getValidLinks()
 {
     $sql = 'select CONCAT(`manufacturers_id`, ":vendor") as `key`, CONCAT("Vendor: ", `manufacturers_name`) as value from cart_manufacturers order by `manufacturers_name` asc';
     $mans = Database::singleton()->query_fetch_all($sql);
     $sql = 'select CONCAT( `categories_id`, ":category") as `key`, CONCAT("Category: ", `categories_name`) as value from cart_categories_description order by `categories_name` asc';
     $cats = Database::singleton()->query_fetch_all($sql);
     $sql = 'select CONCAT( `products_id`, ":product") as `key`, CONCAT("Product: ", `products_name`) as value from cart_products_description order by `products_name` asc';
     $prods = Database::singleton()->query_fetch_all($sql);
     $pages = $cats;
     foreach ($mans as $man) {
         array_push($pages, $man);
     }
     include 'include/CartCategory.php';
     include 'include/CartManufacturer.php';
     include_once 'include/CartProduct.php';
     include_once 'include/CartProductType.php';
     include_once 'include/CartTaxClass.php';
     foreach ($cats as $cat) {
         $curcat = new CartCategory(rtrim($cat['key'], ':category'));
         foreach ($curcat->getCatManufacturers() as $man) {
             array_push($pages, array('key' => $curcat->getId() . '/' . $man->getId() . ':catvend', 'value' => 'Category & Vendor: ' . $curcat->getName() . ' / ' . $man->getName()));
         }
     }
     foreach ($prods as $prod) {
         $curprod = new CartProduct(rtrim($prod['key'], ':product'));
         array_push($pages, array('key' => $curprod->getId() . ':product', 'value' => 'Product: ' . $curprod->getName()));
     }
     array_unshift($pages, array('key' => 0, 'value' => 'Store (Top Level)'));
     return $pages;
 }
Exemple #5
0
    /**
     * Get an Add/Edit form for the object.
     *
     * @param string $target Post target for form submission
     */
    public function getAddEditForm($target = '/admin/Cart')
    {
        $form = new Form('CartProduct_addedit', 'post', $target, '', array('class' => 'admin'));
        $form->setConstants(array('section' => 'products'));
        $form->addElement('hidden', 'section');
        $form->setConstants(array('action' => 'addedit'));
        $form->addElement('hidden', 'action');
        if (!is_null($this->getId())) {
            $form->setConstants(array('cartproduct_products_id' => $this->getId()));
            $form->addElement('hidden', 'cartproduct_products_id');
            $defaultValues['cartproduct_type'] = $this->getType()->getId();
            $defaultValues['cartproduct_quantity'] = $this->getQuantity();
            $defaultValues['cartproduct_pallet_count'] = $this->getPalletCount();
            //$defaultValues ['cartproduct_model'] = $this->getModel();
            $defaultValues['cartproduct_image'] = $this->getImage()->getId();
            $defaultValues['cartproduct_price'] = $this->getPrice();
            //$defaultValues ['cartproduct_virtual'] = $this->getVirtual();
            $defaultValues['cartproduct_date_added'] = $this->getDate_added();
            $defaultValues['cartproduct_lastModified'] = $this->getLastModified();
            $defaultValues['cartproduct_dateAvailable'] = $this->getDateAvailable();
            $defaultValues['cartproduct_weight'] = $this->getWeight();
            $defaultValues['cartproduct_weight_unit'] = $this->getWeightUnit();
            $defaultValues['cartproduct_status'] = $this->getStatus();
            $defaultValues['cartproduct_taxClass'] = $this->getTaxClass()->getId();
            $defaultValues['cartproduct_manufacturer'] = $this->getManufacturer()->getId();
            $defaultValues['cartproduct_ordered'] = $this->getOrdered();
            $defaultValues['cartproduct_orderMin'] = $this->getOrderMin();
            $defaultValues['cartproduct_orderUnits'] = $this->getOrderUnits();
            $defaultValues['cartproduct_pricedByAttribute'] = $this->getPricedByAttribute();
            $defaultValues['cartproduct_isFree'] = $this->getIsFree();
            $defaultValues['cartproduct_isCall'] = $this->getIsCall();
            $defaultValues['cartproduct_quantityMixed'] = $this->getQuantityMixed();
            $defaultValues['cartproduct_isAlwaysFreeShipping'] = $this->getIsAlwaysFreeShipping();
            $defaultValues['cartproduct_qtyBoxStatus'] = $this->getQtyBoxStatus();
            $defaultValues['cartproduct_qtyOrderMax'] = $this->getQtyOrderMax();
            $defaultValues['cartproduct_sortOrder'] = $this->getSortOrder();
            $defaultValues['cartproduct_discountType'] = $this->getDiscountType();
            $defaultValues['cartproduct_discountTypeFrom'] = $this->getDiscountTypeFrom();
            $defaultValues['cartproduct_priceSorter'] = $this->getPriceSorter();
            $defaultValues['cartproduct_category'] = $this->getCategory()->getId();
            $defaultValues['cartproduct_mixedDiscountQty'] = $this->getMixedDiscountQty();
            $defaultValues['cartproduct_titleStatus'] = $this->getTitleStatus();
            $defaultValues['cartproduct_nameStatus'] = $this->getNameStatus();
            $defaultValues['cartproduct_modelStatus'] = $this->getModelStatus();
            $defaultValues['cartproduct_priceStatus'] = $this->getPriceStatus();
            $defaultValues['cartproduct_taglineStatus'] = $this->getTaglineStatus();
            $defaultValues['cartproduct_name'] = $this->getName();
            $defaultValues['cartproduct_description'] = $this->getDescription();
            //$defaultValues ['cartproduct_url'] = $this->getUrl();
            //$defaultValues ['cartproduct_accessoryof'] = $this->getAccessoryOf()->getId();
        } else {
            /*******************************************************/
            //The following few lines assign the default values to the elements.
            //It is very important especially for adjusting the layout
            $defaultValues['cartproduct_date_added'] = "&nbsp;";
            $defaultValues['cartproduct_lastModified'] = "&nbsp;";
            //$defaultValues ['cartproduct_virtual'] = 0;
            /*******************************************************/
        }
        $form->setDefaults($defaultValues);
        $form->addElement('header', 'product_details', 'Product Details');
        $form->addElement('text', 'cartproduct_name', 'Name');
        $desc = $form->addElement('tinymce', 'cartproduct_description', 'Description');
        //$form->addElement('text', 'cartproduct_url', 'Link URL');
        /*
        $products = CartProduct::toArray();
        $products[0] = "Not an Accessory";
        $acc = $form->addElement('select', 'cartproduct_accessoryof', 'Accessory Of', $products);
        $acc->setMultiple(true);
        
        $accs = array();
        
        foreach ($this->getAccessoryOf() as $p) {
        	$accs[] = $p->getId();
        }
        
        $acc->setSelected($accs);
        */
        $form->addElement('select', 'cartproduct_manufacturer', 'Supplier', CartManufacturer::toArray());
        $form->addElement('select', 'cartproduct_category', 'Category', CartCategory::toArray());
        require_once 'CartProductType.php';
        $form->addElement('select', 'cartproduct_type', 'Product Type', @CartProductType::toArray());
        $form->addElement('text', 'cartproduct_quantity', 'Stock Qty');
        $form->addElement('text', 'cartproduct_pallet_count', 'Pallet Count');
        //$form->addElement('text', 'cartproduct_model', 'Model #');
        $newImage = $form->addElement('file', 'cartproduct_image_upload', 'Product Image');
        if ($this->getImage()) {
            $curImage = $form->addElement('dbimage', 'cartproduct_image', $this->getImage()->getId());
        }
        $form->addElement('text', 'cartproduct_price', 'Price ($)');
        //$form->addElement('select', 'cartproduct_virtual', 'Virtual Product', Form::booleanArray());
        $form->addElement('static', 'cartproduct_date_added', 'Date Added');
        $form->addElement('static', 'cartproduct_lastModified', 'Last Modified');
        $form->addElement('text', 'cartproduct_dateAvailable', 'Date Availible');
        $form->addElement('text', 'cartproduct_weight', 'Weight per bag');
        $form->addElement('select', 'cartproduct_weight_unit', 'Weight Unit', CartProduct::getAvailableWeightUnits());
        $form->addElement('select', 'cartproduct_taxClass', 'Tax Class', CartTaxClass::toArray());
        //$form->addElement('text', 'cartproduct_ordered', 'ordered');
        $form->addElement('text', 'cartproduct_orderMin', 'Minimum Order Quantity');
        $form->addElement('select', 'cartproduct_status', 'Status', Form::statusArray());
        $form->addElement('header', 'alt_images', 'Alternate Product Images');
        foreach ($this->getAltImages() as $image) {
            $form->addElement('html', '
				<div style="float: right;" id="delete_altimage_div_' . $image['id'] . '">
					<input type="image" src="/images/admin/cross.gif" name="delete_altimage" onclick="return !deleteAltImage(' . $image['id'] . ');" />
				</div>
			');
            $form->addElement('dbimage', 'cartproduct_altimage_' . $image['id'], $image['image_id']);
        }
        $newAltImage = $form->addElement('file', 'cartproduct_altimage_upload', 'New Alternate Product Image');
        $options = $this->getOptions();
        foreach ($options as $option) {
            $form->addElement('header', 'product_options_' . $option->getId(), 'Product Options: ' . $option->getOptionsId()->getName());
            $form->addElement('static', 'value_' . $option->getId(), $option->getOptionsId()->getName(), $option->getValue()->getName());
            $form->addElement('text', 'attprice[' . $option->getId() . ']', 'Additional Price');
            $form->addElement('text', 'inventory[' . $option->getId() . ']', 'Inventory');
            $form->addElement('html', '
				<div style="float: right;">
					<input type="image" src="/images/admin/cross.gif" name="delete_att" onclick="return !deleteAtt(' . $option->getId() . ');" />
				</div>
			');
            $defaultValues['attprice[' . $option->getId() . ']'] = $option->getValuesPrice();
            $defaultValues['inventory[' . $option->getId() . ']'] = $option->getInventory();
        }
        $form->addElement('header', 'new_product_options', 'New Product Option');
        $form->addElement('select', 'newoption', 'Option', CartProductOption::toArray(), array('onclick' => 'getValues(this);'));
        $form->addElement('select', 'newvalue', 'Value', array());
        $form->addElement('text', 'newattprice', 'Additional Price');
        $form->addElement('text', 'newinventory', 'Inventory');
        $form->setDefaults($defaultValues);
        if (isset($_REQUEST['cartproduct_submit']) && $form->validate() && $form->isSubmitted()) {
            $this->setName($form->exportValue('cartproduct_name'));
            $this->setDescription($form->exportValue('cartproduct_description'));
            //$this->setUrl($form->exportValue('cartproduct_url'));
            $this->setType($form->exportValue('cartproduct_type'));
            $this->setQuantity($form->exportValue('cartproduct_quantity'));
            $this->setPalletCount($form->exportValue('cartproduct_pallet_count'));
            //$this->setModel($form->exportValue('cartproduct_model'));
            $this->setImage($form->exportValue('cartproduct_image_upload'));
            $this->setPrice($form->exportValue('cartproduct_price'));
            //$this->setVirtual($form->exportValue('cartproduct_virtual'));
            $this->setDate_added($form->exportValue('cartproduct_date_added'));
            $this->setLastModified(date('Y-m-d H:i:s'));
            $this->setDateAvailable($form->exportValue('cartproduct_dateAvailable'));
            $this->setWeight($form->exportValue('cartproduct_weight'));
            $this->setWeightUnit($form->exportValue('cartproduct_weight_unit'));
            $this->setStatus($form->exportValue('cartproduct_status'));
            $this->setTaxClass($form->exportValue('cartproduct_taxClass'));
            $this->setManufacturer($form->exportValue('cartproduct_manufacturer'));
            //$this->setAccessoryOf($form->exportValue('cartproduct_accessoryof'));
            //$this->setOrdered($form->exportValue('cartproduct_ordered'));
            if ($form->exportValue('cartproduct_orderMin') <= 0) {
                $this->setOrderMin(1);
            } else {
                $this->setOrderMin($form->exportValue('cartproduct_orderMin'));
            }
            $this->setCategory($form->exportValue('cartproduct_category'));
            /*
            $this->setOrderUnits($form->exportValue('cartproduct_orderUnits'));
            $this->setPricedByAttribute($form->exportValue('cartproduct_pricedByAttribute'));
            $this->setIsFree($form->exportValue('cartproduct_isFree'));
            $this->setIsCall($form->exportValue('cartproduct_isCall'));
            $this->setQuantityMixed($form->exportValue('cartproduct_quantityMixed'));
            $this->setIsAlwaysFreeShipping($form->exportValue('cartproduct_isAlwaysFreeShipping'));
            $this->setQtyBoxStatus($form->exportValue('cartproduct_qtyBoxStatus'));
            $this->setQtyOrderMax($form->exportValue('cartproduct_qtyOrderMax'));
            $this->setSortOrder($form->exportValue('cartproduct_sortOrder'));
            $this->setDiscountType($form->exportValue('cartproduct_discountType'));
            $this->setDiscountTypeFrom($form->exportValue('cartproduct_discountTypeFrom'));
            $this->setPriceSorter($form->exportValue('cartproduct_priceSorter'));
            $this->setMixedDiscountQty($form->exportValue('cartproduct_mixedDiscountQty'));
            $this->setTitleStatus($form->exportValue('cartproduct_titleStatus'));
            $this->setNameStatus($form->exportValue('cartproduct_nameStatus'));
            $this->setModelStatus($form->exportValue('cartproduct_modelStatus'));
            $this->setPriceStatus($form->exportValue('cartproduct_priceStatus'));
            $this->setTaglineStatus($form->exportValue('cartproduct_taglineStatus'));
            */
            if ($newImage->isUploadedFile()) {
                $im = new Image();
                $id = $im->insert($newImage->getValue());
                $this->setImage($id);
                //$curImage->setSource($this->getImage()->getId());
            }
            $this->save();
            if ($newAltImage->isUploadedFile()) {
                $im = new Image();
                $id = $im->insert($newAltImage->getValue());
                $sql = 'insert into cart_products_images set product_id=' . $this->getId() . ', image_id=' . $id;
                Database::singleton()->query($sql);
            }
            if (is_array(@$_REQUEST['attprice'])) {
                foreach (@$_REQUEST['attprice'] as $key => $value) {
                    $att = new CartProductAttribute($key);
                    $att->setValuesPrice($value);
                    $att->setInventory($_REQUEST['inventory'][$key]);
                    $att->save();
                }
            }
            if (isset($_REQUEST['newvalue']) && isset($_REQUEST['newoption']) && isset($_REQUEST['newattprice'])) {
                $a = new CartProductAttribute();
                $a->setProductid($this->getId());
                $a->setOptionsId($_REQUEST['newoption']);
                $a->setValue($_REQUEST['newvalue']);
                $a->setValuesPrice($_REQUEST['newattprice']);
                $a->setInventory($_REQUEST['newinventory']);
                $a->save();
            }
        }
        $form->addElement('header', 'product_submit', 'Save Product');
        $form->addElement('submit', 'cartproduct_submit', 'Submit');
        return $form;
    }
Exemple #6
0
 /**
  * @param null $n number of products to return
  * @param false $onlyAvailables wether or not to return only available products
  *
  * @return mixed Cart products
  * @throws Exception
  */
 public function getProducts($n = null, $onlyAvailables = false, $ignoreCache = false)
 {
     if (!$this->cart_srl) {
         throw new ShopException('Cart is not persisted');
     }
     //an entity-unique cache key for the current method and parameters combination
     $cacheKey = 'getProducts|' . ($n ? $n : "_") . (string) ($onlyAvailables ? 'av' : 'all');
     if ($ignoreCache || !($products = self::$cache[$cacheKey])) {
         $products = array();
         $shopInfo = new ShopInfo($this->module_srl);
         $checkIfInStock = $shopInfo->getOutOfStockProducts() == 'Y';
         $params = array('cart_srl' => $this->cart_srl);
         if ($n) {
             $params['list_count'] = $n;
         }
         $output = $this->query('getCartAllProducts', $params, true);
         $stds = $output->data;
         foreach ($stds as $i => $data) {
             $cartProduct = new CartProduct($data);
             $simpleProduct = $cartProduct->getProduct();
             if ($simpleProduct->isPersisted()) {
                 if (!$simpleProduct->isAvailable($checkIfInStock) && $onlyAvailables) {
                     continue;
                 }
             } else {
                 if ($onlyAvailables || !$cartProduct->cart_product_srl) {
                     continue;
                 }
             }
             $products[$i] = $cartProduct;
         }
         self::$cache[$cacheKey] = $products;
     }
     //if limit did not work:
     if ($n && $n < count($products)) {
         return array_slice($products, 0, $n);
     }
     return $products;
 }