Back to PhocacartProduct class

Method getProductKey

public static
getProductKey
(mixed $id, mixed $attributes = array(), mixed $encode = 1)

Method getProductKey - Source code

public static function getProductKey($id, $attributes = array(), $encode = 1)
{
    $key = (int) $id . ':';
    if (!empty($attributes)) {
        // Sort attributes (because of right key generation)
        ksort($attributes);
        // Remove empty values, so items with empty values (add to cart item view) is the same
        // like item without any values (add to cart category view)
        foreach ($attributes as $k => $v) {
            // Transform all attribute values to array (when they get string instead of array from html)
            if (!is_array($v)) {
                $attributes[$k] = array((int) $v => (int) $v);
            }
            // Unset when string is empty or zero
            if ($v == 0 || $v == '') {
                unset($attributes[$k]);
            }
            // Unset when we have transformed it to array but it is empty
            if (empty($v)) {
                unset($attributes[$k]);
            }
            if (!empty($v) && is_array($v)) {
                $attributeType = PhocacartAttribute::getAttributeType((int) $k);
                foreach ($v as $k2 => $v2) {
                    //$attributes[$k][$k2] = (int)$v2;
                    $optionType = 0;
                    // Ask for optionType but only by GIFT attribute (20) ATTRIBUTETYPE
                    if ($attributeType == 20) {
                        $optionType = PhocacartAttribute::getOptionType((int) $k2);
                    }
                    $attributes[$k][$k2] = PhocaCartAttribute::setAttributeValue((int) $attributeType, $v2, false, false, $optionType);
                    // TEXT or GIFT attributes
                    if ($attributes[$k][$k2] == '') {
                        unset($attributes[$k][$k2]);
                    }
                }
                if (empty($attributes[$k])) {
                    unset($attributes[$k]);
                    // if all values removed from attribute, remove the attribute completely
                }
            }
        }
        // Sort options (because of right key generation)
        foreach ($attributes as $k3 => $v3) {
            if (is_array($v3)) {
                ksort($attributes[$k3]);
            }
        }
        if (!empty($attributes)) {
            if ($encode == 0) {
                return serialize($attributes);
            }
            $key .= base64_encode(serialize($attributes));
        }
    }
    $key .= ':';
    return $key;
    /*$key = 'ID:'.(int)$id .'{';
    
            if (!empty($attributes)) {
    
                ksort($attributes);
                // Remove empty values, so items with empty values (add to cart item view) is the same
                // like item without any values (add to cart category view)
                foreach($attributes as $k => $v) {
                    if ($v == 0 || $v == '') {
                        unset($attributes[$k]);
                    }
                }
                foreach($attributes as $k => $v) {
                    if (is_array($v)){
                        asort($attributes[$k]);
                    }
                }
    
                foreach($attributes as $k => $v) {
                    $key .= 'AID:'.(int)$k . '[';
                    if (is_array($v)){
                        foreach($v as $k2 => $v2) {
                            $key .= 'OID:('.(int)$v2 . ')';
                        }
                    } else {
                        $key .= 'OID:('.(int)$v.')';
                    }
                    $key .= ']';
                }
    
    
                if (!empty($attributes)) {
                    $k .= base64_encode(serialize($attributes));
                }*/
    /*}
      $key .= '}';
      return $key;*/
}