We are currently on vacation until 21st July 2025, ticket responses maybe be longer than normal during this period, sorry for any inconvenience.

Taking action after the product is added to the system

Closed

Import WP Addon - WooCommerce Updated 6 months ago 2 Replies

Muenker asked 7 months ago on January 6, 2025 at 6:44 am

Hello,

I want to update a certain field immediately after the product is added to the site from XML. For example; I want to add “_1234” to the end of the stock code or I want to write a barcode to the GTIN field.

How do I do this? Is there a hook that I can catch this?

J

James replied Support Agent

7 months ago on January 9, 2025 at 9:37 pm

Hi Muenker,

Similar to the other question, the following save method is called after a record is imported, from within the method you can access the imported data.

/**
 * Initialize the ImportWP addon after it has been initialized but not loaded.
 */
add_action('plugins_loaded', function () {

    class IWPHD1016_ImporterAddon extends \ImportWP\Common\AddonAPI\ImporterAddon
    {
        protected function can_run()
        {
            // uncomment the following line, to only run on the woocommerce template
            // return $this->get_template_id() == 'woocommerce-product';

            // uncomment the following line, to only run on a specific importer based on its id e.g. http://localhost:8888/wp-admin/tools.php?page=importwp&edit=16&step=4
            //return $this->get_importer_id() == 16;

            // this will run on every importer
            return true;
        }

        public function save($data)
        {
            // get importer data class
            $parserData = $data->get_data();

            // fetch key value paired data
            $dataGroup = 'default';
            $rowData = $parserData->getData($dataGroup);
            $post_title = $rowData['post_title'];

            // Uncomment to fetch a single value from a data group
            // $post_title = $parserData->getValue('post_title', $dataGroup);

            wp_insert_post([
                'ID' => $data->get_id(),
                'post_title' => '123-' . $post_title
            ]);
        }
    }

    new IWPHD1016_ImporterAddon();
}, 9);

You can view a list of all template data groups by using: $parserData->getGroupKeys()

$parserData->getGroupKeys()

For example if you wanted to get the stock code from the woocommerce template, you would get this from the inventory group.

$regular_price = $parserData->getValue('price._regular_price', 'price');
$sku = $parserData->getValue('inventory._sku', 'inventory');

James

J

James replied Support Agent

6 months ago on January 20, 2025 at 12:44 pm

Hello Muenker

This ticket is being marked as closed due to inactivity.

If this is not the case, please reply to this ticket to re-open it.

James