Files
shopPRO/autoload/shop/class.Basket.php

92 lines
2.3 KiB
PHP

<?php
namespace shop;
class Basket implements \ArrayAccess
{
public function __construct()
{
}
static public function validate_basket( $basket )
{
if ( !is_array( $basket ) )
return $basket = array();
else
return $basket;
}
// sprawdzanie czy ilość produktu w koszyku nie jest większa niż stan magazynowy
public static function check_product_quantity_in_stock( $basket, bool $message = false )
{
$result = false;
foreach ( $basket as $key => $val )
{
$permutation = null;
if ( isset( $val['parent_id'] ) and (int)$val['parent_id'] and isset( $val['product-id'] ) )
$permutation = \shop\Product::get_product_permutation_hash( (int)$val['product-id'] );
if ( !$permutation and isset( $val['attributes'] ) and is_array( $val['attributes'] ) and count( $val['attributes'] ) )
$permutation = implode( '|', $val['attributes'] );
$quantity_options = \shop\Product::get_product_permutation_quantity_options(
$val['parent_id'] ? $val['parent_id'] : $val['product-id'],
$permutation
);
if (
(int)$basket[ $key ][ 'quantity' ] < 1
and ( (int)$quantity_options['quantity'] > 0 or (int)$quantity_options['stock_0_buy'] === 1 )
)
{
$basket[ $key ][ 'quantity' ] = 1;
$result = true;
}
if ( ( $val[ 'quantity' ] > $quantity_options['quantity'] ) and !$quantity_options['stock_0_buy'] )
{
$basket[ $key ][ 'quantity' ] = $quantity_options['quantity'];
if ( $message )
\S::error( 'Ilość jednego lub więcej produktów została zmniejszona z powodu niestarczających stanów magazynowych. Sprawdź proszę koszyk.' );
$result = true;
}
}
\S::set_session( 'basket', $basket );
return $result;
}
public function __get( $variable )
{
if ( array_key_exists( $variable, $this -> data ) )
return $this -> $variable;
}
public function __set( $variable, $value )
{
$this -> $variable = $value;
}
public function offsetExists( $offset )
{
return isset( $this -> $offset );
}
public function offsetGet( $offset )
{
return $this -> $offset;
}
public function offsetSet( $offset, $value )
{
$this -> $offset = $value;
}
public function offsetUnset( $offset )
{
unset( $this -> $offset );
}
}