Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Translating custom meta fields + import categories #196

Open
Lockoloop opened this issue Mar 14, 2022 · 0 comments
Open

Translating custom meta fields + import categories #196

Lockoloop opened this issue Mar 14, 2022 · 0 comments

Comments

@Lockoloop
Copy link

Hi,
So I have two tiny problems with the plug-in:

  • first, exporting and importing the products CSV means that I end up with a new category for every product I reimported (if I update 10 products of the Spices & Herbs category, then I end up with 10 categories called Spices & Herbs): my website is in English and Dutch, I tried importing the CSV using the English category name, the Dutch name, and [:en]/[:nl] tags, and it happens in every case.
  • second, I created some custom meta fields for my products (such as ingredients, storage, usage etc.), and they display correctly in both languages if I import everything from a CSV using the [:en]/[:nl] tags, but as soon as I edit them in Woocommerce product page, the new value overwrites both languages.
    I guess this might have to do with the way I save out the meta fields, but I haven't been able to figure out how to do it so it works:
<?php

// Create additional info Tab
add_filter('woocommerce_product_data_tabs', 'LCLPC_additional_product_settings_tabs');
function LCLPC_additional_product_settings_tabs($tabs)
{
    $tabs['additionnal'] = array(
        'label'    => 'Additional info',
        'target'   => 'additional_product_data',
        'class' => ['hide_if_external'],
        'priority' => 21
    );
    return $tabs;
}

// Additional info Tab content in back-end
add_action('woocommerce_product_data_panels', 'additional_product_panels');
function additional_product_panels()
{
    echo '<div id="additional_product_data" class="panel woocommerce_options_panel hidden">';
	
    woocommerce_wp_textarea_input(array(
        'id'          => 'usage',
        'value'       => get_post_meta(get_the_ID(), 'usage', true),
        'label'       => 'Usage',
    ));
	
    woocommerce_wp_textarea_input(array(
        'id'          => 'storage',
        'value'       => get_post_meta(get_the_ID(), 'storage', true),
        'label'       => 'Storage',
    ));
    woocommerce_wp_text_input(array(
        'id'          => 'allergens',
        'value'       => get_post_meta(get_the_ID(), 'allergens', true),
        'label'       => 'Allergen warnings',
    ));
	
    woocommerce_wp_textarea_input(array(
        'id'          => 'ingredients',
        'value'       => get_post_meta(get_the_ID(), 'ingredients', true),
        'label'       => 'Ingredients',
    ));

    woocommerce_wp_text_input(array(
        'id'          => 'supplier',
        'value'       => get_post_meta(get_the_ID(), 'supplier', true),
        'label'       => 'Supplier',
    ));

    woocommerce_wp_text_input(array(
        'id'          => 'origin',
        'value'       => get_post_meta(get_the_ID(), 'origin', true),
        'label'       => 'Origin',
    ));

    echo '</div>';
}

//Save
 add_action('woocommerce_process_product_meta', function ($post_id) {
    $product = wc_get_product($post_id);
    $product->update_meta_data('usage', sanitize_text_field($_POST['usage']));
	$product->update_meta_data('ingredients', sanitize_text_field($_POST['ingredients']));
	$product->update_meta_data('allergens', sanitize_text_field($_POST['allergens']));
    $product->update_meta_data('supplier', sanitize_text_field($_POST['supplier']));
    $product->update_meta_data('storage', sanitize_text_field($_POST['storage']));
    $product->update_meta_data('origin', sanitize_text_field($_POST['origin']));

    $product->save();
});

Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant