Wordpress REST API search products - json

Hope that you can help me.
I have a website that runs wooccommerce. I am using the woocommerce rest api. Now on another website what I want to do is create a simple search field where I type something and the search form needs to search through the woocommerce website and return results.
Is there a way I can achieve that ?

Try this:
/wp-json/wc/v2/products?search={{product_name}}
Works for me.

I think no.
The New Version of WooCommerce API (v2) is supports id based calls.
so instead you can get a list of abstract product details in your 2nd website and call back via product id.

/wp-json/wc/v2/products?search={{product_name}}
Is this function working if you are looking a exact product?
If you have a product like MR2050 and another with MR2050K, the result is not as you may expected, cause API rest will return 2 products, not just ONE?

If you use a wc-api-php (PHP library), please follow like this :
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'http://mydomain', // Your store URL
'ck_****', // Your consumer key
'cs_****', // Your consumer secret
[
'wp_api' => true, // Enable the WP REST API integration
'version' => 'wc/v3' // WooCommerce WP REST API version
]
);
echo "<pre>";
print_r($woocommerce->get('products', ['search' => 'key word']));
echo "</pre>";

This works for me in 2021:
/wp-json/wc/v3/products?search={{productName}}
productName - refers to the input that you enter in the Search box

Related

Angular 9 ng2-smart table

I am using ng2-smart-table, I am trying to do server-side pagination & sorting. I am not able to get how to do it? Once I click on page number it should go to the backend. What function/method/event I have to use, when clicked on page or sort it should go to the function which calls API.
Also I want to add a anchor link to one column, how to do that?
If you provide me some example that will be helpful.
Please help me with the solution. Thanks
You need to configure ServerDataSource with the names of the parameters your server uses for each purpose, after that you can deal with them in the server side.
this.dataSource = new ServerDataSource(this.http, {
endPoint: environment.SERVICE + `your_route`,
dataKey: 'Data',
pagerPageKey: 'offset',
pagerLimitKey: 'limit',
filterFieldKey: 'query',
sortFieldKey: 'sortby',
sortDirKey: 'order',
totalKey: 'Total',
});

Insert data in Laravel application from a Wordpress website using mysql

i'm developing an application in Laravel but the thing is: my client already have a wordpress website and wants all registrations made in this website to go automatically to my application. I'm using mysql and the wp website too.
Can anyone give me a light here?
Thanks and sorry for my bad english.
I will try to give a basic workflow concept.
Do you want that, user will register in wp site, and that time he will be added/registered in the laravel application as well ? If so, then,
Create a user table, User model and userController with a 'store' method in laravel app. This 'store' method will create the user. The table's column names would be better to match the wp site's wp_users table columns name.
Now create an api route in laravel app that will take user data as post parameters. This user data will come from wp site after registration. The api route could be something like this
Route::post('store', 'userController#store')->name('users.store');
Now in wp site , place this code in 'user_register' hook. This code calls that store api of laravel app and pass the user data of that registered user as user data.
add_action( 'user_register', 'my_registration_save', 10, 1 );
function my_registration_save( $user_id ) {
$user = get_userdata($user_id);
//api call
wp_remote_post( the_api_url, array(
'user_email' => $user->user_email,
'user_login' => $user->user_login
...
...
) )
}
Now the wp site is sending the data through the api, your userController's store method have the access of that data. Use that data to store it in laravel's db, the userController's store method will be something like this
function store( Request $request) {
$user = new App\User();
$user->user_email = $request->user_email,
$user->user_login = $request->user_login
...
...
$user->save()
}
That's it.

Connecting one dropdown list to another perl

