Skip to content

Instantly share code, notes, and snippets.

@codeagencybe
Created August 19, 2018 23:26
Show Gist options
  • Select an option

  • Save codeagencybe/6fdfb4247f4e0c16a65fa3064844a9d3 to your computer and use it in GitHub Desktop.

Select an option

Save codeagencybe/6fdfb4247f4e0c16a65fa3064844a9d3 to your computer and use it in GitHub Desktop.
WooCommerce import with WP CLI
#!/bin/bash
echo -e "Importing products now!"
OLDIFS=$IFS
while IFS='|' read -r sku images product_title short_description cats weight regular_price stock_qty manage_stock length height width
do
DIMS=({\"length\":\"${length}\"\,\"width\":\"${width}\"\,\"height\":\"${height}\"})
echo ${DIMS[@]}
IFS=", "
read -a CATEGORIES <<<$cats
#reset IFS
IFS=$OLDIFS
CATS=([{\"id\":${CATEGORIES[0]}},{\"id\":${CATEGORIES[1]}}])
echo ${CATS[@]}
echo SKU = ${sku}
echo Image = ${images}
echo Title = ${product_title}
echo Desc = ${short_description}
echo Weight = ${weight}
echo Price = ${regular_price}
echo Stock Qty = ${stock_qty}
echo Manage Stock = ${manage_stock}
wp media import "${images}" --featured_image --post_id=$(wp wc product create --sku="${sku}" --name="${product_title}" --short_description="${short_description}" --weight="${weight}" --regular_price="${regular_price}" --stock_quantity="${stock_qty}" --manage_stock="${manage_stock}" --dimensions=${DIMS[@]} --categories=${CATS[@]} --allow-root --user=siteadmin | awk '{print $4}') --user=siteadmin --allow-root
IFS=$OLDIFS
done < product-new-final.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment