Exemplo n.º 1
0
 function add()
 {
     $app = JFactory::getApplication();
     JFactory::getDocument()->setCharset('utf-8');
     $params = JComponentHelper::getParams('com_k2store');
     $model = $this->getModel('mycart');
     $cart_helper = new K2StoreHelperCart();
     require_once JPATH_COMPONENT . '/helpers/cart.php';
     $error = array();
     $json = array();
     //get the product id
     $product_id = $app->input->getInt('product_id', 0);
     //no product id?. return an error
     if (empty($product_id)) {
         $error['error']['product'] = JText::_('K2STORE_ADDTOCART_ERROR_MISSING_PRODUCT_ID');
         echo json_encode($error);
         $app->close();
     }
     //Ok. we have a product id. so proceed.
     //get the quantity
     $quantity = $app->input->get('product_qty');
     if (isset($quantity)) {
         $quantity = $quantity;
     } else {
         $quantity = 1;
     }
     $product = $cart_helper->getItemInfo($product_id);
     //get the product options
     $options = $app->input->get('product_option', array(0), 'ARRAY');
     $fabrics = $app->input->get('fabric', array(0), 'ARRAY');
     if (isset($options)) {
         $options = array_filter($options);
     } else {
         $options = array();
     }
     $product_options = $model->getProductOptions($product_id);
     //iterate through stored options for this product and validate
     foreach ($product_options as $product_option) {
         if ($product_option['required'] && empty($options[$product_option['product_option_id']])) {
             $json['error']['option'][$product_option['product_option_id']] = JText::sprintf('K2STORE_ADDTOCART_PRODUCT_OPTION_REQUIRED', $product_option['option_name']);
         }
     }
     //trigger before addtocart plugin event now... send post values
     $post_data = $app->input->getArray($_POST);
     JPluginHelper::importPlugin('k2store');
     $results = $app->triggerEvent("onK2StoreBeforeAddCart", array($post_data));
     if (isset($results) && count($results)) {
         foreach ($results as $result) {
             if (!empty($result['error'])) {
                 $json['warning'] = $result['error'];
             }
         }
     }
     //validation is ok. Now add the product to the cart.
     if (!$json) {
         // JFactory::getSession()->set('fabric_id', $post_data['fabric_id']);
         // $fabricid[$product_id] = $post_data['fabric_id'];
         $cart_helper->add($product_id, $quantity, $options, $fabrics);
         //trigger plugin event- after addtocart
         $app->triggerEvent("onK2StoreAfterAddCart", array($post_data));
         $product_info = K2StoreHelperCart::getItemInfo($product_id);
         $cart_link = JRoute::_('index.php?option=com_k2store&view=mycart');
         $json['success'] = true;
         $json['successmsg'] = $product_info->product_name . ' ' . JText::sprintf('K2STORE_ADDTOCART_ADDED_TO_CART');
         $json['data'] = $post_data;
         //$total =  K2StoreHelperCart::getTotal();
         $totals = $model->getTotals();
         if ($params->get('auto_calculate_tax', 1)) {
             $total = $totals['total'];
         } else {
             $total = $totals['total_without_tax'];
         }
         $product_count = K2StoreHelperCart::countProducts();
         //get product total
         $json['total'] = JText::sprintf('K2STORE_CART_TOTAL', $product_count, K2StorePrices::number($total));
         //get cart info
         //do we have to redirect to the cart
         if ($params->get('popup_style', 1) == 3) {
             $json['redirect'] = $cart_link;
         }
     } else {
         //do we have to redirect
         //	$url = 'index.php?option=com_k2&view=item&id='.$product_id;
         //	$json['redirect'] = JRoute::_($url);
     }
     echo json_encode($json);
     $app->close();
 }