Example #1
0
    public function addToCart()
    {
        $this->onExecuteBefore('addToCart');
        $jinput = JFactory::getApplication()->input;
        $count = $jinput->get('count', 1);
        $price = $jinput->get('price', 0, 'float');
        $id = $jinput->get('id', 0, 'int');
        $prd = KSMProducts::getProduct($id);
        if ($this->order_id == 0) {
            $order_object = new stdClass();
            $order_object->user_id = $this->_user->id;
            $order_object->region_id = $this->_region_id;
            $order_object->shipping_id = $this->_shipping_id;
            $order_object->payment_id = $this->_payment_id;
            $order_object->status_id = 2;
            try {
                $result = $this->_db->insertObject('#__ksenmart_orders', $order_object);
                $this->order_id = $this->_db->insertid();
                $this->_session->set('shop_order_id', $this->order_id);
            } catch (Exception $e) {
            }
        }
        if (count($prd) > 0 && ($count <= $prd->in_stock || $this->params->get('use_stock', 1) == 0)) {
            if ($price == 0) {
                $price = $prd->val_price_wou;
            }
            if ($prd->type == 'set') {
                $related = KSMProducts::getSetRelated($id);
                $properties = $this->getProperties();
                $item_properties = array();
                foreach ($properties as $property) {
                    foreach ($related as $r) {
                        $value = JRequest::getVar('property_' . $r->relative_id . '_' . $property->id, '');
                        if (!empty($value)) {
                            $item_properties[$property->id] = array('value_id' => $value);
                        }
                    }
                }
            } else {
                $properties = $this->getProperties();
                $item_properties = array();
                foreach ($properties as $property) {
                    $value = JRequest::getVar('property_' . $id . '_' . $property->id, '');
                    if (!empty($value)) {
                        $item_properties[$property->id] = array('value_id' => $value);
                    }
                }
            }
            $query = $this->_db->getQuery(true);
            $query->select('
					p.id, 
					p.order_id,
					p.product_id,
					p.basic_price,
					p.price,
					p.count,
					p.properties
				')->from('#__ksenmart_order_items AS p')->where('order_id=' . $this->order_id)->where('product_id=' . $id)->where('properties=\'' . json_encode($item_properties) . '\'');
            $this->_db->setQuery($query);
            $item = $this->_db->loadObject();
            if (count($item) > 0) {
                $order_item_object = new stdClass();
                $order_item_object->id = $item->id;
                $order_item_object->count = $item->count + $count;
                $order_item_object->properties = json_encode($item_properties);
                try {
                    $result = $this->_db->updateObject('#__ksenmart_order_items', $order_item_object, 'id');
                } catch (Exception $e) {
                }
            } else {
                $order_item_object = new stdClass();
                $order_item_object->order_id = $this->order_id;
                $order_item_object->product_id = $prd->id;
                $order_item_object->price = $price;
                $order_item_object->count = $count;
                $order_item_object->properties = json_encode($item_properties);
                try {
                    $result = $this->_db->insertObject('#__ksenmart_order_items', $order_item_object);
                } catch (Exception $e) {
                }
            }
            if ($this->params->get('use_stock', 1) == 1) {
                $product_object = new stdClass();
                $product_object->id = $prd->id;
                $product_object->in_stock = $prd->in_stock - $count;
                try {
                    $result = $this->_db->updateObject('#__ksenmart_products', $product_object, 'id');
                } catch (Exception $e) {
                }
            }
            $cost = $price * $count;
            $order_object = new stdClass();
            $order_object->id = $this->order_id;
            $order_object->cost = $this->getOrderCost($this->order_id) + $cost;
            try {
                $result = $this->_db->updateObject('#__ksenmart_orders', $order_object, 'id');
            } catch (Exception $e) {
            }
            $this->onExecuteAfter('addToCart', array(&$result));
            return true;
        }
        return false;
    }
Example #2
0
 public function display($tpl = null)
 {
     $app = JFactory::getApplication();
     $document = JFactory::getDocument();
     $this->params = JComponentHelper::getParams('com_ksenmart');
     $path = $app->getPathway();
     $model = $this->getModel();
     $this->state = $this->get('State');
     $document->addScript(JURI::base() . 'components/com_ksenmart/js/highslide/highslide-with-gallery.js', 'text/javascript', true);
     $document->addScript(JURI::base() . 'components/com_ksenmart/js/highslide.js', 'text/javascript', true);
     $document->addScript(JURI::base() . 'components/com_ksenmart/js/slides.min.jquery.js', 'text/javascript', true);
     $document->addStyleSheet(JURI::base() . 'components/com_ksenmart/js/highslide/highslide.css');
     $document->addStyleSheet(JURI::base() . 'components/com_ksenmart/css/slides.css');
     if ($model->_id && $this->getLayout() != 'comment_congratulation') {
         $this->product = $model->getProduct();
         if ($this->product) {
             if (!JFactory::getConfig()->get('config.caching', 0)) {
                 $cat_path = $this->get('CategoriesPath');
                 foreach ($cat_path as $cat) {
                     $path->addItem($cat->title, $cat->link);
                 }
             }
             $title = $model->getProductTitle();
             $this->images = $model->getImages();
             $this->related = KSMProducts::getRelated($this->product->id);
             $this->links = KSMProducts::getLinks($this->product->id);
             $document->setTitle($title);
             $model->setProductMetaData();
             if ($this->product->type == 'set') {
                 $document->addScript(JURI::base() . 'components/com_ksenmart/js/set.js', 'text/javascript', true);
                 $this->set_related = KSMProducts::getSetRelated($this->product->id, true);
             } else {
                 $document->addScript(JURI::base() . 'components/com_ksenmart/js/product.js', 'text/javascript', true);
             }
             if ($this->product->is_parent) {
                 $template = $this->params->get('parent_products_template', 'list');
                 if ($template == 'list') {
                     $this->childs_groups = $model->getChildsGroups();
                 } elseif ($template == 'select') {
                     $this->childs_titles = $model->getChildsTitles();
                     $this->childs_title = $model->getChildsTitle();
                     if (!count($this->images)) {
                         $model->_id = $this->product->parent_id;
                         $this->images = $model->getImages();
                         $model->_id = $this->product->id;
                     }
                 }
                 $this->setLayout('parent_product_' . $template);
             } elseif ($this->product->parent_id != 0) {
                 $this->product->parent = KSMProducts::getProduct($this->product->parent_id);
                 if ($this->params->get('parent_products_template', 'list') != 'list') {
                     $template = $this->params->get('parent_products_template', 'list');
                     $this->product->title = $this->product->parent->title;
                     $this->assign('childs_titles', $model->getChildsTitles($this->product->parent_id));
                     $this->assign('childs_title', $model->getChildsTitle($this->product->parent_id));
                     $this->setLayout('parent_product_' . $template);
                 } else {
                     $this->setLayout($this->product->type);
                 }
                 if (!JFactory::getConfig()->get('config.caching', 0)) {
                     $path->addItem($this->product->parent->title, $this->product->parent->link);
                 }
             } else {
                 $this->setLayout($this->product->type);
             }
             if (!JFactory::getConfig()->get('config.caching', 0)) {
                 $path->addItem($this->product->title);
             }
             $model->form = 'review';
             $this->reviewform = $model->getForm();
         } else {
             $this->setLayout('no_product');
         }
     } else {
         if ($this->getLayout() == 'product_comment_form') {
             $document->addScript(JURI::base() . 'components/com_ksenmart/js/product.js', 'text/javascript', true);
             $this->product->id = JRequest::getVar('id', 0);
             $this->setLayout('product_comment_form');
         } elseif ($this->getLayout() == 'comment_congratulation') {
             echo '<script> window.parent.location.reload();</script>';
             $this->setLayout('product_comment_form');
         } else {
             $this->setLayout('no_product');
         }
     }
     parent::display($tpl);
 }