Jump to content

Add custom or pre-defined feature value programmatically in PHP


hakeryk2
 Share

Recommended Posts

Hello devs,

How to add custom (not-predefined) product feature to feature group in already existing  product? For example I have:

Product = Pretty furniture; and Feature group = Furniture material

How to add to this Furniture material group custom value like "asdasdasd" in PHP?

I tried with Product::addFeaturesToDB and Product::addFeatureProductImport but with no luck :(

 

Can anyone provide step by step solution how to add custom value to feature?

 

 

Edited by hakeryk2 (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

Hello @hakeryk2 I heard You have problem with this and I will help You because You are such a great person. Here is a solution to your concern.

If You will digg in into AdminImportController.php You will find out that there is a option to import features. Just grab it from there and adjust it to your needs or create a complete new function that You will be using with ease in future. Here it is. Just put into override Product.php

 

 /**
     * Adds a feature value to a Product. If You want to have feature selected from dropdown leave $custom to false.
     * @param int|$id_product
     * @param int|$id_feature_group
     * @param string|$feature_value
     * @param bool|true $custom
     * @param int|$id_lang
     * @return bool
     */
    public static function setFeatureValueToProduct($id_product, $id_feature_group, $feature_value, $custom = false, $id_lang)
    {
        $group_feature_obj = Feature::getFeature($id_lang, $id_feature_group);

        if ($group_feature_obj){
            $feature_name = $group_feature_obj['name'];
            $feature_value = $feature_value ? trim($feature_value) : '';
            $position = false;

            if (!empty($feature_name) && !empty($feature_value)) {
                $id_feature = (int)FeatureCore::addFeatureImport($feature_name, $position);

                $id_feature_value = (int)FeatureValue::addFeatureValueImport($id_feature, $feature_value, $id_product, $id_lang, $custom);
                if (Product::addFeatureProductImport($id_product, $id_feature, $id_feature_value)){
                    Feature::cleanPositions();
                    return true;
                } else {
                    return;
                }
            }
        } else {
            return;
        }
    }

Example of use:


Product::setFeatureValue(1597, 15,  'Dracarys', false, 1);

 

It was really nice to help You, have a good luck in future.

 

  • Like 3
  • Haha 4

Share this post


Link to post
Share on other sites

  • 11 months later...
  • 6 months later...

Hi,

that seems to be a very nice solution.

I'd like to use it but don't know where to put and call this code.

I guess I should paste your function "setFeatureValueToProduct" in override/controllers/front/ProductController.php to override the genuine file??

My goal is to create a PHP script in root folder ex: www/testFeature.php to loop on older tables for get features and assign them to new products.
Is there a way call setFeatureValueToProduct function in my testFeature.php? Or should i do it diffetently?

Thanks a lot for your help.

Pierre

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More