0

I'm using the SOAP web service to add images to products after I import them. Now before I want to add the image to the specific product, I want to delete all the images of that product. But the SOAP service wants me to specify the name of the image. This is randomly generated so I don't have the name of the image for each product.

Ho do I delete all the images of 1 product with a SOAP call.

Here's a part of the code:

foreach ($array as $prod) {
            $sku = $prod[0];

            $photo = $prod[8];
            $data = file_get_contents($photo);
            $base64 = base64_encode($data);

            $file = array(
                'content' => $base64,
                'mime' => 'image/jpeg'
            );

            $resultDelete = $client->catalogProductAttributeMediaRemove($sessionId,(string)$sku, '');
            $result = $client->catalogProductAttributeMediaCreate($sessionId, (string)$sku, array('file' => $file, 'label' => $sku, 'position' => '1', 'types' => array('thumbnail', 'image', 'small_image')), '', 'sku');

            print_r($resultDelete . ' - ' . $result . '<br>');

        }

As you can see, what do I need to add to the parameters of

catalogProductAttributeMediaRemove

to make it delete all the images of that product instead of one?

0

I fixed it by first getting all the image file name of the current product, and deleting them. Not optimal but it works.

foreach ($array as $prod) {
            $sku = $prod[0];

            $currentImages = $client->catalogProductAttributeMediaList($sessionId, $sku);

            foreach($currentImages as $img)
            {
                $file = $img->file;
                $client->catalogProductAttributeMediaRemove($sessionId,(string)$sku, (string)$file);
            }

            $photo = $prod[8];
            $data = file_get_contents($photo);
            $base64 = base64_encode($data);

            $file = array(
                'content' => $base64,
                'mime' => 'image/jpeg'
            );

            $result = $client->catalogProductAttributeMediaCreate($sessionId, (string)$sku, array('file' => $file, 'label' => $sku, 'position' => '1', 'types' => array('thumbnail', 'image', 'small_image')), '', 'sku');

            print_r($result . '<br>');
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.