Woocommerce product api not accept images in some urls - php

I am creating products from external in woocommerce site. My code is like bellow:
if($_POST["Type"] == "CREATE"){
$data = array(
'product' => array(
'title' => $_POST["Title"],
'type' => 'simple',
'regular_price' => $_POST["Regular_price"],
'description' => $_POST["Description"],
'short_description' => $_POST["Short_description"],
'categories' => array(
$_POST['CategoryName']
),
'images' => array(
array(
'src' => $_POST["Image_url"],
'position' => 0
),
array(
'src' => $_POST["Image_url"],
'position' => 1
)
)
)
);
$res = $client->products->create($data);
}
When using $_POST["Image_url"] such like http://app.test.net:8080/test/img/company-logo.png gives the following error:
PHP Fatal error: Uncaught exception 'WC_API_Client_HTTP_Exception' with message 'Error: Error getting remote image
But if we give a normal url(url do not contain the specific port) such like http://test.com/wp-content/uploads/2016/02/test.png it's working properly.
How to resolve this?

In my case above mentioned code in the such a URL (http://app.test.net:8080/api/v1/product.php). Calling this API from .net application by (http://appname.test.net). Both are having the same domain part (test.net).
There fore difficult to find the URL to woo commerce.I have change URL the of .net application by my server configuration(I have using virtual hosts).Then it works properly.

---- (Edit 2) ----
Sorry but your question is Not clear at all. You have to update it, because it is very incomplete and fuzzy. Same thing for the title.
The medias images are located in a 2nd server on port 8080, right…
Form me the problem is here in:
'images' => array(
array(
'src' => $_POST["Image_url"],
'position' => 0
),
array(
'src' => $_POST["Image_url"],
'position' => 0
)
)
with 'src' => $_POST["Image_url"]
Have a look to this interesting related thread: Synchronize external database with woocommerce database

Related

Post to remote WordPress site with custom taxonomy

I'm using this code to push data to a custom post type of a remote website:
$api_response = wp_remote_post( 'https://example.com/wp-json/wp/v2/clothing_line', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'admin:5mMcJGUGNFYq9PxU5P0ad0Np' )
),
'body' => array(
'title' => 'Pink Shirt',
'status' => 'publish',
'post_type' => 'clothes',
'categories' => 2,
'slug' => 'pink-shirt',
'meta' => array('amount' => '12.50', 'style' => 'petite', 'size' => 'small', 'gender' => 'ladies', 'author' => 1)
)
));
...and it works fine except for one issue.
'categories' => 2 will probably work fine for regular categories, but I have a custom taxonomy for it called 'clothing_line_cats'.
I need the post that is created on the remote site to have the category for the 'clothing_line_cats' taxonomy.
I tried adding this...
'taxonomy' => 'clothing_line_cats'
...but that did not work.
I can't find any documentation for achieving this.
Feel kind of stupid I didn't realise this before.
Change this...
'categories' => 2
...to this...
'clothing_line_cats' => 2

Redux Framework creating a Protocol Relative URL Filter

I'm working on a WP Theme that uses Redux Framework and I am trying to create a filter for fields that have a URL attached to them, ie, Background Fields, Media Fields, so that I can make them Protocol relative and able to use SSL site-wide without conflicts.
So far I have the following function in my options-init.php file for a Background Field but to be honest I have very little experience with Filters, and the documentation for Redux Framework is very vague.
Field is as follows:
array(
'id' => 'front-background',
'type' => 'background',
'url' => true,
'title' => __('Front Page Background', 'blanque'),
'desc' => __('Background image for Front Page', 'blanque'),
'subtitle' => __('', 'blanque'),
'compiler' => true,
'output' => array(
'background' => 'body.home',
),
'default' => array(
'url' => '',
),
'background-color' => false,
'preview_height' => '100px',
)
Fuction to filter output:
function the_theme_redux_filters($url) {
$relativeURL = str_replace(array('http://','https://'), '//', $url);
return $relativeURL;
}
add_filter( 'redux/validate/front-background/class/{field.validate}', '', 10, 1 );
Would someone be able to give me a clue as to what I should actually be doing please?

How to add product image via the woocommerce REST API?

