Taking action after the product is added to the system

Pending

Import WP Addon - WooCommerce Updated 1 week ago 1 Reply

Muenker asked 2 weeks 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

1 week 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