Back to Easy Shop Support

paulgrafx
paulgrafx

There is an issue with the output of a single type of product in the shopping cart page.

Example being if there is Product A that has 12 in stock.

If I choose to add 4 of Product A and then I try it again to add another 4 of Product A then I go to the checkout page I get this.

  • Product A x 4
  • Product A x 4.

Instead of this.

  • Product A x 8.

Last updated

paulgrafx
paulgrafx

I think this needs adding to the /components/CartTrait.php to sort the above problem.

Note that this has't been fully tested. I also added some code to sort the item quantity issue against the item database quantity.

$itemAdded = $cart->addItem($item, $options, $price, $qty, $index);

// Detect duplicates sort quantity and total
$check = array();
foreach($cart->items as $product){ 
    if(isset($check[$product['id']])){  
        $check[$product['id']]['quantity'] += $product['quantity']; 
        $check[$product['id']]['total'] += $product['total']; 
    } else {  
        $check[$product['id']] = $product;  
    }

    // Detect quantity is higher than the database item quantity
    if($item->quantity < $check[$product['id']]['quantity']){
        $check[$product['id']]['quantity'] = $item->quantity; 
        $check[$product['id']]['total'] = $price * $item->quantity; 
        Flash::warning('The maximum quantity for this product has been reached.');
    }
}

// Reset the index keys
$check = array_merge($check);

$cart->items = $check;

Last updated

paulgrafx
paulgrafx

The above worked fine for single products but for products with 'variants' like t-shirts such as red, green etc, it just didn't work so here are my changes which I could do with somebody else testing and maybe implementing into the plugin.

file: /components/cartcontainer/cart.html Sorts the deleting individual items out of cart.

Line 29: data-request-data="index: '{{ item.id }}-{{ item.index }}'"

file: /components/CartTrait.php

$itemQuantity = null;

if($item->is_with_variants){
    foreach ($item->variants as $variant) {                 
        $id = $variant['id'];
        $optionVal = null;

        if(array_key_exists($id, $optionsIndex) && !empty($optionsIndex[$id]))
        $optionVal = $optionsIndex[$id];
        if($optionVal == null && !$variant['optional'])
        return [ Flash::error(trans('pixel.shop::lang.messages.option_required', ['option' => $variant['variant']])) ];

        // Get the quantity
        foreach ($variant['items'] as $variantItem) {   
            if($variantItem['ref'] == $optionVal) {
                $itemQuantity = $variantItem['quantity'];
            }
        }
    }
}

$itemAdded = $cart->addItem($item, $options, $price, $qty, $index);

// Detect duplicates sort quantity and total
$check = array();
foreach($cart->items as $product){ 

    if (array_key_exists($product['id'].'-'.$product['index'], $check))
    {
        // Key exists
        $check[$product['id'].'-'.$product['index']]['quantity'] += $product['quantity']; 
        $check[$product['id'].'-'.$product['index']]['total'] += $product['total']; 

        // Detect quantity is higher than the database item quantity
        if($item->quantity < $check[$product['id'].'-'.$product['index']]['quantity']){
            $check[$product['id'].'-'.$product['index']]['quantity'] = $item->quantity; 
            $check[$product['id'].'-'.$product['index']]['total'] = $price * $item->quantity; 
            Flash::warning('The maximum quantity for this product has been reached.');
        }
    }
    else
    {
        // Key does not exist
        $check[$product['id'].'-'.$product['index']] = $product; 
    }

    // Detect quantity is higher than the database item quantity    
    if(isset($itemQuantity) && $itemQuantity < $check[$product['id'].'-'.$product['index']]['quantity'] 
        && $product['index'] == $id . '::' . $optionVal){
        $check[$product['id'].'-'.$product['index']]['quantity'] = $itemQuantity; 
        $check[$product['id'].'-'.$product['index']]['total'] = $price * $itemQuantity; 
        Flash::warning('The maximum quantity for this product has been reached ' . $itemQuantity . '.');
    }
}

$cart->items = $check;

$cart->updateTotals();
$cart->save();

Last updated

paulgrafx
paulgrafx

Also had to sort the reduceInventory function when a product has been paid for and quantity had to be changed.

Last updated

pixeldev
pixeldev

Thanks for the feedback. Unfortunately I cannot continue the project at the moment. I changed the plugin from Paid to free for the same reason, I am already working on a bigger project and my time is limited and I don't receive any monetary compensation by EasyShop. The project is open so that they can extend it and add those extra functionalities to it.

1-5 of 5