Skip to content

Instantly share code, notes, and snippets.

@nicoandrade
Last active February 22, 2018 14:25
Show Gist options
  • Save nicoandrade/267239c60af0f871c1d803a7b8a26cfc to your computer and use it in GitHub Desktop.
Save nicoandrade/267239c60af0f871c1d803a7b8a26cfc to your computer and use it in GitHub Desktop.
Create a WooCommerce product from a Nike URL
//http://simplehtmldom.sourceforge.net
require get_template_directory() . '/simple_html_dom.php';
$urls = "https://store.nike.com/us/en_us/pd/sb-icon-mens-hoodie-agklpO/pid-11983778/pgid-12133502
https://store.nike.com/us/en_us/pd/training-utility-mens-long-sleeve-top-4yDbDJ/pid-11857251/pgid-11970545
https://store.nike.com/us/en_us/pd/jordan-sportswear-flight-tech-fleece-mens-full-zip-hoodie-xOpY4K/pid-11528527/pgid-12286332
https://store.nike.com/us/en_us/pd/jordan-sportswear-city-of-flight-mens-jacket-3MOVYl/pid-11924998/pgid-12130627
https://store.nike.com/us/en_us/pd/therma-mens-training-jacket-7DRA7m/pid-11764247/pgid-11970496
https://store.nike.com/us/en_us/pd/jordan-sportswear-wings-1988-mens-hoodie-0jmELv/pid-12127199/pgid-12196381
https://store.nike.com/us/en_us/pd/jordan-sportswear-wings-fleece-mens-full-zip-hoodie-lkTPBbBm/pid-11534038/pgid-12269000
https://store.nike.com/us/en_us/pd/hurley-protect-plus-mens-jacket-NwTzdkb8/pid-12062346/pgid-12063090
https://store.nike.com/us/en_us/pd/jordan-sportswear-wings-mens-pullover-hoodie-PKJZgb/pid-11925011/pgid-12130441
";
$url_array = explode( "\n", $urls );
if ( is_array( $url_array ) ) {
foreach ( $url_array as $url ) {
shopapp_scrap_item( $url );
}
}
function shopapp_scrap_item( $url = '' ) {
$response = wp_remote_get( $url );
if ( is_array( $response ) ) {
$body = $response['body']; // use the content
$html = str_get_html( $body );
if ( ! is_object( $html ) ) {
return false;
}
$pictures = $html->find( 'div.d-sm-h .grid-image' );
$images = array();
foreach( $pictures as $element ){
if ( ! is_object( $element->find( 'picture', 1) ) ) {
return false;
}
$img = $element->find( 'picture', 1)->find( 'img' );
$img_url = add_query_arg(
array(
'wid' => '1860',
'hei' => '1860',
'fmt' => 'jpeg',
'qlt' => '85',
'bgc' => 'F5F5F5',
),
$img[0]->attr['src']
);
array_push( $images, $img_url );
}
$title = $html->find( 'h1.productTitle' );
$title = $title[0]->plaintext;
$title = str_replace( 'Nike ', '', $title );
$price = $html->find( '.rr-subtitle span span' );
$price = $price[0]->plaintext;
$price = str_replace( '$', '', $price );
$category_text = $html->find( '.productSubTitle' );
$category_text = $category_text[0]->outertext;
$content = $html->find( 'div.description-preview' );
$content = $content[0]->innertext;
if( strpos( $category_text, 'Women' ) !== false ) {
$category = 'women';
}else{
$category = 'men';
}
$excerpt = substr( $content, 0, 200 ) . '...';
$post_prev = get_page_by_title( $title, 'OBJECT', 'product' );//Don't upload same post twice
if ( is_object( $post_prev ) ) {
return false;
}
$user_id = get_current_user_id(); // So, user is selected..
$post_id = wp_insert_post( array(
'post_author' => $user_id,
'post_title' => $title,
'post_content' => $content,
'post_excerpt' => $excerpt,
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'simple', 'product_type' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0' );
update_post_meta( $post_id, '_downloadable', 'no' );
update_post_meta( $post_id, '_virtual', 'no' );
update_post_meta( $post_id, '_regular_price', $price );
update_post_meta( $post_id, '_sale_price', '' );
update_post_meta( $post_id, '_purchase_note', '' );
update_post_meta( $post_id, '_featured', 'no' );
update_post_meta( $post_id, '_product_attributes', array() );
update_post_meta( $post_id, '_sale_price_dates_from', '' );
update_post_meta( $post_id, '_sale_price_dates_to', '' );
update_post_meta( $post_id, '_price', $price );
update_post_meta( $post_id, '_sold_individually', '' );
update_post_meta( $post_id, '_manage_stock', 'no' );
update_post_meta( $post_id, '_backorders', 'no' );
update_post_meta( $post_id, '_stock', '' );
if ( 'women' == $category ) {
wp_set_object_terms( $post_id, 'Women', 'product_cat' );
}else{
wp_set_object_terms( $post_id, 'Men', 'product_cat' );
}
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$i = 0;
$uploaded_images = array();
foreach ( $images as $image ) {
if ( 0 == $i ) {
$image_id = media_sideload_image( $image, $post_id, null, 'id' );
set_post_thumbnail( $post_id, $image_id );
}elseif( 4 > $i ){ //Only add 3 images to gallery
$image_id = media_sideload_image( $image, $post_id, null, 'id' );
array_push( $uploaded_images, $image_id );
}
$i++;
}
$images_to_save = implode ( ", ", $uploaded_images ); //images to save as gallery
update_post_meta( $post_id, '_product_image_gallery', $images_to_save );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment