whoami7 - Manager
:
/
data
/
vhosts
/
pruebas
/
www
/
cambiar_imagenes_defecto
/
Upload File:
files >> /data/vhosts/pruebas/www/cambiar_imagenes_defecto/index.php
<?php // Definiciones define('API_URL', 'http://pyc.ncdesarrollos.com/rest/V1/'); define('API_USER', 'roberto'); define('API_PASS', '1156Magento'); // Obtener el token de acceso $ch = curl_init(API_URL . 'integration/admin/token'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('username' => API_USER, 'password' => API_PASS))); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $token = json_decode(curl_exec($ch)); curl_close($ch); // Conectar con mysqli $conexion = new mysqli("pyc.ncdesarrollos.com", "magento", "HqaSf7etVjPC", "magento"); // Comprobar la conexión if ($conexion->connect_errno) { printf("Falló la conexión: %s\n", $conexion->connect_error); exit(); } // Consulta SQL $sql = "SELECT sku FROM catalog_product_entity ORDER BY sku ASC;"; $resultado = $conexion->query($sql); // Recorrer el resultado while ($producto = $resultado->fetch_assoc()) { // Limpiar la variable $imagen_defecto = null; // Obtener las imágenes del producto usando el API de Magento $ch = curl_init(API_URL . 'products/' . $producto['sku'] . '/media'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $imagenes = json_decode(curl_exec($ch)); // Si la imagen es la posición 0, es la imagen por defecto foreach ($imagenes as $imagen) { if ($imagen->position == 0) { $imagen_defecto = $imagen; } } if ($imagen_defecto != null) { // Cambiar la imagen por defecto $ch = curl_init(API_URL . 'products/' . $producto['sku'] . '/media/' . $imagen_defecto->id); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token)); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('entry' => array('id' => $imagen_defecto->id, 'media_type' => 'image', 'label' => 'Image', 'position' => 0, 'disabled' => false, 'types' => array('image', 'small_image', 'thumbnail'))))); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $resultado = json_decode(curl_exec($ch)); curl_close($ch); } } ?>
Copyright ©2021 || Defacer Indonesia