In Perl, I have script that creates a dropdown list based on a database that contains a list of vendors. I would like to use the selection of the first list to populate the second list with values of different contacts given that specific vendor.
ie. Haliburton is vendor....once this is chosen contact Jim, Paul, George are available in contact list that is next to it.
Currently I am getting the list of vendors and the list of contacts separately. How do I get the list of contacts based on the vendor in the database to be populated in the CGI popup_menu?
The following is my current code:
#!c:\perl\bin\perl.exe
use CGI;
use strict;
use warnings;
require ("data_eXchangeSubs.pm");
$query = new CGI;
print $query->header(-expires=>'-1d');
print $query->start_html(-title=>'Dex Vendor Testing',
-bgcolor=>'white'
);
my $dataX = ${ConnectToDatabase($main::DB1, $main::DBEnv)};
$resultSet = $dataX->Execute("select vendor from vendor_info group by vendor");
my #list_of_vendors;
while(!$resultSet->EOF) {
push #list_of_vendors, $resultSet->Fields("vendor")->Value;
$resultSet->MoveNext;
}
From here, I would like to populate another dropdown list with contacts from the vendor_info data table. Currently I'm making a separate query execution but I would like to take a given vendor from the previous array and populate only those contacts specific to the chosen vendor.
I know I have to change the values of the contacts but don't know how to :(
$resultContact = $dataX->Execute("select contact from vendor_info");
my #list_of_contacts;
while(!$resultContact->EOF) {
push #list_of_contacts, $resultContact->Fields("contact")->Value;
$resultContact->MoveNext;
}
print $query->popup_menu(
-name => 'vendors'
, -values =>\#list_of_vendors
, -default => $default_vendor
, -style=> 'width:200px'
);
print $query->popup_menu(
-name => 'contacts'
, -values => \#list_of_contacts
, -default => $default_vendor
);
print $query->end_html;
This is a very broad question. You have a couple of simple options.
1) Create an HTML form with the list of vendors, take (and sanitize!) the user's selection, and return the list of contacts. This is a traditional dynamic CGI form.
2) Use JavaScript. This only works for a reasonable amount of data. There are several ways to do it but the gist of the idea is: a) associate each contact with the correct vendor, b) hide all the contacts initially, c) when a vendor is selected, display the correct contacts.
JQuery is a handy JavaScript tool for this kind of manipulation and magically hides many browser incompatibility problems.
3) You could create a fancy, modern AJAX-ified form, but from your question, this is probably overkill.
Sorry there is no quick-and-dirty solution but I'm hoping to give you enough here to start Googling in the right direction! Best!
With CGI, you need to use CGI::param method.
# I know the name of the select is "vendors",
# but it makes more sense calling it here with 'vendor'
# *you would have to change the name of the element*
my $vendor_name = $query->param( 'vendor' );
And I'm not sure how your database object works (kind of looks like the Microsoft iteration model), but using the proper way of parameterized queries in DBI, it looks like this:
my $stmt = $dbh->prepare( 'select contact from vendor_info where vendor_name=?' );
$stmt->execute( $vendor_name ); # binds the variable to the query.
Then you step through the rows of the query and build your list of contacts. Of course, you'd mostly likely use fetchrow_array or fetchrow_arrayref methods to walk through them.
Although this might be better off utilizing a more modern strategy by getting a combined list of vendors and contacts for JavaScript or using Ajax to query the contacts for a vendor.
There is no "connecting" structure in basic Perl CGI. You would have to use a more modern web framework for that.

Get term id of current taxonomy in wordpress

I am trying to create a function which adds additional custom data fields to my taxonomy pages, I have found this plugin which add this capability.
Now I can get the saving the custom data works fine.. however trying to get the metadata on edit form pages is another story..
I've followed the documentation which says to use the following line..
get_term_meta($term_id, $key, $single)
However I am unable to get this to work.. I have to manually enter the term_id like so..
$term_meta = get_term_meta('36', '', true);
.. in order for it to work.
Can someone tell me what code I would need for php get the term_id?
You can use this function to do the debug
$term = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
print_r ($term);
Thanks
yes true.
$idObj = get_category_by_slug(post_type);
$id1 = $idObj->term_id;
If you are using any WordPress inbuilt function, then you need write the objname->subkey.
Okay so I found the problem
I had to use $term_id->term_id instead of just $term_id

Get Facebook Status from Fan Page using API

I've been trying to get the most recent Facebook Status for a fan page via the API for a while now and can't seem to get what I'm after. I'm really trying to avoid using RSS for it. I can get the full list from the feed via https://graph.facebook.com/174690270761/feed but I want only the last status posted by the page admin, not by anyone else.
Is their an easy way to get it without having to authenticate?
Thanks in advance
edit:
https://graph.facebook.com/174690270761/statuses seems to be what I'm looking for but I need an OAuthAccessToken for this but can't see how to without involving the user, as I'm trying to access through my own credentials/application
Facebook has changed the api and now requires you to provide an access token.
You can use Facebooks PHP sdk for that but i found a much quicker way to go:
<?
$appid = '1234567890';
$secret = 'db19c5379c7d5b0c79c7f05dd46da66c';
$pageid = 'Google';
$token = file_get_contents('https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='.$appid.'&client_secret='.$secret);
$feed = file_get_contents('https://graph.facebook.com/'.$pageid.'/feed&'.$token);
print_r(json_decode($feed));
?>
Update 15/12/12
app access tokens nosist of the id and secret now - use this request:
$feed = file_get_contents('https://graph.facebook.com/Saxity/feed?access_token='.$appid.'|'.$secret);
I finally found out that this doesn't have to be done through the API, or require any authentication at all.
Basically you can access the data via a JSON feed:
$pageID = "ID of Page" //supply the Id of the fan page you want to access
$url = "https://graph.facebook.com/". $pageID ."/feed";
$json = file_get_contents($url);
$jsonData = json_decode($json);
foreach($jsonData->data as $val) {
if($val->from->id == $pageID) { //find the first matching post/status by a fan page admin
$message = $val->message;
echo $message;
break; //stop looping on most recent status posted by page admin
}
}
I don't think there's a good way to do this. You can process the JSON pretty easily though from the looks of it. You can limit the number of results by using the since query parameter. For instance:
https://graph.facebook.com/174690270761/feed?since=last%20Monday
You can also use limit on that, but I don't think that filters by user. If the administrator is the only one allowed to post, then you may be able to get away with using limit=1.