Search code examples
phpprestashopprestashop-1.7

Get product features using feature_id and id_feature_value in prestashop 1.7


I want to customize shipping method using core files of prestashop 1.7 On the php I have an array features. In the product array the features are having the ids only. id_feature and id_feature_value. But i need to fetch the actual values of the feature name and value.

$products = Context::getContext()->cart->getProducts();
        foreach ($products as $key => $product) 
        {  
            foreach($product['features'] as $feature){
                $featuresvalue = FeatureValue::getFeatureValuesWithLang($this->context->language->id, $feature['id_feature']);
                echo "<pre>";
                print_r($featuresvalue['id_feature_value']);
                echo "/<pre>";
            }
       }

Here is the full product array

Array
(
    [id_product_attribute] => 0
    [id_product] => 1174
    [cart_quantity] => 100
    [id_shop] => 1
    [id_customization] => 
    [name] => Appalachian Flooring Canadian Solid Hardwood Red Oak Auburn 20 SqFt/Box Prestige
    [is_virtual] => 0
    [description_short] => Appalachian Flooring Canadian Solid Hardwood Red Oak Auburn (2-1/4" x 3/4" x RL) 20 SqFt/Box Prestige
    [available_now] => 
    [available_later] => 
    [id_category_default] => 23
    [id_supplier] => 0
    [id_manufacturer] => 44
    [manufacturer_name] => Appalachian Flooring
    [on_sale] => 0
    [ecotax] => 0.000000
    [additional_shipping_cost] => 0.00
    [available_for_order] => 1
    [show_price] => 1
    [price] => 5.8
    [active] => 1
    [unity] => 
    [unit_price_ratio] => 0.000000
    [quantity_available] => 0
    [width] => 0.000000
    [height] => 0.000000
    [depth] => 0.000000
    [out_of_stock] => 0
    [weight] => 61.4
    [available_date] => 2019-05-01
    [date_add] => 2020-03-30 17:23:02
    [date_upd] => 2020-05-27 16:06:25
    [quantity] => 100
    [link_rewrite] => canadian-solid-hardwood-red-oak-auburn-2-1-4-1
    [category] => hardwood
    [unique_id] => 000000117400000000001110
    [id_address_delivery] => 111
    [advanced_stock_management] => 0
    [supplier_reference] => 
    [customization_quantity] => 
    [reference] => 136-1194
    [ean13] => 
    [isbn] => 
    [upc] => 
    [minimal_quantity] => 10
    [wholesale_price] => 0.000000
    [id_image] => 1174-3020
    [legend] => 
    [reduction_type] => 0
    [is_gift] => 
    [reduction] => 0
    [reduction_without_tax] => 0
    [price_without_reduction] => 6.554
    [specific_prices] => 
    [stock_quantity] => 0
    [price_without_reduction_without_tax] => 5.8
    [price_with_reduction] => 6.554
    [price_with_reduction_without_tax] => 5.8
    [total] => 580
    [total_wt] => 656
    [price_wt] => 6.554
    [reduction_applies] => 
    [quantity_discount_applies] => 
    [allow_oosp] => 1
    [features] => Array
        (
            [0] => Array
                (
                    [id_feature] => 1
                    [id_product] => 1174
                    [id_feature_value] => 475
                )

            [1] => Array
                (
                    [id_feature] => 2
                    [id_product] => 1174
                    [id_feature_value] => 53
                )

            [2] => Array
                (
                    [id_feature] => 38
                    [id_product] => 1174
                    [id_feature_value] => 357
                )

            [3] => Array
                (
                    [id_feature] => 38
                    [id_product] => 1174
                    [id_feature_value] => 358
                )

            [4] => Array
                (
                    [id_feature] => 40
                    [id_product] => 1174
                    [id_feature_value] => 360
                )

            [5] => Array
                (
                    [id_feature] => 40
                    [id_product] => 1174
                    [id_feature_value] => 377
                )

            [6] => Array
                (
                    [id_feature] => 40
                    [id_product] => 1174
                    [id_feature_value] => 378
                )

            [7] => Array
                (
                    [id_feature] => 41
                    [id_product] => 1174
                    [id_feature_value] => 379
                )

            [8] => Array
                (
                    [id_feature] => 42
                    [id_product] => 1174
                    [id_feature_value] => 362
                )

            [9] => Array
                (
                    [id_feature] => 42
                    [id_product] => 1174
                    [id_feature_value] => 363
                )

            [10] => Array
                (
                    [id_feature] => 43
                    [id_product] => 1174
                    [id_feature_value] => 365
                )

            [11] => Array
                (
                    [id_feature] => 44
                    [id_product] => 1174
                    [id_feature_value] => 380
                )

            [12] => Array
                (
                    [id_feature] => 44
                    [id_product] => 1174
                    [id_feature_value] => 384
                )

            [13] => Array
                (
                    [id_feature] => 45
                    [id_product] => 1174
                    [id_feature_value] => 427
                )

            [14] => Array
                (
                    [id_feature] => 46
                    [id_product] => 1174
                    [id_feature_value] => 428
                )

        )

    [rate] => 0
    [tax_name] => 
    [price_without_specific_price] => 5.8
)

But i am not getting the values. Anyone know how to fetch the values? Thanks in advance


Solution

  • Try something like this to add the value of the feature in the array of features

           $productsCart = Context::getContext()->cart->getProducts();
         
            foreach ($productsCart as $product) {
            foreach ($product['features'] as &$feature) {    
                    $featureValueObj = new FeatureValue($feature['id_feature_value'],Context::getContext()->language->id);
                    $feature['value']= $featureValueObj->value;
    
                echo "<pre>";
                print_r($product['features']);
                echo "</pre>";
               }
             }