How to retrieve custom attributes in Opencart?

Banjer picture Banjer · Jul 12, 2012 · Viewed 9.2k times · Source

I have defined some custom attributes and assigned them to products via the Opencart admin panel. I'm trying to retrieve these attributes upon checkout to adjust some shipping costs.

I'm currently working in the catalog/model/shipping/ups.php file in the getQuote function. I'm getting close with this:

print_r($this->cart->getProducts());

Which gives me the following product data:

Array
(
    [59] => Array
        (
            [key] => 59
            [product_id] => 59
            [name] => My Product Name 
            [model] => ABC
            [shipping] => 1
            [image] => data/myproduct.jpg
            [option] => Array
                (
                )

            [download] => Array
                (
                )

            [quantity] => 1
            [minimum] => 1
            [subtract] => 1
            [stock] => 1
            [price] => 29.99
            [total] => 29.99
            [reward] => 0
            [points] => 0
            [tax_class_id] => 0
            [weight] => 3.75
            [weight_class_id] => 5
            [length] => 0.00
            [width] => 0.00
            [height] => 0.00
            [length_class_id] => 3
        )

)

However, no custom attributes are returned. I'm sure there is a nice way to pass the product ID to an attribute-getting function, but I can't find it. The Opencart docs are a little lacking for the development side and no luck on Google.

Usually I'd grep the hell out of the whole directory structure to find what I'm looking for, but shell access is disabled on this particular web host :(.

EDIT

I believe Attributes are a newer feature that is built into Opencart. Its like a custom field.

Anyway, on the admin side I went to Catalog > Attributes and created a custom attribute called "Shipping Box Type". Then, under a specific product I can set the attribute. See screenshot. My goal is to retrieve the value of "Shipping Box Type". Does that answer your question @Cleverbot? I'm sure I can do a custom db query to grab it, but theres got to be a built-in function to grab attributes?

opencart product attributes

Answer

Banjer picture Banjer · Jul 14, 2012

Retrieve attributes for a given product_id

$this->load->model('catalog/product');

$attributes = $this->model_catalog_product->getProductAttributes(59); 

print_r($attributes);

Output

Array
(
    [0] => Array
        (
            [attribute_group_id] => 9
            [name] => Shipping Fields
            [attribute] => Array
                (
                    [0] => Array
                        (
                            [attribute_id] => 16
                            [name] => Shipping Box Type
                            [text] => SM2
                        )

                )

        )

)

Taking it further, here is an example of looping through the products currently in the cart to see what attributes they have:

$this->load->model('catalog/product');

$cart_products = $this->cart->getProducts();

// get attributes for each product in the cart
foreach($cart_products as $product) {
    $attributes = $this->model_catalog_product->getProductAttributes($product['product_id']);
    print_r($attributes);
}