Thanks @dynamiczach, will have a look at it tonight.
In the meantime I’ve also started to look into the WooCommerce REST API v3 but it breaks on the first use
line :/
<?php
function shortcode_wooproducts_REST_API( $args = [] )
{
// Normalize attribute keys, lowercase
$args = array_change_key_case( (array)$args, CASE_LOWER );
// Merge supplied $args with default $args (override defaults)
$args = shortcode_atts([
'status' => 'publish',
'stock_status' => 'instock',
], $args);
## https://docs.woocommerce.com/document/woocommerce-rest-api/
## https://woocommerce.github.io/woocommerce-rest-api-docs/?php#rest-api-keys
## https://woocommerce.github.io/woocommerce-rest-api-docs/?php#authentication-over-https
require_once( WP_PLUGIN_DIR ."/woocommerce/vendor/autoload.php" ); //require_once( __DIR__ . '/vendor/autoload.php' );
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'https://domain.tld/blog2',
'ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // consumer key
'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // consumer secret
[
'wp_api' => true,
'version' => 'wc/v3',
'query_string_auth' => true // Force Basic Authentication as query string true and using under HTTPS
]
);
## https://packagist.org/packages/automattic/woocommerce#3.0.0
## https://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-products
use Automattic\WooCommerce\HttpClient\HttpClientException;
try {
$results = $woocommerce->get( 'products', $args );
echo '<pre><code>' . print_r( $results, true ) . '</code><pre>'; // JSON output
} catch (HttpClientException $e) {
echo '<pre><code>' . print_r( $e->getMessage(), true ) . '</code><pre>'; // Error message.
echo '<pre><code>' . print_r( $e->getRequest(), true ) . '</code><pre>'; // Last request data.
echo '<pre><code>' . print_r( $e->getResponse(), true ) . '</code><pre>'; // Last response data.
}
}
function shortcode_wooproducts_REST_API_init()
{
## https://stackoverflow.com/questions/57372608/shortcode-doesnt-work-when-im-getting-content-from-wp-rest-api-call
add_shortcode('wooproducts_REST_API', 'shortcode_wooproducts_REST_API');
}
add_action('init', 'shortcode_wooproducts_REST_API_init');
Error message:
PHP Parse error: syntax error, unexpected 'use' (T_USE) in /home/***/domain.tld/.../wp-content/themes/child/functions.php on line 19\
-
This reply was modified 5 years ago by Steven.