@ 2017-07-11T17:29:45Z <?php
$product = false;
$quantity = 1;
$postedOptions = false;
$downloads = false;
$combine = false; //is this an item from a separate cart being combined?
extract($data);
if(is_int($product))
{
$product = \CI::Products()->getProduct($product);
if(!$product)
{
return json_encode(['error'=>lang('error_product_not_found')]);
}
//Clean up the product for the orderItems database
$product = $this->cleanProduct($product);
//get downloadable files
$downloads = \CI::DigitalProducts()->getAssociationsByProduct($product->product_id);
}
$update = false;
if(empty($product->hash))
{
$product->hash = md5(json_encode($product).json_encode($postedOptions));
//set defaults for new items
$product->coupon_discount = 0;
$product->coupon_discount_quantity = 0;
$product->coupon_code = '';
}
else
{
if(!$combine)
{
//this is an update
$update = true;
}
}
$product->order_id = $this->cart->id;
//loop through the products in the cart and make sure we don't have this in there already. If we do get those quantities as well
$qty_count = $quantity;
$this->getCartItems(); // refresh the cart items
foreach($this->items as $item)
{
if(intval($item->product_id) == intval($product->product_id))
{
if($item->hash != $product->hash) //if the hashes match, skip this step (this is an update)
{
$qty_count = $qty_count + $item->quantity;
}
}
if($item->hash == $product->hash && !$update) //if this is an update skip this step
{
//if the item is already in the cart, send back a message
return json_encode(['message'=>lang('item_already_added'), 'product_id'=>$product->product_id]);
}
}
if(!config_item('allow_os_purchase') && (bool)$product->track_stock)
{
$stock = \CI::Products()->getProduct($product->product_id);
if($stock->quantity < $qty_count)
{
return json_encode(['error'=>sprintf(lang('not_enough_stock'), $stock->name, $stock->quantity)]);
}
}
if (!$quantity || $quantity <= 0 || $product->fixed_quantity==1)
{
$product->quantity = 1;
}
else
{
$product->quantity = $quantity;
}
//create save options array here for use later.
$saveOptions = [];
if(!$update && $product->product_id) // if not an update or non-product, try and run the options
{
//set the base "total_price"
if($product->saleprice > 0)
{
$product->total_price = $product->saleprice;
}
else
{
$product->total_price = $product->price;
}
//set base "total_weight"
$product->total_weight = $product->weight;
$productOptions = \CI::ProductOptions()->getProductOptions($product->product_id);
//option error vars
$optionError = false;
$optionErrorMessage = lang('option_error').'<br/>';
foreach($productOptions as $productOption)
{
// are we missing any required values?
$optionValue = false;
if(!empty($postedOptions[$productOption->id]))
{
$optionValue = $postedOptions[$productOption->id];
}
if((int)$productOption->required && empty($optionValue))
{
$optionError = true;
$optionErrorMessage .= "- ". $productOption->name .'<br/>';
continue; // don't bother processing this particular option any further
}
if(empty($optionValue))
{
//empty? Move along, nothing to see here.
continue;
}
//create options to save to the database in case we get past the errors
if($productOption->type == 'checklist')
{
if(is_array($optionValue))
{
foreach($optionValue as $ov)
{
foreach($productOption->values as $productOptionValue)
{
if($productOptionValue->id == $ov)
{
$saveOptions[] = [
'option_name'=>$productOption->name,
'value'=>$productOptionValue->value,
'price'=>$productOptionValue->price,
'weight'=>$productOptionValue->weight
];
$product->total_weight += $productOptionValue->weight;
$product->total_price += $productOptionValue->price;
}
}
}
}
}
else //every other form type we support
{
$saveOption = [];
if($productOption->type == 'textfield' || $productOption->type == 'textarea')
{
$productOptionValue = $productOption->values[0];
$productOptionValue->value = $optionValue;
}
else //radios and checkboxes
{
foreach($productOption->values as $ov)
{
if($ov->id == $optionValue)
{
$productOptionValue = $ov;
break;
}
}
$saveOption['value'] = $optionValue;
}
if(isset($productOptionValue))
{
$saveOption['option_name'] = $productOption->name;
$saveOption['price'] = $productOptionValue->price;
$saveOption['weight'] = $productOptionValue->weight;
$saveOption['value'] = $productOptionValue->value;
//add it to the array;
$saveOptions[] = $saveOption;
//update the total weight and price
$product->total_weight += $productOptionValue->weight;
$product->total_price += $productOptionValue->price;
}
}
}
if($optionError)
{
return json_encode(['error'=>$optionErrorMessage]);
}
}
//save the product
$product_id = \CI::Orders()->saveItem((array)$product);
//save the options if we have them
foreach($saveOptions as $saveOption)
{
$saveOption['order_item_id'] = $product_id;
$saveOption['order_id'] = $this->cart->id;
\CI::Orders()->saveItemOption($saveOption);
}
if($update)
{
foreach($this->items as $key => $item)
{
if($item->id == $product_id)
{
$this->items[$key] = $product;
}
}
}
else
{
$product->id = $product_id;
$this->items[] = $product;
//update file downloads
if($downloads)
{
foreach($downloads as $file)
{
\CI::Orders()->saveOrderItemFile(['order_id'=>$this->cart->id, 'order_item_id'=>$product->id, 'file_id'=>$file->file_id]);
}
}
}
//get current item count
$itemCount = $this->totalItems();
if($update)
{
return json_encode(['message'=>lang('cart_updated'), 'itemCount'=>$itemCount]);
}
else
{
return json_encode(['message'=>lang('item_added_to_cart'), 'itemCount'=>$itemCount]);
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for git.master , git.master_jit , rfc.property-hooks Fatal error: Uncaught TypeError: extract(): Argument #1 ($array) must be of type array, null given in /in/g6qTv:10
Stack trace:
#0 /in/g6qTv(10): extract(NULL)
#1 {main}
thrown in /in/g6qTv on line 10
Process exited with code 255 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
35.31 ms | 401 KiB | 8 Q