Bug with one variation
-
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 variableadd_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?
- The topic ‘Bug with one variation’ is closed to new replies.