From simple entries to variable

Closed

Import WP Addon - WooCommerce Updated 5 months ago 38 Replies

George asked 5 months ago on June 25, 2024 at 8:50 am

Hi James,

How can I ensure that a simple entry from an xml is updating a woocommece variation?

 

For example this code: 455-WD70C-400 9tha is a simple product entry in xml)

 

it is the first variation: https://fshome.gr/product/devon-iwis-wd-400/

 

But when I run the importer the variation price is not updating becuse it is variable product.

 

This product that is at the same xml https://fshome.gr/product/fossil-jade-marble-ms50-410/ is updating correctly

GF

George replied

5 months ago on June 25, 2024 at 11:21 am

The way it is identified is with a custom code for ACF to add a custom field at the variation of the variable product

 

// Display custom field in product variation

add_action('woocommerce_product_after_variable_attributes', function ($loop, $variation_data, $variation) {

    woocommerce_wp_text_input([

        'id' => 'suppliercode_' . $loop,

        'name' => 'suppliercode[' . $loop . ']',

        'label' => 'Custom Field (suppliercode)',

        'value' => get_post_meta($variation->ID, 'suppliercode', true)

    ]);

}, 10, 3);

 

// Save custom field value

add_action('woocommerce_save_product_variation', function ($variation_id, $i) {

    if (isset($_POST['suppliercode'][$i])) {

        update_post_meta($variation_id, 'suppliercode', sanitize_text_field($_POST['suppliercode'][$i]));

    }

}, 10, 2);

 

J

James replied Support Agent

5 months ago on June 25, 2024 at 1:26 pm

Hi George,

How have you setup this field in the importer? Can you please export and attach your importer settings to this ticket by going to Tools > Import WP > Settings / Tools > Import / Export and select and export your importer as a JSON file.

James

GF

George replied

5 months ago on June 25, 2024 at 2:51 pm

J

James replied Support Agent

5 months ago on June 25, 2024 at 9:24 pm

Thanks,

based on your importer config i can see that you have not setup a custom field for 'suppliercode', the way you defined the field in code means that it is not an acf field and just a normal post meta field.

In the importer you would need to set the custom field name manually to 'suppliercode'

GF

George replied

5 months ago on June 26, 2024 at 9:29 am

Hi James,

 

You mean to choose something of the below value? Because none of 3 worked.

https://prnt.sc/yrxE4XKRrj5A

https://prnt.sc/y03z__AaEZzO 

 

Same goes here: 

https://prnt.sc/za6qANN1Xv4Y

J

James replied Support Agent

5 months ago on June 26, 2024 at 1:22 pm

Hi, 

I have tested this locally and it updates the suppliercode field on the variation, for reference i did the following:

I have added your custom code to register the suppliercode field onto a variable product with a set value of 123456 (shown in the attached screenshot).

Looking in the wordpress database under the postmeta table for the post of id 216, i can see the value stored against the meta key: suppliercode and the value of 123456 (also attached).

Now replicating this in the importer as a very basic example using the following CSV Containing 1 record that references the previously mentioned id of 216:

ID, Suppliercode
216, ABCDEF

On the template fields screen, i would enable the Product id field, and set this to {0} which is 216, and under the custom fields section i would add a record with the name "suppliercode" , type text and value of {1}.

Under the permissions tab, i would use the "Select a template field to be used", and select the "ID" template field.

Under the restrict which fields can be imported section, i would only enable "update", and set the import to "Only the following fields", and select the "custom_fields" : "suppliercode" checkbox.

Now running the importer updates the custom field on the variable product with id 216.

I have attached screenshots to show the bulk of the importer setup previously mentioned.

GF

George replied

5 months ago on June 26, 2024 at 3:29 pm

Is is possible to test this xml?

 

I am doing the same steps and I cannot replicate it. 

For example I have this product variable:

https://staging.fshome.gr/product/devon-iwis-decor-walk-in-mat-mauro/

 

with variations (suppliercode):

455-IW70DC-400

455-IW80DC-400

455-IW90DC-400

455-IW100DC-400

 

I cannot target them for some reason.

 

I get empty values at the template field:

path: https://prnt.sc/uoK6znU42BVz

product id: https://prnt.sc/GcqwGguieJXI

suppliercode: https://prnt.sc/wFtizUa5-7BU

 

J

James replied Support Agent

5 months ago on June 26, 2024 at 3:35 pm

In your example you are using an xml file and not csv so the xml selector will not be {1}, as this is refenencing the second column in the csv file.

Can you explain using your attached xml file what you are trying to do. and what you are trying to set.

what i have taken from your previous responses, is you have a product variation on a variable product that has the supplier code: 455-IW70DC-400 , and if the importer ran it would update that variation with the price: 210 ? does this sound correct.

J

James replied Support Agent

5 months ago on June 27, 2024 at 10:04 am

Can you check the history tab of the importer for the row that matches the example variation to see what it says.

GF

George replied

5 months ago on June 27, 2024 at 5:45 pm

Not enough permissions to create record.

GF

George replied

5 months ago on June 29, 2024 at 7:43 am

Could it be an issue with the iwp custom plugin? that checks if the tag or product brand exists?

J

James replied Support Agent

5 months ago on July 1, 2024 at 8:29 pm

Hi,

The custom plugin, looking through the code should not effect this.

The message "Not enough permissions to create record", suggests that the supplier code does not match which is why it is trying to insert a record.

can you compare this with the previous record from the screenshot 355-048-400 and see if there is anything different? is that also a product variation?

James

 

GF

George replied

5 months ago on July 2, 2024 at 8:57 am

The difference is that this is simple product https://fshome.gr/product/carron-048-400/ 

 

J

James replied Support Agent

5 months ago on July 2, 2024 at 9:37 am

