Copy Product Feature Into Other Languages

Hi. CS-Cart 4.15

Question is simple: How can I make the product feature take the same value for all languages by default? (I.e until you change it to something else for a specific language.)

When you create a product feature in a multi language instance, that feature is created for all languages with the same value until you change them for the other languages. Which is pretty expected and good behavior.

This behavior is also the same when it comes to fill-in the feature variants.

But when it comes to "additional information" fields, this behavior changes. When you add a product and fill in the product features in a language, it works only for that language. As an end user, when you are browsing the site in main language, you see all the features of the product are present; but when you change the language, more than half of the product features become absent; because you didn't fill that feature for each of the languages.

This behavior should be the same with the first two. As a real life example, say you create a feature for "MPN". It will show the MPN only for the main language, while it won't change in other languages for sure. So as an admin, you need to edit this product, switch all the languages one by one and copy the very same content to the very same feature box.

Instead it should take the value as default unless you change it to something else in another language. This is how features work anyway in the first place. So why would its values work different way?

This difference in behavior also effect Google Merchant exports pretty badly. If you have for example 5 languages, you have to save each of your products 5 times to get your product listed in Google Merchant. (Through rich snippets.)

Now, is there a work around for this?

1 Like

Hello

Yes. You are right. If you want to change this behavior plese look into /app/functions/fn.features.php

if ($feature_type !== ProductFeatures::TEXT_FIELD) { // feature values are common for all languages, except text (T)

foreach (Languages::getAll() as $i_data['lang_code'] => $_d) {
db_replace_into('product_features_values', $i_data);
}
} else { // for text feature, update current language only
$i_data['lang_code'] = $lang_code;
db_query('INSERT INTO ?:product_features_values ?e', $i_data);
}
You can connect to the hook "update_product_features_value_post" in your addon and achieve the effect you need.
Best regards
Robert

Thank you for your time. Looking at the code, I fear that this way you will always have a standard value even if you want to change it to something else for another language, is that correct? I mean it will by default fill-in the feature value for all languages, which is what I asked; but what if you want to change the feature value to something else for a specific language? Looking at the code I think it won't let you make distinctive changes between languages, am I missing something?

Hello

I think you can achieve the effect that you need to write a piece of code in a hook "update_product_features_value_post" We will post some code later or maybe someone on the forum will do it.

Best regards

Robert

Hello

The code in the hook could look like this:

foreach ($product_features as $f_feature_id => $f_feature_value) {
        $f_feature_info = fn_get_feature_data_with_subfeatures($f_feature_id, $lang_code);
        if($f_feature_info['feature_type'] == "T") {
            $existing_langs = db_get_fields("SELECT lang_code FROM ?:product_features_values WHERE feature_id = ?i AND product_id = ?i", $f_feature_id, $product_id);
            foreach (Languages::getAll() as $f_lang_code => $f_d) {
                if(!in_array($f_lang_code, $existing_langs)) {
                    $structure = array(
                        "feature_id" => $f_feature_id,
                        "product_id" => $product_id,
                        "variant_id" => 0,
                        "value" => $f_feature_value,
                        "lang_code" => $f_lang_code
                    );
                db_query('INSERT INTO ?:product_features_values ?e', $structure);
            } // end if
        } // end foreach
    } // end if
} // end foreach

Not tested!

Best regards

Robert

1 Like