I’m trying to change product image via php. But I can’t get it to work.
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$yesterday = date("Y-m-d", time() - 60 * 60 * 24);
$jsondata = file_get_contents("730.json");
ini_set('memory_limit', '-1');
$json = json_decode($jsondata, true);
$price = $json["AWP | Asiimov (Well-Worn)"][$yesterday]["price"]/100;
$post_information = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "publish",
'post_title' => 'test',
'post_parent' => '',
'post_type' => "product",
);
$image = media_sideload_image("http://www.technologijos.lt/upload/image/n/technologijos/it/S-49657/samsung-ssd-840-evo.jpg", 0, "The WordPress Logo");
$post_id = wp_insert_post($post_information);
$attachment = wp_get_attachment_image( 18, 'thumbnail' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0');
update_post_meta( $post_id, '_downloadable', 'no');
update_post_meta( $post_id, '_virtual', 'yes');
update_post_meta( $post_id, '_regular_price', $price );
update_post_meta( $post_id, '_sale_price', "1" );
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta($post_id, '_sku', "145");
update_post_meta( $post_id, '_product_attributes', array());
update_post_meta( $post_id, '_sale_price_dates_from', "" );
update_post_meta( $post_id, '_sale_price_dates_to', "" );
update_post_meta( $post_id, '_price', $price );
update_post_meta( $post_id, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_backorders', "no" );
update_post_meta( $post_id, '_stock', "" );
update_post_meta( $post_id, '_product_image_gallery', $attachment );
The last line doesnt work. And i dont know how to get it working. I tried changing different things, (I am new to PHP/ WordPress).
Are you trying to set the featured image?
If so, you may want to use the function given in the answer here.
The Function:
Usage:
I would like to add to bryceadams answer.
Seems like that, for example, eBay’s Finding API returns images whose URL has a structure that will not allow the command
to operate as expected because, what happens is, all URLs of the images have the same ending basename and the files get conflicted. I found out that the command
works better for many APIs. Hope it adds up and helps.