• Resolved levelzwo

    (@levelzwo)


    I’m kind of freaking out with this issue.

    We have a huge CSV file and the products should be automatically imported with the rest api.

    Everything works fine (Thank you very much, that’s pretty awesome):
    – the product has been created
    – all variations, attributes, price etc. are succesfully imported
    – the images were uploaded succesfully and they’ve also been set to the gallery (with title and alt attributes)

    but we still have one issue:
    – Setting image[‘position’] = 0 to featured image is not working although the image have also been succesfully uploaded.

    All other positions are working perfect, it’s only about position = 0. Everything is in his place, but the connection between image and featured image of the product is missing/not working.

    Notes (if it helps to find the solution):
    – in some cases the position=0 image is uploaded AFTER position = 1 (if it’s important)
    – functions that handle this feature (class-wc-api-products.php):
    create_product (250) -> save_product_images (2259) -> upload_image_from_url (2348) -> set_uploaded_image_as_attachment (2437) -> set_post_thumbnail (called in line 2279)

    <?php
    
    	$url = "https://myshop.com/";
    	$ck = "consumer_key";
    	$cs = "consumer_secret";
    
    	require_once( 'lib/woocommerce-api.php' );
    
    	$options = array(
    		'debug'           => true,
    		'timeout'         => 30,
    		'ssl_verify'      => false,
    	);
    
    	try {
    
    		$client = new WC_API_Client( $url, $ck, $cs, $options );
    		$data = array(
    			//'product' => array(
    		        'title' => 'Notebook',
    		        'type' => 'simple',
    		        'regular_price' => '21.99',
    		        'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
    		        'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
    		        'categories' => array("Notebook"),
    		        'images' => array(
    		            array(
    		                'src' => 'https://www.it-guenstiger.net/uploads/ipad_1.png',
    		                'position' => 0,
    		                'title' => 'Notebook',
    		                'alt' => 'Notebook'
    		            ),
    		            array(
    		                'src' => 'https://www.it-guenstiger.net/uploads/home-pc-system.png',
    		                'position' => 1,
    		                'title' => 'Notebook',
    		                'alt' => 'Notebook'
    		            )
    		        )
    		     //)
    		);
    
    		print_r($client->products->create($data));
    
    	} catch ( WC_API_Client_Exception $e ) {
    
    		echo $e->getMessage() . PHP_EOL;
    		echo $e->getCode() . PHP_EOL;
    
    		if ( $e instanceof WC_API_Client_HTTP_Exception ) {
    
    			print_r( $e->get_request() );
    			print_r( $e->get_response() );
    		}
    	}
    
    	?>

    I’m testing the code with kloon’s rest api client: https://github.com/kloon/WooCommerce-REST-API-Client-Library (changed kloon’s version to v3)

    WordPress v4.4.2
    WooCommerce v2.5.2
    nginx v1.8

    Does someone have the same issue or a hint to fix this?

    https://www.ads-software.com/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Thread Starter levelzwo

    (@levelzwo)

    Resolved.

    The problem was caused by the W3 Total Cache Plugin -> turned of databse cache.

Viewing 1 replies (of 1 total)
  • The topic ‘REST Api v3: create product -> unable to set product featured image’ is closed to new replies.