I am working with the Woocommerce REST API and need to add a product to the store.
It worked before. Now I have this error:
stdClass Object ( [errors] => Array (
[0] => stdClass Object ( [code] =>
woocommerce_api_invalid_remote_product_image
[message] => Error getting remote image
https://www.google.lt/images/srpr/logo11w.png ) ) )
Here is the documentation for adding a product via the WooCommerce REST API
http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product
Here is my code:
$dataArray = array(
'title' => 'xxxxxxxxxx',
'description' => 'description1',
'price' => '69',
'sku' => 'sku2',
'tags' => 'tag1, tag2, tag3',
'color' => array('red', 'blue'),
'size' => array('S', 'M'),
'image' => 'https://www.google.lt/images/srpr/logo11w.png'
);
public function addProduct($data)
{
$wc_api = $this->_getClient();
$newProductData = array(
'product' => array(
'title' => $data['title'],
'type' => 'variable',
'regular_price' => $data['price'],
'description' => $data['description'],
'sku' => $data['sku'],
'tags' => [ $data['tags'] ],
'images' => [ array('src' => $data['image'], 'position' => '0') ],
'virtual' => true
)
);
return $wc_api->create_product($newProductData);
}
I'm using this client to call the REST API
https://github.com/kloon/WooCommerce-REST-API-Client-Library
EDITED:
If I get image from wordpress where woocommerce is hosted then all is fine. But, if I use a link from another site then I get an error.
I had a similar issue and the error that cURL was generating inside WordPress which leads to woocommerce_api_invalid_remote_product_image was {"errors":{"http_request_failed":["SSLRead() return error -9806"]},"error_data":[]} which means, according to Asaph in https://stackoverflow.com/a/26538127/266531,
php is compiled with a version of cURL that uses Apple's Secure
Transport under Yosemite and the target of the URL request doesn't
support SSLv3 (which was probably disabled due to the POODLE
vulnerability).
My guess is that you are experiencing errors with using SSL over cURL.
Have you tried it with an http link rather thank https?
If you can debug the server side, take a look at what's happening inside class-wc-api-products.php around line 1700. That's what's generating the error. You may be experiencing SSL errors.
If it is the same type of SSL issue, then your possible solutions are
Use a non-secure image link (http instead of https), or
Follow Asaph's steps to install PHP using OpenSSL instead of SecureSSL in his answer at https://stackoverflow.com/a/26538127/266531
In Woocommerce REST API liblary You can also set option to dont validate SSL.
$options = array(
'debug' => true,
'return_as_array' => false,
'validate_url' => false,
'timeout' => 60,
'ssl_verify' => false,
);

Adding a new field to an existing content-type in Drupal 7

I'm new to Drupal and I'm looking for a way to add a new field to an already installed content-type in Drupal 7. Please note that some content is already present in the database. Also, I need to do this programmatically and not via a GUI.
Googling, I have already found the following documents, which seem to be related:
https://api.drupal.org/api/drupal/modules!field!field.module/group/field/7
https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_update_N/7
Still, my ideas are a bit confused and a basic example would clarify things.
This snippet should get you started. It was found on the Drupal Stackexchange. I suggest you check there first in the future.
https://drupal.stackexchange.com/questions/8284/programmatically-create-fields-in-drupal-7
$myField_name = "my_new_field_name";
if(!field_info_field($myField_name)) // check if the field already exists.
{
$field = array(
'field_name' => $myField_name,
'type' => 'image',
);
field_create_field($field);
$field_instance = array(
'field_name' => $myField_name,
'entity_type' => 'node',
'bundle' => 'CONTENT_TYPE_NAME',
'label' => t('Select an image'),
'description' => t(''),
'widget' => array(
'type' => 'image_image',
'weight' => 10,
),
'formatter' => array(
'label' => t('label'),
'format' => 'image'
),
'settings' => array(
'file_directory' => 'photos', // save inside "public://photos"
'max_filesize' => '4M',
'preview_image_style' => 'thumbnail',
'title_field' => TRUE,
'alt_field' => FALSE,
)
);
field_create_instance($field_instance);
drupal_set_message("Field created successfully!");
}
You can execute this code countless ways. I am not privy to the requirements of your project so its hard for me to make a recommendation. You could hook this into the update/install functions, or you could build it into a page hook in a module, or you could just bootstrap any new php file in the root directory with this:
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Modifying the number of default chunks in the Page Module for PyroCMS

I want to have 2 default chunks instead of 1 when making a new page.
The reason is, my main template is split into 2 sections and I want to start with 2 chunks.
What I would like to do is edit the page module so that whenever a user creates a new page they will always start with 2 chunks that I have named and defined.
What would I have to modify in order to achieve this?
Note: I already modified the page module and database to add some custom fields. I assume this is the same deal but I can't find where the initial chunk is being created...
If you look at around line 340 in the admin controller of the pages module, there is an array being created in the $pages->chunks property. I amended this as below by adding another array/object and this creates pages with two chunks called "default" and "default2".
$page->chunks = array((object) array(
'id' => 'NEW',
'slug' => 'default',
'body' => '',
'type' => 'wysiwyg-advanced',
),
(object) array(
'id' => 'NEW2',
'slug' => 'default2',
'body' => '',
'type' => 'wysiwyg-advanced',
),
);
To be honest I'm not sure what the id does, but probably be best to make sure it is unique.
Thanks to #nick-pyett. Here is the code that worked for me.
this is the final code:
$page->chunks = array(array(
'id' => 'NEW',
'slug' => 'default',
'class' => '',
'body' => '',
'type' => 'wysiwyg-advanced',
),
array(
'id' => 'NEW2',
'slug' => 'default2',
'class' => '',
'body' => '',
'type' => 'wysiwyg-advanced',
),
);
I looked to see where the id ends up. It seems to get appended to the name of each element. Like so:
chunk_slug[NEW2]
The actual slug goes into the first input on the chunk which would be the slug you would use to call it in a page.
{{ pages:chunk id="{{ page:id }}" name="default2" }}

Categories