I have a site that uses php to upload product information to woo commerce. everything works fine; however i am now stuck trying to work out how to add multiple images. I have got it adding 1 image fine as seen below
$imageURL = "https://www.[website].co.uk/new/wp-content/uploads/products/";
$imageURL = $imageURL . $product->ITEMNO . ".JPG";
if (!(false === file_get_contents($imageURL,0,null,0,1))) {
$data['images'] = [[
'name' => $product->DESC,
'src' => $imageURL,
'alt' => $product->DESC
]];
echo "Image URL: " . $imageURL . "\n";
}
$woocommerce->put('products/'.$searched[0]->id, $data);
If i try adding in another image using $data['image'] = ... with different data; it just overwrites the image
Im assume EITHER theres a different input for adding it to the product gallery instead of just the main product image. but i couldnt see one at https://woocommerce.github.io/woocommerce-rest-api-docs/?php#product-images-properties
OR if its to do with different id's for the image. Of which i tried adding 'id' => 56565656, to the $data['images'] but it just crashed on me.
Any help would be appreciated.
From woo commerce docs, 'images' is an array type; so it was just figuring out how to write it to get the 2nd image in the second slot of the array; find below the answer
$data['images'] =
[
[
'name' => $product->DESC,
'src' => $imageURL,
'alt' => $product->DESC
],
[
'name' => $product->DESC2,
'src' => $imageURL2,
'alt' => $product->DESC2
]
];
Related
I have been trying since days but I can not. I want to upload products with multiple options/variants and its images, product images its variant & options are uploaded but variant images are not being assigned. What I have tried since. Event it does not show the error, products are uploading but not variant images
1# Using Shopify SDK
$shopify = new ShopifySDK($config);
$products = $shopify->Product->post($product_array);
$productVariants = $shopify->ProductVariant->post($id, $variant_array);
$image = $shopify->Image->post($v_id, $images_array)
2# Using
Signifly\Shopify\Shopify;
$product = $shopify->createProduct($array); // inserting whole product, variant, options and images at a time
and uploading separately
$shopify->createProduct($product_array);
$shopify->createVariant($id, $variant_array);
$shopify->createProductImage($id, $images_array);
#3 Using Rest api
$client = new Rest('mystore-6842.myshopify.com', 'shpat_*********************');
$response = $client->get('products');
$products_array = array(
"product" => array(
"title" => 'title of product',
"body_html" => 'description',
"vendor" => "vendor of product",
"published" => true ,
"variants" => array(
array(
"sku" => 'sku of product',
"price" => 'product price',
"taxable" => false,
"inventory_quantity"=> 'available quantity',
)
)
)
);
$shop = Shopify::shop();
$products=$shop->api()->rest('POST','admin/api/2023-01/products.json',$products_array);
in the rest api package is unable to locate my shop.
I am build a laravel 5.8 API only application and want to return the image path as path of an API resource so that it can be consumed inside an image source attribute. So this is what I have to done to achieve this.
1. I ran php artisan storgae:link command to create the symbolic link from public/storage to storage/app/public
First I store the image inside a productImages table when a new product is successfully created like this
public function store(Request $request)
{
// create & store the product
if ($product = Product::create([
'name' => $request->name,
'category' => $request->category,
'status' => $request->status,
'price' => $request->price,
'interest' => $request->interest,
])) {
// store the product image
$file = $request->file('image');
$destinationPath = "public/images/products";
$filename = 'pramopro_' . $product->name . '_' . $product->id . '.' . $file->extension();
Storage::putFileAs($destinationPath, $file, $filename);
ProductImage::create([
'product_id' => $product->id,
'name' => $filename
]);
}
// return new product
return new ProductResource($product);
}
Return the image path in the ProductResource like this
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'category' => $this->category,
'status' => $this->status,
'price' => $this->price,
'interest' => $this->interest,
'hidden' => $this->hidden,
'imageUrl' => asset('images/products/' . $this->image->name)
];
}
Testing it on my local serve I get a path like this
{
"id": 1,
"name": "dpk",
"category": "fuel",
"status": "open",
"price": 100000,
"interest": 0.2,
"hidden": 0,
"imageUrl": "http://localhost:8000/images/products/pramopro_dpk_1.jpeg"
}
When I try to view the image by putting http://localhost:8000/images/products/randomtext_1.jpeg in my browser, I get 404 not found error.
But this http://localhost:8000/storage/images/products/pramopro_dpk_1.jpeg displays the image as expected.
How should I be retrieving the image path for it to work?
the asset() function puts you in your public directory, you need to add /storage to the path of your file , try this in your toArray function
'imageUrl' => asset('storage/images/products/' . $this->image->name)
Personal note , handling media in laravel can be a headache i personally use LaravelMediaLibrary by spaite
https://github.com/spatie/laravel-medialibrary
it has really helped me and made handling media as easy as it can get especially when handling multiple media for the same model
I am beginner in wordpress. I have to upload a file in the subfolder of root(http://www.domain.com/myfoldername) for a special post. It will work only for a special post, not for all. No need to change my default directory and I wan't not use any plugin.
Can anyone help me to do this? I already searched, but didn't get any solution.
Thanks
Through WordPress you can only upload files in the default upload directory. If you don't want to use any plugin, you cannot upload file in a different directory. You can upload file anywhere you want through FTP and then just put the link of the file in the post.
Try this code for your specific page.
add_filter('upload_dir', 'upload_image_specific_calback');
function upload_image_specific_calback( $param ){
//$_GET['post'] which is your target post like 10 is post id.
//After click update button.
if(isset($_GET['post'])){
if($_GET['post'] == 71){
$param = array(
'path' => get_home_path().'myfoldername',
'url' => home_url().'/myfoldername',
'subdir' => '',
'basedir' => get_home_path(),
'baseurl' => home_url(),
'error' => false
);
}
}
//$_POST['post_id'] which is your target post like 10 is post id.
//instant upload time before save
if(isset($_POST['post_id'])){
if($_POST['post_id'] == 71) {
$param = array(
'path' => get_home_path().'myfoldername',
'url' => home_url().'/myfoldername',
'subdir' => '',
'basedir' => get_home_path(),
'baseurl' => home_url(),
'error' => false
);
}
}
error_log("path={$param['path']}");
error_log("url={$param['url']}");
error_log("subdir={$param['subdir']}");
error_log("basedir={$param['basedir']}");
error_log("baseurl={$param['baseurl']}");
error_log("error={$param['error']}");
return $param;
}
My HTML form allows users to select any number of images to be uploaded to a custom post type. I've created a repeater field in ACF called 'images', of 'image' objects.
How do I save the uploaded images into that repeater field?
This is what I currently have:
$files = $_FILES['file'];
$image_number = 1;
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$uploaded_file = wp_handle_upload($file, array('test_form' => FALSE));
update_sub_field( array('images', $image_number++, 'image'), $uploaded_file, $post_id);
}
}
But the images aren't saved. Any help appreciated, thanks!
As far as I see, your use of the update_field function especially it's signature is wrong. You mixed up the parameters. Have a look here: Wordpress ACF: How to add rows to a repeater field attached to a user via custom code (PHP)
Try the following code. It is working for me. Although it's an old post, it might help someone else in future.
$attachment = array(
'guid' => $upload_dir . '/' . basename( $filename ),
'post_mime_type' => $filetype['type']
);
$attachment_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
$row = array(
'name' => "Hello",
'logo' => $attachment_id, // this is where you put post id of your image
'website' => "http://hello.com"
);
add_row( "repeater_field", $row, $blog_post_id);
I also noticed that add_row works only if you have already at least one row. But if you want to add a new row, use update_field. Take a look here: add_row in ACF Pro isn't saving repeater values
I'm using cakephp-upload plugin of jose gonzalez to upload images to our app. By default, they're being saved in a directory like this: webroot/files/user/photo/{user_id} which is fine, except for when we want to display such images using $this->Html->image() which searches for images in the webroot/img directory.
I have already tried to display the images with
echo $this->Html->image('../files/user/photo/' .
$user['User']['photo_dir'] . '/' .
$user['User']['photo']);
which works but I was wondering if there's some way to tell this plugin to save into the img directory? The documentation doesn't mention any of that.
And also, is there any way to tell the $this->Form->input('User.photo', array('type' => 'file')); to accept only image files?
as you can see in this file, path is set like:
public $defaults = array(
'rootDir' => null,
'pathMethod' => 'primaryKey',
'path' => '{ROOT}webroot{DS}files{DS}{model}{DS}{field}{DS}',
...
you could change it to make:
'path' => '{ROOT}webroot{DS}img{DS}'
and for your second question, you could use accept attribute, like:
$this->Form->input('User.photo',
array(
'type' => 'file',
'options' => array('accept' => 'image/*')
)
);