public static
getStockItemsChangedByAttributes
(mixed &$stockStatus, mixed $attributes, mixed $item, mixed $selectedAttributes = 0)
/*
* $selectedAttributes ... ajax, checkout, checkout json - attributes are not all attributes assigned to product but only the one selected by e.g. select box
* (including the option that no attribute was selected - in this case the main product is even a variant)
*/
public static function getStockItemsChangedByAttributes(&$stockStatus, $attributes, $item, $selectedAttributes = 0)
{
//$paramsC = PhocacartUtils::getComponentParameters();
//$display_unit_price = $paramsC->get( 'display_unit_price', 1 );
$stock = 0;
// main stock count - rendered output of stock item (by product, attribute or mix of attributes ASM)
$stockProduct = isset($item->stock) ? $item->stock : 0;
// stock stored by product
$stockAttribute = 0;
// stock stored by each attribute
$fullAttributes = array();
// Array of full objects (full options object)
$thinAttributes = array();
// Array of integers only
if ($selectedAttributes == 2) {
// Final order, we get the attributes in different format as by adding them to cart
// it is final order, we order the items so we don't check e.g. if default values or not - the values are selected yet
$fullAttributes = PhocacartAttribute::getAttributeFullValues($attributes);
$thinAttributes = PhocacartAttribute::getAttributesSanitizeOptionArray($attributes);
//select only default v a to create product key
} else {
if ($selectedAttributes == 1) {
$fullAttributes = PhocacartAttribute::getAttributeFullValues($attributes);
$thinAttributes = $attributes;
//select only default value attributes (selected options) to create product key
} else {
$fullAttributes = $attributes;
$thinAttributes = PhocacartAttribute::getAttributesSelectedOnly($attributes);
//select only default v a to create product key
}
}
// Stock Calculation
// 0 ... Main Product
// 1 ... Product Variations
// 2 ... Advanced Stock Management
// 3 ... Advanced Stock And Price Management ( 2 + price)
if ($item->stock_calculation == 1) {
// Product Variations - Be aware can be wrong count of stock when mixing attributes - works only one attribute
$i = 0;
if (!empty($fullAttributes)) {
// Is some attribute with its option active (selected). If not, then the main product variant should be set
$productAttributeSelected = 0;
foreach ($fullAttributes as $k => $v) {
$attributeSelected = 0;
$stockAttribute = 0;
if (!empty($v->options)) {
$i++;
foreach ($v->options as $k2 => $v2) {
// Is the options set as default
// See: administrator\components\com_phocacart\libraries\phocacart\price\price.php
// function getPriceItemsChangedByAttributes - similar behaviour
if ($selectedAttributes == 1 || $selectedAttributes == 2 || $selectedAttributes == 0 && isset($v2->default_value) && $v2->default_value == 1) {
$attributeSelected = 1;
$productAttributeSelected = 1;
if (isset($v2->stock) && $v2->stock > 0) {
$stockAttribute += (int) $v2->stock;
}
}
}
}
if ($attributeSelected == 1) {
$stock += $stockAttribute;
} else {
// we iterate over attributes not products
// so this is the variant for main product
// IF there is more than one parameter, this will be always wrong here because
// 1) first attribute not selected, second selected: should we add only second or second plus main product stock?
// Product stock 100
// Product attribute A stock 50
// Product attribute B stock 50
// first attribute not selected = 0, second selected = 50 (we skip to add the main product 100)
// 2) first not selected, second not selected: ok we add only the main product stock
// both have 0 but we select main product which is 100
// if we should add main product then:
// $stock += $stockProduct;
}
}
if ($productAttributeSelected == 0) {
// We have attributes and their options
// but no one set as default or selected
$stock += $stockProduct;
}
} else {
if ($selectedAttributes == 1) {
// If there are no attributes, the main product is the varaint itself
$stock += $item->stock;
}
}
if ($i > 1 && $selectedAttributes != 1) {
PhocacartLog::add(3, 'Warning', $item->id, Text::_('COM_PHOCACART_INAPPROPRIATE_METHOD_STOCK_CALCULATION_PRODUCT_VARIATIONS') . ' ' . Text::_('COM_PHOCACART_PRODUCT') . ': ' . $item->title);
}
} else {
if ($item->stock_calculation == 2 || $item->stock_calculation == 3) {
// Advanced Stock Management
$k = PhocacartProduct::getProductKey((int) $item->id, $thinAttributes);
$stock = PhocacartAttribute::getCombinationsStockByKey($k);
} else {
// Main Product
$stock = $item->stock;
}
}
// Get all stock status information: count, status, image, ...
$stockStatus = PhocacartStock::getStockStatus((int) $stock, (int) $item->min_quantity, (int) $item->min_multiple_quantity, (int) $item->stockstatus_a_id, (int) $item->stockstatus_n_id);
return $stock;
}