How I can create an API KEY for woocommerce by WP-CLI?
Asked Answered
S

4

5

How I can create an API KEY for woocommerce by WP-CLI?

I'm creating a WooCommerce store by this commands:

sudo /usr/local/bin/wp --info --allow-root
sudo /usr/local/bin/wp cli update --allow-root
sudo /usr/local/bin/wp core download --allow-root
sudo /usr/local/bin/wp core config --dbname=$MYSQL_DATABASE --dbuser=$MYSQL_USER --dbpass=$MYSQL_PASSWORD --allow-root
sudo chown -R apache:apache wp-config.php
sudo /usr/local/bin/wp core install --url=$URL --title="$WP_TITLE" --admin_user=$WP_USERNAME --admin_password=$WP_PASSWORD --admin_email=$WP_MAIL --allow-root

sudo /usr/local/bin/wp theme install woot storefront --allow-root
sudo /usr/local/bin/wp plugin install homepage-control  --allow-root
sudo /usr/local/bin/wp plugin activate homepage-control  --allow-root
sudo /usr/local/bin/wp theme activate woot --allow-root

sudo /usr/local/bin/wp plugin install woocommerce  --allow-root
sudo /usr/local/bin/wp plugin activate woocommerce  --allow-root

sudo /usr/local/bin/wp post delete 1 --allow-root

After that I need to create a new KEY for the API by command line, can anyone help me please?

Thanks

Shote answered 28/9, 2016 at 0:16 Comment(0)
G
5