Using the custom field viewier: https://www.importwp.com/docs/how-to-import-wordpress-custom-fields/#find-existing-custom-fields , can you select the product variation that is not being updated / found by the importer, and take a screenshot of all the custom fields that are displayed.

GF

George replied

5 months ago on July 2, 2024 at 12:20 pm

https://prnt.sc/NYSff_i0RXLg it seems correct

J

James replied Support Agent

5 months ago on July 2, 2024 at 1:47 pm

Can you download the xml file and attach from the feed, and test with that xml file by choosing file upload instead of remote file, so that i can try to replicate it locally with the exact same supplier code.

Can you then export and attach your importer settings to this ticket by going to Tools > Import WP > Settings / Tools > Import / Export and select and export your importer as a JSON file.

Can you also export your current products via Tools > Import WP > Exporters > Add exporter, and select WooCommerce Products:

Unique ID: ID
File Type: CSV
Leave Fields to export empty.

And then click save and run, and download and attach the generated csv file.

GF

George replied

5 months ago on July 2, 2024 at 8:13 pm

Yes here they are.

GF

George replied

5 months ago on July 2, 2024 at 8:24 pm

the csv file is 158mb do you have any email to send it via wetranser?

GF

George replied

5 months ago on July 2, 2024 at 8:28 pm

Sent it at support@helpdesk.importwp.com

J

James replied privately Support Agent

5 months ago on July 2, 2024 at 8:42 pm

GF

George replied

5 months ago on July 2, 2024 at 8:44 pm

Yes I did it (it is uploading)

J

James replied Support Agent

5 months ago on July 2, 2024 at 9:34 pm

Thanks, at the bottom of the exporter can you click on the "Download importer config", and attach

GF

George replied

5 months ago on July 2, 2024 at 9:38 pm

J

James replied Support Agent

5 months ago on July 3, 2024 at 12:51 pm

Thanks George,

After testing locally i cant seem to replicate, can we try the following (i have reduced the number of records in the xml file to a single record with the ID: 455-IW70DC-400, this is the supplier code of a product variation?).

1. Enable debug log via Import WP > Settings / Tools > General Settings > "Enable debug mode".
2. Once enabled, upload the attached xml file, and run the import.
3. Once complete, at the bottom right of the importer there should be a button "Show Debug", where you can view the debug log and download it, then attach it here.
4. Repeat step 1 and disable the debug log.

GF

George replied

5 months ago on July 3, 2024 at 6:15 pm

GF

George replied

5 months ago on July 3, 2024 at 6:19 pm

2. file settings https://prnt.sc/BdgEB6kiT_Wc

3. Template Fields https://prnt.sc/b6GTYGrqAAmd + https://prnt.sc/A0dKeKgonSaC

4. Permissions https://prnt.sc/OCgUiKKbjAOl + https://prnt.sc/fDJEhsdVeZ0n

5. Run import https://prnt.sc/tKvSvHvd-bc_

J

James replied Support Agent

5 months ago on July 3, 2024 at 8:21 pm

The log shows that your find query also has the extra tax search, this will be the reason for it not being able to find the record to update:

"tax_query":[{"taxonomy":"product_tag","field":"name","terms":"baklatsidis"}]

GF

George replied

5 months ago on July 3, 2024 at 8:28 pm

Yes but this tag exists https://prnt.sc/tj8MXjyoXxWL (and it exists at all products from this xml, all simple have this tag and it works normally). maybe we have to add something to work also for variation at the custom plugin?

J

James replied Support Agent

5 months ago on July 3, 2024 at 8:40 pm

Variable products will have the tag, but the product variation will not.

You would need to write some custom SQL JOIN's across multiple tables if the post_type = product_variation to check if the parent product has that tag.

GF

George replied

5 months ago on July 3, 2024 at 8:51 pm

If I make it to check product brands, will it be solved? Or not?

J

James replied Support Agent

5 months ago on July 3, 2024 at 8:54 pm

No, because only the variable product has taxonomies not the variation

GF

George replied

5 months ago on July 3, 2024 at 9:03 pm

so the fast solution is to remove the script that checks for the product_tags?

J

James replied Support Agent

5 months ago on July 3, 2024 at 9:06 pm

Yes, removing the taxonomy query would allow the product variation to be found.

J

James replied Support Agent

5 months ago on July 3, 2024 at 9:26 pm

To be able to keep your product_tag query what you would need to do is update the custom plugin by adding the following function to the end of the file:

function iwp_also_check_parent_taxonomies($query)
{
    $query['join'] = " LEFT JOIN wp_term_relationships ON (
        (wp_posts.post_type = 'product_variation' AND wp_posts.post_parent = wp_term_relationships.object_id)
        OR (wp_posts.post_type = 'product' AND wp_posts.ID = wp_term_relationships.object_id)
        ) 
        INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )";

    remove_filter('posts_clauses', 'iwp_also_check_parent_taxonomies');

    return $query;
}

Then to use this code you would find the if statement that matches your query e.g.

} elseif (iwp()->importer->getId() == 504287) {
        $query_args['tax_query'] = [
            [
                'taxonomy' => 'product_tag',
                'field' => 'name',
                'terms' => 'baklatsidis',
            ]
        ];
    
    }

and update it so that the filter is included.

} elseif (iwp()->importer->getId() == 504287) {
// add filter to use parent id for taxonomy relationship
add_filter('posts_clauses', 'iwp_also_check_parent_taxonomies'); $query_args['tax_query'] = [ [ 'taxonomy' => 'product_tag', 'field' => 'name', 'terms' => 'baklatsidis', ] ]; }
GF

George replied

5 months ago on July 3, 2024 at 11:04 pm

It worked!! Thanks :) your support is amazing