4

如何通过 WP-CLI 为 woocommerce 创建 API KEY?

我正在通过以下命令创建一个 WooCommerce 商店:

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

之后我需要通过命令行为 API 创建一个新的 KEY,有人可以帮我吗?

谢谢

4

2 回答 2

2

这是我制作的 bash 脚本,它将完全按照您的意愿进行操作。如果您喜欢这个答案,请给我一些爱,这花了我很多时间。

        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

您将需要提供这些环境变量

        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)

这是我的 docker 镜像的一部分,用于配置带有 API 密钥和测试数据的 WooCommerce 安装。

于 2018-08-14T09:26:59.807 回答
1

这是对@Derwent 答案的修改,因为我无法完全让他的wp eval命令起作用:

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)
  )
);'

我遇到的主要问题是$WOOCOMMERCE_CONSUMER_KEYwp eval. getenvPHP 函数解决了这个问题。

于 2020-05-12T02:08:19.650 回答