Code: Select all
product_id=247&quantity=1&wk_token=d594eb95a6c54973c975bf4d7a
Code: Select all
public function addToCart() {
$this->load->language('account/api');
$post = $this->request->post;
//Accepting data in json format / raw data
$raw_data = json_decode(file_get_contents("php://input"),true);
if ($raw_data) {
foreach ($raw_data as $key => $value) {
$post[$key] = $value;
}
}
//Get wk_token from header
if (isset(getallheaders()['wk_token'])) {
$post['wk_token'] = getallheaders()['wk_token'];
} elseif (isset(getallheaders()['Wk-Token'])) {
$post['wk_token'] = getallheaders()['Wk-Token'];
}
// Use when get the option in json format
if (isset($post['option']) && $post['option'] && !is_array($post['option'])) {
$post['option'] = stripslashes(html_entity_decode($post['option']));
$post['option'] = json_decode($post['option'],true);
}
if (!isset($post['wk_token']) || !$this->checkSession($post['wk_token'])) {
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode(array('fault'=>1,'message'=>$this->language->get('text_token_message'))));
} else {
$productData = array();
foreach ($post as $key => $value) {
$productData[$key] = $value;
}
$this->load->language('checkout/cart');
$json = array();
if (isset($productData['product_id'])) {
$product_id = (int)$productData['product_id'];
} else {
$product_id = 0;
}
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($product_id);
if ($product_info) {
if (isset($productData['quantity']) && ((int)$productData['quantity'] >= $product_info['minimum'])) {
$quantity = (int)$productData['quantity'];
} else {
$quantity = $product_info['minimum'] ? $product_info['minimum'] : 1;
}
if (isset($productData['option'])) {
$option_data = array();
$option_value_data = array();
foreach ($productData['option'] as $option_key => $option_value) {
if (is_array($option_value)) {
foreach ($option_value as $value_key => $value_value) {
$option_value_data[$value_key] = $value_value;
}
$option_data[$option_key] = $option_value_data;
}
$option_data[$option_key] = $option_value;
}
$option = array_filter($option_data);
} else {
$option = array();
}
$product_options = $this->model_catalog_product->getProductOptions($productData['product_id']);
foreach ($product_options as $product_option) {
if ($product_option['required'] && empty($option[$product_option['product_option_id']])) {
// remove the index(['error']['option'][$product_option['product_option_id']]) of the option for making it simple array for the app end
$json[] = sprintf($this->language->get('error_required'), $product_option['name']);
}
}
if (isset($productData['recurring_id'])) {
$recurring_id = $productData['recurring_id'];
} else {
$recurring_id = 0;
}
$recurrings = $this->model_catalog_product->getProfiles($product_info['product_id']);
if ($recurrings) {
$recurring_ids = array();
foreach ($recurrings as $recurring) {
$recurring_ids[] = $recurring['recurring_id'];
}
if (!in_array($recurring_id, $recurring_ids)) {
// remove the index(['error']['recurring']) for making it simple array for the app end
$json[] = $this->language->get('error_recurring_required');
}
}
if (!$json) {
$this->cart->add($productData['product_id'], $quantity, $option, $recurring_id);
$return_array['message'] = strip_tags(sprintf($this->language->get('text_success'), '', $product_info['name'], ''));
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
// Totals
if (version_compare(VERSION,'3.0.0.0','>=') || version_compare(VERSION,'2.0.0.0','<')) {
$this->load->model('setting/extension');
}else{
$this->load->model('extension/extension');
}
$total_data = array();
$total = 0;
$taxes = $this->cart->getTaxes();
if (version_compare(VERSION,'2.2.0.0','>=')) {
$total_data = array(
'totals' => &$totals,
'taxes' => &$taxes,
'total' => &$total
);
}
// Display prices
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$sort_order = array();
if (version_compare(VERSION,'3.0.0.0','>=') || version_compare(VERSION,'2.0.0.0','<')) {
$results = $this->model_setting_extension->getExtensions('total');
}else{
$results = $this->model_extension_extension->getExtensions('total');
}
foreach ($results as $key => $value) {
$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
}
array_multisort($sort_order, SORT_ASC, $results);
foreach ($results as $result) {
if (version_compare(VERSION,'3.0.0.0','>=')) {
$total_name = 'total_' .$result['code'] . '_status';
}
else{
$total_name = $result['code'] . '_status';
}
if ($this->config->get($total_name)) {
if (version_compare(VERSION,'2.3.0.0','>=')) {
$this->load->model('extension/total/' . $result['code']);
$this->{'model_extension_total_' . $result['code']}->getTotal($total_data);
} else {
$this->load->model('total/' . $result['code']);
if (version_compare(VERSION,'2.2.0.0','>=')) {
$this->{'model_total_' . $result['code']}->getTotal($total_data);
} else {
$this->{'model_total_' . $result['code']}->getTotal($total_data,$total, $taxes);
}
}
}
}
$sort_order = array();
foreach ($total_data as $key => $value) {
if (isset($value['sort_order'])) {
$sort_order[$key] = $value['sort_order'];
} else {
$sort_order[$key] = 1;
}
}
array_multisort($sort_order, SORT_ASC, $total_data);
}
$return_array['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total,$this->session->data['currency']));
$return_array['error'] = 0;
} else {
$return_array['error'] = 1;
$return_array['message'] = $json;
}
}
Code: Select all
{
"message": "Success: You have added iphone x 64g to your shopping cart!",
"total": "5",
"error": 0
}
the resule most loke like this
Code: Select all
{
"message": "Success: You have added iPod Shuffle to your shopping cart!",
"total": "1 item(s) - $100.00",
"error": 0
}