Skip to content

Commit

Permalink
fixed product minimum and added translations for Hungarian
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad Tomaraei committed Mar 11, 2020
1 parent 9298fc7 commit ee52fd8
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 114 deletions.
242 changes: 137 additions & 105 deletions src/catalog/controller/vsbridge/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public function pull(){
$token = $this->getParam('token', true);
$cart_id = $this->getParam('cartId');

if($this->validateCartId($cart_id, $token)){
$this->load->model('vsbridge/api');
$this->load->model('vsbridge/api');

if($this->validateCartId($cart_id, $token)){
$cart = $this->cart->getProducts();

$response = array();
Expand Down Expand Up @@ -124,14 +124,14 @@ public function pull(){
}
}

if(!empty($out_of_stock_products)){
if(!empty($out_of_stock_products)) {
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_out_of_stock').' '.implode(', ', $out_of_stock_products);
}else{
$this->result = $response;
$this->result = $this->language->get('error_out_of_stock') . ' ' . implode(', ', $out_of_stock_products);
$this->sendResponse();
}

$this->result = $response;
}

$this->sendResponse();
Expand Down Expand Up @@ -188,77 +188,91 @@ public function update(){
$cart_id = $this->getParam('cartId');
$input = $this->getPost();

if($this->validateCartId($cart_id, $token)){
if(!empty($input['cartItem'])){

if(!empty($input['cartItem']['sku']) && isset($input['cartItem']['qty'])){

$input['cartItem']['qty'] = (int) $input['cartItem']['qty'];

/* quantity must be greater than or equal to 1 */
/* the delete endpoint is used for removing the item from the cart */
if($input['cartItem']['qty'] < 1){
$input['cartItem']['qty'] = 1;
}

$this->load->model('vsbridge/api');
$this->load->model('vsbridge/api');

$product_info = $this->model_vsbridge_api->getProductBySku($input['cartItem']['sku'], $this->language_id);
if($this->validateCartId($cart_id, $token)){
if(empty($input['cartItem'])) {
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_missing_cart_item');
$this->sendResponse();
}

if(!empty($product_info)){
if(empty($input['cartItem']['sku'])){
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_missing_sku_qty');
$this->sendResponse();
}

// Check if the requested quantity is available on the selected product
if(intval($input['cartItem']['qty']) <= intval($product_info['quantity'])){
$input['cartItem']['qty'] = (int) $input['cartItem']['qty'];

if(isset($input['cartItem']['item_id'])){
$this->cart->update($input['cartItem']['item_id'], $input['cartItem']['qty']);
}else{
$this->cart->add($product_info['product_id'], $input['cartItem']['qty']);
}
/* quantity must be greater than or equal to 1 */
/* the delete endpoint is used for removing the item from the cart */
if($input['cartItem']['qty'] < 1){
$input['cartItem']['qty'] = 1;
}

$cart_products = $this->cart->getProducts();

$response = array();

foreach($cart_products as $cart_product){
if($cart_product['product_id'] == $product_info['product_id']){
$response = array(
'item_id' => (int) $cart_product['cart_id'],
'sku' => $cart_product['model'],
'qty' => (int) $cart_product['quantity'],
'name' => $cart_product['name'],
'price' => (float) $cart_product['price'],
'product_type' => 'simple',
'quote_id' => $cart_id
);
}
}
$product_info = $this->model_vsbridge_api->getProductBySku($input['cartItem']['sku'], $this->language_id);

$this->result = $response;
if(empty($product_info)){
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_invalid_sku');
$this->sendResponse();
}

}else{
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_out_of_stock').' ['.$product_info['model'].'] '.$product_info['name'];
}
// Check if the requested quantity is available on the selected product
if(intval($input['cartItem']['qty']) > intval($product_info['quantity'])){
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_out_of_stock').' ['.$product_info['model'].'] '.$product_info['name'];
$this->sendResponse();
}

}else{
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_invalid_sku');
}
// Check if the requested quantity meets the minimum (and multiple of minimum) product quantity
if($product_info['minimum']){
if (intval($input['cartItem']['qty']) < intval($product_info['minimum'])) {
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = sprintf($this->language->get('error_minimum_product_quantity'), $product_info['model'], $product_info['minimum']);
$this->sendResponse();
}

}else{
$qty_multiple = intval($input['cartItem']['qty']) / intval($product_info['minimum']);
if (!is_int($qty_multiple)) {
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_missing_sku_qty');
$this->result = sprintf($this->language->get('error_minimum_multiple_product_quantity'), $product_info['model'], $product_info['minimum']);
$this->sendResponse();
}
}

if(isset($input['cartItem']['item_id'])){
$this->cart->update($input['cartItem']['item_id'], $input['cartItem']['qty']);
}else{
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_missing_cart_item');
$this->cart->add($product_info['product_id'], $input['cartItem']['qty']);
}

$cart_products = $this->cart->getProducts();

$response = array();

foreach($cart_products as $cart_product){
if($cart_product['product_id'] == $product_info['product_id']){
$response = array(
'item_id' => (int) $cart_product['cart_id'],
'sku' => $cart_product['model'],
'qty' => (int) $cart_product['quantity'],
'name' => $cart_product['name'],
'price' => (float) $cart_product['price'],
'product_type' => 'simple',
'quote_id' => $cart_id
);
}
}

$this->result = $response;
}

$this->sendResponse();
Expand Down Expand Up @@ -294,38 +308,37 @@ public function delete(){
$cart_id = $this->getParam('cartId');
$input = $this->getPost();

if($this->validateCartId($cart_id, $token)) {
if (!empty($input['cartItem'])) {

if (!empty($input['cartItem']['sku']) && isset($input['cartItem']['item_id'])) {

$this->load->model('vsbridge/api');

$product_info = $this->model_vsbridge_api->getProductBySku($input['cartItem']['sku'], $this->language_id);
$this->load->model('vsbridge/api');

if(!empty($product_info)){
if($this->validateCartId($cart_id, $token)) {
if (empty($input['cartItem'])) {
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_missing_cart_item');
$this->sendResponse();
}

$this->cart->remove($input['cartItem']['item_id']);
if (empty($input['cartItem']['sku']) || !isset($input['cartItem']['item_id'])) {
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_missing_sku_item_id');
$this->sendResponse();
}

$this->result = true;

}else{
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_invalid_sku');
}

}else{
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_missing_sku_item_id');
}
$product_info = $this->model_vsbridge_api->getProductBySku($input['cartItem']['sku'], $this->language_id);

}else{
if(empty($product_info)){
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_missing_cart_item');
$this->result = $this->language->get('error_invalid_sku');
$this->sendResponse();
}

$this->cart->remove($input['cartItem']['item_id']);

$this->result = true;
}

$this->sendResponse();
Expand All @@ -347,21 +360,20 @@ public function apply_coupon(){
$cart_id = $this->getParam('cartId');
$coupon = $this->getParam('coupon');

if($this->validateCartId($cart_id, $token)) {
$this->load->model('extension/total/coupon');

$this->load->model('extension/total/coupon');
if($this->validateCartId($cart_id, $token)) {
$coupon_info = $this->model_extension_total_coupon->getCoupon($coupon);

if($coupon_info){
$this->session->data['coupon'] = $coupon;
$this->result = true;

}else{
if(!$coupon_info){
$this->load->language('extension/total/coupon');
$this->code = 500;
$this->result = $this->language->get('error_coupon');
$this->sendResponse();
}

$this->session->data['coupon'] = $coupon;
$this->result = true;
}

$this->sendResponse();
Expand Down Expand Up @@ -554,7 +566,6 @@ public function payment_methods(){

if($this->validateCartId($cart_id, $token)) {
/* We're estimating the payment methods since OpenCart requires address details that aren't provided by VSF at registration */

$this->load->language('checkout/checkout');

$country_id = $this->config->get('config_country_id');
Expand Down Expand Up @@ -655,20 +666,22 @@ public function payment_methods(){
}

if (empty($this->session->data['payment_methods'])) {
$this->load->language('vsbridge/api');
$this->code = 500;
$this->result = $this->language->get('error_no_payment');
}else{
$adjusted_payment_methods = array();
$this->sendResponse();
}

foreach($this->session->data['payment_methods'] as $payment_method){
$adjusted_payment_methods[] = array(
'code' => $payment_method['code'],
'title' => $payment_method['title']
);
}
$adjusted_payment_methods = array();

$this->result = $adjusted_payment_methods;
foreach($this->session->data['payment_methods'] as $payment_method){
$adjusted_payment_methods[] = array(
'code' => $payment_method['code'],
'title' => $payment_method['title']
);
}

$this->result = $adjusted_payment_methods;
}

$this->sendResponse();
Expand Down Expand Up @@ -750,6 +763,25 @@ public function shipping_methods(){
$quote = $this->{'model_extension_shipping_' . $result['code']}->getQuote($this->session->data['shipping_address']);

if ($quote) {
// Clear Thinking: Ultimate Restrictions
if (($this->config->get('ultimate_restrictions_status') || $this->config->get('module_ultimate_restrictions_status')) && isset($this->session->data['ultimate_restrictions'])) {
foreach ($quote['quote'] as $index => $restricting_quote) {
foreach ($this->session->data['ultimate_restrictions'] as $extension => $rules) {
if ($extension != $result['code']) continue;
foreach ($rules as $comparison => $values) {
$adjusted_title = explode('(', $restricting_quote['title']);
$adjusted_title = strtolower(html_entity_decode(trim($adjusted_title[0]), ENT_QUOTES, 'UTF-8'));
if (($comparison == 'is' && in_array($adjusted_title, $values)) || ($comparison == 'not' && !in_array($adjusted_title, $values))) {
unset($quote['quote'][$index]);
}
}
}
}
if (empty($quote['quote'])) {
continue;
}
}
// end
$shipping_methods[$result['code']] = array(
'title' => $quote['title'],
'quote' => $quote['quote'],
Expand All @@ -768,13 +800,13 @@ public function shipping_methods(){

array_multisort($sort_order, SORT_ASC, $shipping_methods);

if ($shipping_methods) {
$this->session->data['shipping_methods'] = $shipping_methods;
} else {
if (!$shipping_methods) {
$this->code = 500;
$this->result = $this->language->get('error_no_shipping');
$this->sendResponse();
}

$this->session->data['shipping_methods'] = $shipping_methods;

$adjusted_shipping_methods = array();

Expand Down
14 changes: 6 additions & 8 deletions src/catalog/controller/vsbridge/products.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,19 @@ public function populateProducts($input){
'tax_class_id' => (int) $product['tax_class_id'],
'media_gallery' => $media_gallery,
'name' => $product['name'],
'minimum' => !empty($product['minimum']) ? (int) $product['minimum'] : 1,
'status' => (int) $product['status'],
'slug' => $slug,
'url_path' => $slug,
'created_at' => $product['date_added'],
'updated_at' => $product['date_modified']
);

if(!empty($product['quantity'])){
if(intval($product['quantity']) > 0){
$product_array['qty'] = 1;
}else{
$product_array['qty'] = 0;
}
}else{
$product_array['qty'] = 0;
$product_array['qty'] = 0;

if(isset($product['quantity']) && intval($product['quantity']) > 0) {
// If the product has stock, set the add-to-cart qty as the minimum product quantity
$product_array['qty'] = $product_array['minimum'];
}

foreach(array('length', 'width', 'height', 'weight') as $dimension) {
Expand Down
3 changes: 2 additions & 1 deletion src/catalog/language/en-gb/vsbridge/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
$_['error_invalid_payment_code'] = 'Invalid payment code.';
$_['error_no_payment_method'] = 'No payment method provided.';
$_['error_cart_product_stock'] = 'Cart does not have products with valid stocks.';
$_['error_minimum_product_quantity'] = 'You must order at least %s of %s, but the current ordered quantity is %s.';
$_['error_minimum_product_quantity'] = 'The minimum order quantity of %s is %s.';
$_['error_minimum_multiple_product_quantity'] = 'The order quantity for %s can only be a multiple of %s.';
$_['error_extension_disabled'] = 'Extension disabled.';
$_['error_api_endpoint_disabled'] = 'API endpoint disabled.';
$_['error_token_expired'] = 'Authentication failed. Token has expired.';
Expand Down
Loading

0 comments on commit ee52fd8

Please sign in to comment.