Back to PhocacartAttribute class

Method getAllRequiredAttributesByProduct

public static
getAllRequiredAttributesByProduct
(mixed $productId)

Method getAllRequiredAttributesByProduct - Source code

/*
 * Check if attribute is required or not
 * This is checked when adding products to cart (normally, this should not happen, as html5 input form checking should do it)
 * Adding products to cart - this is only security check
 * Checking products before making order - this is only security check
 * Standard user will not add empty attributes if required because html5 form checking will tell him
 * This is really only for cases, someone will try to forge the form - server side checking
 */
/*public static function checkIfRequired($id, $value) {

        // Multiple value
        if ((int)$id > 0 && is_array($value) && !empty($value)) {

            return true;
        }
        // One value
        if ((int)$id > 0 && (int)$value > 0) {
            return true;// Attribute set and value set too - we don't have anything to check, as attribute value was selected
        }

        if ((int)$id > 0 && (int)$value == 0) {
            $db =Factory::getDBO();
            $query = ' SELECT a.required'
            .' FROM #__phocacart_attributes AS a'
            .' WHERE a.id = '.(int)$id
            .' ORDER BY a.id'
            .' LIMIT 1';
            $db->setQuery($query);
            $attrib = $db->loadObject();
            if (isset($attrib->required) && $attrib->required == 0) {
                return true;
            } else {
                return false;// seems like attribute is required but not selected
            }
        }

        return false;
    }*/
/* Check if the product includes some required attribute
 * If yes, but users tries to add the product without attribute (forgery)
 * just check it on server side
 * BE AWARE - this test runs only in case when attributes are empty
 * We don't check if attribute was selected or not or if is required or not
 * We didn't get any attribute when ordering this product and we only check
 * if the product includes some attribute
 */
/*public static function checkIfExistsAndRequired($productId) {

        $wheres		= array();
        $wheres[] 	= ' a.id = '.(int)$productId;
        $db 		= Factory::getDBO();
        $query = ' SELECT a.id,'
        .' at.required AS attribute_required'
        .' FROM #__phocacart_products AS a'
        .' LEFT JOIN #__phocacart_attributes AS at ON a.id = at.product_id AND at.id > 0 AND at.required = 1'
        . ' WHERE ' . implode( ' AND ', $wheres )
        . ' ORDER BY a.id'
        . ' LIMIT 1';
        $db->setQuery($query);
        $attrib = $db->loadObject();

        if ((int)$attrib->attribute_required > 0) {
            return false;
        } else {
            return true;
        }

        return false;
    }*/
public static function getAllRequiredAttributesByProduct($productId)
{
    $wheres = array();
    $wheres[] = ' a.id = ' . (int) $productId;
    $db = Factory::getDBO();
    // 1) Select required attributes
    $query = ' SELECT at.id, at.type, "1" AS required_type, "" AS options' . ' FROM #__phocacart_products AS a' . ' LEFT JOIN #__phocacart_attributes AS at ON a.id = at.product_id AND at.id > 0 AND at.required = 1' . ' WHERE ' . implode(' AND ', $wheres) . ' ORDER BY a.id';
    $db->setQuery($query);
    $attributes = $db->loadAssocList('id');
    // Select required options of specific attributes of attributes which are not required (so $attributes and $attributesOptions will no cover each other)
    $query = ' SELECT av.id as option_id, at.id, at.type, "2" AS required_type, "" as options' . ' FROM #__phocacart_products AS a' . ' LEFT JOIN #__phocacart_attributes AS at ON a.id = at.product_id AND at.id > 0 AND at.required = 0' . ' LEFT JOIN #__phocacart_attribute_values AS av ON at.id = av.attribute_id AND av.id > 0 AND av.required = 1' . ' WHERE ' . implode(' AND ', $wheres) . ' ORDER BY a.id';
    $db->setQuery($query);
    $attributesOptions = $db->loadAssocList();
    // correct empty attributes and add attributes which have required options but are not required themselves
    if (!empty($attributes)) {
        if (!empty($attributesOptions)) {
            foreach ($attributesOptions as $k => $v) {
                if (isset($v['id']) && $v['id'] > 0 && isset($v['option_id']) && $v['option_id'] > 0) {
                    $idA = $v['id'];
                    $idO = $v['option_id'];
                    $attributes[$idA]['id'] = $idA;
                    $attributes[$idA]['options'][$idO] = $v['option_id'];
                    $attributes[$idA]['required_type'] = $v['required_type'];
                    $attributes[$idA]['type'] = $v['type'];
                }
            }
        }
        foreach ($attributes as $k => $v) {
            if (!$v['id'] && !$v['type']) {
                unset($attributes[$k]);
            }
        }
    }
    return $attributes;
}