Here's a bash script that I made that will do exactly what you want to do. If you like this answer, please show me some love, this took me ages.

        as_web_user() {
            su $user --shell $SHELL -c "$*"
        }

        # Run the wp commands as web-user
        # This part has some inspiration from https://github.com/autopilotpattern/wordpress/blob/master/bin/prestart.sh
        as_web_user "wp core install --url=\"$WORDPRESS_SITE_URL\" \
          --title=\"$WORDPRESS_SITE_TITLE\" \
          --admin_user=\"$WORDPRESS_ADMIN_USER\" \
          --admin_password=\"$WORDPRESS_ADMIN_PASSWORD\" \
          --admin_email=\"$WORDPRESS_ADMIN_EMAIL\" "

        if [ -n "$WORDPRESS_ACTIVE_THEME" ]; then
            as_web_user "wp theme activate \"$WORDPRESS_ACTIVE_THEME\""
        fi

        # TODO: install plugins from env variable and ensure woocommerce
        as_web_user "wp plugin install woocommerce --activate"

        # Set up woocommerce
        as_web_user "wp wc tool run install_pages --user=\"$WORDPRESS_ADMIN_USER\""
        # Creates the woocommerce_api_keys table if it doesn't exist

        if [ -n $WOOCOMMERCE_CONSUMER_KEY ] && [ -n $WOOCOMMERCE_CONSUMER_SECRET ]; then

            as_web_user "wp eval 'WC_Install::install();'"

            as_web_user "wp eval '
                global \$wpdb; 
                echo \$wpdb->insert(
                    \$wpdb->prefix . \"woocommerce_api_keys\", 
                    array(
                        \"user_id\"=>1, 
                        \"permissions\"=>\"read_write\", 
                        \"consumer_key\"=> wc_api_hash( \"$WOOCOMMERCE_CONSUMER_KEY\" ), 
                        \"consumer_secret\"=> \"$WOOCOMMERCE_CONSUMER_SECRET\", 
                        \"truncated_key\" => substr( \"$WOOCOMMERCE_CONSUMER_SECRET\", -7 ) 
                    ), 
                    array( \"%d\", \"%s\", \"%s\",\"%s\",\"%s\", ) 
                );'"
        fi

        if [ -n "$WOOCOMMERCE_TEST_DATA" ] && [ ! -f "sample_products.xml" ]; then
            as_web_user "wp plugin install wordpress-importer --activate"
            as_web_user "curl -OL https://raw.githubusercontent.com/woocommerce/woocommerce/master/sample-data/sample_products.xml"
            as_web_user "wp import sample_products.xml --authors=create"
        fi

You will need to provide these environment variables

        WORDPRESS_SITE_URL
        WORDPRESS_SITE_TITLE
        WORDPRESS_ADMIN_USER
        WORDPRESS_ADMIN_PASSWORD
        WORDPRESS_ADMIN_EMAIL
        WORDPRESS_ACTIVE_THEME
        WOOCOMMERCE_TEST_DATA
        WOOCOMMERCE_CONSUMER_KEY
        WOOCOMMERCE_CONSUMER_SECRET
        user # the web user (e.g. www-data)

This is part of my docker image for provisioning a WooCommerce install complete with API keys and test data.

Guideline answered 14/8, 2018 at 9:26 Comment(1)
thanks. Its sad that woocommerce devs dont support this via CLIRavage
C
4

This is a modification of @Derwent's answer as I couldn't quite get his wp eval command to work:

WOOCOMMERCE_CONSUMER_KEY=<key> WOOCOMMERCE_CONSUMER_SECRET=<secret> wp eval '
global $wpdb;
echo $wpdb->insert(
  $wpdb->prefix . "woocommerce_api_keys",
  array(
    "user_id" => 1,
    "description" => "Frontend Client",
    "permissions" => "read",
    "consumer_key"=> wc_api_hash(getenv("WOOCOMMERCE_CONSUMER_KEY")),
    "consumer_secret" => getenv("WOOCOMMERCE_CONSUMER_SECRET"),
    "truncated_key" => substr(getenv("WOOCOMMERCE_CONSUMER_SECRET"), -7)
  )
);'

The main issue I was running into was that $WOOCOMMERCE_CONSUMER_KEY was not recognizable within wp eval. The getenv PHP function solves this.

Coast answered 12/5, 2020 at 2:8 Comment(0)
T
1

You can easily create or update or Generate Woocommerce API keys using (https://wordpress.org/plugins/code-snippets)

copy and paste this code and modify it with your information:

global $wpdb;
echo $wpdb->insert(
  $wpdb->prefix . "woocommerce_api_keys",
  array(
    "user_id" => 1,
    "description" => "App_Key",
    "permissions" => "read",
    "consumer_key"=> "ck_bdd51885c94eaa1c35a8714de211a84567XXXXXX",
    "consumer_secret" => "cs_9e5aa761a36d7c6b321f882c0aacf99929XXXXXX",
    "truncated_key" => substr("cs_9e5aa761a36d7c6b321f882c0aacf99929XXXXXX", -7)
  )
);

on the code snippets plugin select Only run once. and then delete it.

Happy coding!

Theresiatheresina answered 2/9, 2022 at 18:50 Comment(0)
A
0

You can combine the previous solution with the WooCommerce source code at https://github.com/woocommerce/woocommerce/blob/bd838d824aab83631a79af18a4c439b5f67020e7/plugins/woocommerce/includes/class-wc-auth.php#L210

Use this if you want a dynamic generated pair of ck and cs.

wp eval '
global $wpdb;
$user = get_user_by("login", "api_user");
$description = sprintf(
    "%s - API (%s)",
    wc_trim_string(wc_clean("api_user"), 170),
    gmdate("Y-m-d H:i:s")
);

$permissions = "read_write";
$consumer_key = "ck_" . wc_rand_hash();
$consumer_secret = "cs_" . wc_rand_hash();

$result = $wpdb->insert(
    $wpdb->prefix . "woocommerce_api_keys",
    [
        "user_id" => $user->ID,
        "description" => $description,
        "permissions" => $permissions,
        "consumer_key" => wc_api_hash($consumer_key),
        "consumer_secret" => $consumer_secret,
        "truncated_key" => substr($consumer_key, -7),
    ],
    ["%d", "%s", "%s", "%s", "%s", "%s"]
);

if ($result) {
    echo json_encode([
        "userId" => $user->ID,
        "consumerKey" => $consumer_key,
        "consumerSecret" => $consumer_secret,
    ]);
} else {
    echo json_encode(["error" => "Failed to insert API key"]);
}
'

Aga answered 16/10, 2024 at 21:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.