• Resolved easycomtech

    (@easycomtech)


    Hi,

    I use this function for updating a custom field to each variations but after import, only the product with 2 or more variations are updated.
    If the product has only one variation, the custom field is not being updated.
    I’m importing XML and my variations are child XML elements
    I have disabled the option “Create products with no variations as simple products”, because I want all products to be variable

    add_action( ‘pmxi_saved_post’, ‘my_set_variations’, 10, 3 );

    function my_set_variations( $id, $xml, $u ) {
    $product = wc_get_product( $id );
    if ( ! $product ) return;
    if ( ! $product->is_type( ‘variable’ ) ) return;

    $record = json_decode( json_encode( (array) $xml ), 1 );

    $i = 0;

    while( array_key_exists( $i, $record[‘Variants’][‘Variant’] ) ) {

    if ( $product_id = wc_get_product_id_by_sku( $record[‘Variants’][‘Variant’][ $i ][‘sku’] ) ) {

    update_post_meta( $product_id, ‘some_custom_field’, $record[‘Variants’][‘Variant’][ $i ][‘custom_field_element’] );

    }
    $i++;

    }
    }

    xml file for test:

    <?xml version=”1.0″ encoding=”utf-8″?>
    <root>
    <node>
    <title>Parent</title>
    <sku>ParentSKU1</sku>
    <Variants>
    <Variant>
    <title>V1</title>
    <price>1</price>
    <color>Red</color>
    <sku>PRODUCTRED1</sku>
    <custom_field_element>A</custom_field_element>
    </Variant>
    <Variant>
    <title>V2</title>
    <price>2</price>
    <color>Green</color>
    <sku>PRODUCTGREEN1</sku>
    <custom_field_element>B</custom_field_element>
    </Variant>
    <Variant>
    <title>V3</title>
    <price>3</price>
    <color>Blue</color>
    <sku>PRODUCTBLUE1</sku>
    <custom_field_element>C</custom_field_element>
    </Variant>
    </Variants>
    </node>
    <node>
    <title>Parent2</title>
    <sku>ParentSKU2</sku>
    <Variants>
    <Variant>
    <title>V1</title>
    <price>1</price>
    <color>Red</color>
    <sku>PRODUCTRED2</sku>
    <custom_field_element>A</custom_field_element>
    </Variant>
    </Variants>
    </node>
    </root>

    Could you help me?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hi @easycomtech,

    The “variants” array is going to be structured differently when there’s only 1 variation compared to multiple. You’d have to do something like this to update the records with 1 variant:

    add_action( 'pmxi_saved_post', 'my_set_variations', 10, 3 );
    
    function my_set_variations( $id, $xml, $u ) {
        $product = wc_get_product( $id );
        if ( ! $product ) return;
        if ( ! $product->is_type( 'variable' ) ) return;
        
        $record = json_decode( json_encode( (array) $xml ), 1 );
        
        $i = 0;
        
        if ( count( $record['Variants']['Variant'] ) > 1 && ! array_key_exists( 'sku', $record['Variants']['Variant'] ) ) {
             while( array_key_exists( $i, $record['Variants']['Variant'] ) ) {
        
                if ( $product_id = wc_get_product_id_by_sku( $record['Variants']['Variant'][ $i ]['sku'] ) ) {
                
                update_post_meta( $product_id, 'some_custom_field', $record['Variants']['Variant'][ $i ]['custom_field_element'] );
            
                }
                $i++;
        
            }   
        } else {
            if ( $product_id = wc_get_product_id_by_sku( $record['Variants']['Variant']['sku'] ) ) {
                update_post_meta( $product_id, 'some_custom_field', $record['Variants']['Variant']['custom_field_element'] );
            }
        }
    }
    Plugin Author WP All Import

    (@wpallimport)

    Hey @easycomtech,

    I’m marking this thread as resolved since it’s been inactive for a few weeks now. Feel free to open a new topic if you still have questions.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Bug with one variation’ is closed to new replies.