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

Rui Martins
  • 3,337
  • 5
  • 35
  • 40

3 Answers3

4

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.

Derwent
  • 606
  • 1
  • 5
  • 13
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.

Joey Orlando
  • 1,408
  • 2
  • 17
  • 33
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!

aminbadri7
  • 89
  • 2
  • 9