Importing custom product values

Hi Mark,
good example :grinning: but I have tried to harden it a little bit so here is my product data from a PHP script:

    [
        'sku' 			=> 'test2',
        'name' 			=> 'Big TV',
	'description'	=> 'ma tEsEt descr',
        'price' 		=> 15000, // in cents
        'sale_price' 	=> 129900,
        'sale_price_from' 	=> '2019-08-07 18:11:31+02:00',
        'sale_price_until' 	=> '2019-10-01 01:00:00+02:00',
        'stock' 		=> 14,
        'weight' 		=> 15.50,
        'weight_unit' 	=> 'kg',
	'length_unit'	=> 'cm',
        'image' 		=> '/assets/components/bob/img/logo.svg',
	'properties' 	=> serialize([
		    'product_with_alcohol' => 0,
		    'extra_details' 		=> [
		            'image_alt_0' 	=> 'abc',
		            'image_1' 		=> '/assets/products/images/ast.jpg',
		            'image_alt_1' 	=> '#$%^',
		            'image_2'		=> '/assets/products/images/xpink.jpg',
		            'image_alt_2' 	=> '12',
		            'image_3' 		=> '/assets/products/images/hat.jpg',
		            'image_alt_3' 	=> '',
		            'image_4'		=> '',
		            'image_alt_4' 	=> '',
		            'image_5' 		=> '',
		            'image_alt_5' 	=> '',
		            'video_url' 	=> '',
		            'youtube_id' 	=> '',
				]
		]),
		'width'		=> 12.50,
		'height'	=> 11.50,
		'depth'		=> 10.50,


    ],

but you know it does not update width, height, depth, properties fields. In your script I have found a very important comment “// This assumes the source data has the right keys” and I was taking the fields from the Mysql query: DESCRIBE modx_commerce_product which has assured me I was on the right way.

Do you know what I was doing wrong, please?

It looks like your closing the properties serialize function before width, height and depth, excluding those from the properties array. If you move the bracket ) behind depth it should work:

      'youtube_id' => '',
    ],
    'width'  => 12.50,
    'height' => 11.50,
    'depth'  => 10.50,
  ]),
],

Hi, OK but width, height, depth, properties are the separate fields in mysql table xxxxx_commerce_product


that’s why my code looks like this: gist abstract

So did you create a custom product class or did you just add those to the default commerce_product table?

Hi,
OK, the width, height, depth fields could be custom fields created from an Extra or another dev and I was thinking the properties fields is one of the default fields as well. So, none of the mentioned fields is the default and an additional qry needs to be executed once the main qry created a product, right?

I’m assuming you have a custom commerce class setup there so it’s difficult to give advice without knowing more details. I would suggest you open a separate thread with more details for your specific question.

1 Like

properties is a standard field, the width/height/depth are not. Assuming those fields were implemented “correctly” (this whole approach is something we don’t recommend) as a custom product type, you’ll need to make sure that the $modx->newObject('comProduct') line is updated with your custom product name.

1 Like