Woocommerce: Moving Gtins lists to a different field?

Started by Dom, Jul 30, 2024, 08:18 AM

Previous topic - Next topic

Dom

Hey everyone,

I'm currently managing an online store and have been using the EAN for WooCommerce plugin to store GTIN numbers for each product. These are stored in the database under the _woosea_gtin field. Recently, I activated the Yoast SEO for WooCommerce plugin, which also allows you to store GTINs but in a different field.

I'm planning to cut down on my plugins to enhance my site's performance, so I want to deactivate the EAN for WooCommerce plugin. However, before I do that, I need to migrate all GTIN entries from _woosea_gtin to the field that Yoast uses.

Has anyone done this before or knows a way to efficiently move all these entries within the database? Any SQL queries or tips would be greatly appreciated. Thanks in advance!

tech_wiz

Hi there,

I've had to do something similar a few months ago. Here's a step-by-step guide to help you migrate your GTIN data from _woosea_gtin to the field used by Yoast SEO for WooCommerce.

First, you'll need to find out the meta key that Yoast SEO uses to store GTINs. This could be something like _yoast_gtin or similar. You can check this within the plugin's settings or documentation.

Before making any changes, always backup your database. This is crucial to avoid any irreversible damage. You can use phpMyAdmin or any database management tool you prefer.

Run SQL Query: Here's a SQL query that you can run in your database to copy data from _woosea_gtin to the Yoast GTIN field:

UPDATE wp_postmeta
 SET meta_value = (
     SELECT meta_value
     FROM wp_postmeta AS pm2
     WHERE pm2.post_id = wp_postmeta.post_id
     AND pm2.meta_key = '_woosea_gtin'
 )
 WHERE meta_key = '_yoast_gtin';

This query assumes that the meta key used by Yoast is `_yoast_gtin`. Replace it with the actual meta key if it's different.

After running the query, check a few product entries to ensure that the GTINs have been correctly copied to the new field.

Deactivate the EAN Plugin: Once you've confirmed the data migration, you can safely deactivate the EAN for WooCommerce plugin.

Test Your Site: Finally, do some testing to make sure everything works as expected. Check product pages, product feeds for ads, perform searches, and ensure that Yoast SEO is correctly recognizing the GTINs.

Hopefully this helps. If you run into any issues or need further clarification, feel free to ask!

Best regards.  8)

Jack

You could probably just rename the database column from _woose_gtin to what you want instead.

Bill90

To make Yoast SEO store GTINs in the _woosea_gtin field instead of its default field, you'll need to use some custom code.

You can add custom code in your themes functions.php file to hook into Yoast's save process for product metadata. This code will ensure that GTINs are stored in the _woosea_gtin field.

Here is an example of how you can achieve this:

// Hook into the save_post action
add_action('save_post', 'save_custom_gtin_field');

function save_custom_gtin_field($post_id) {
    // Check if the post type is 'product'
    if (get_post_type($post_id) == 'product') {
        // Check if the Yoast GTIN field is set
        if (isset($_POST['_yoast_wpseo_gtin'])) {
            // Get the GTIN value
            $gtin = sanitize_text_field($_POST['_yoast_wpseo_gtin']);
            // Save the GTIN value into the _woosea_gtin field
            update_post_meta($post_id, '_woosea_gtin', $gtin);
        }
    }
}

If you want to ensure that the _woosea_gtin field is always in sync with Yoast's GTIN field, you can add additional code to update both fields whenever one is changed.

// Hook into the save_post action for syncing fields
add_action('save_post', 'sync_gtin_fields');

function sync_gtin_fields($post_id) {
    // Check if the post type is 'product'
    if (get_post_type($post_id) == 'product') {
        // Get the GTIN values from both fields
        $yoast_gtin = get_post_meta($post_id, '_yoast_wpseo_gtin', true);
        $woosea_gtin = get_post_meta($post_id, '_woosea_gtin', true);

        // Update the _woosea_gtin field if it's different from Yoast's field
        if ($yoast_gtin && $yoast_gtin !== $woosea_gtin) {
            update_post_meta($post_id, '_woosea_gtin', $yoast_gtin);
        }

        // Optionally, update Yoast's field if it's different from _woosea_gtin field
        if ($woosea_gtin && $woosea_gtin !== $yoast_gtin) {
            update_post_meta($post_id, '_yoast_wpseo_gtin', $woosea_gtin);
        }
    }
}

 ;D