Scrape products from URL




', $product)[1]; $name = explode('', $name)[0]; $price = explode('', $product)[1]; $price = explode('', $price)[0]; $price = explode('class="GBP show">', $price)[1]; $price = explode('', $price)[0]; $price = explode('£', $price)[1]; $price = floatval($price); $sale_price = explode('', $product)[1]; $sale_price = explode('
', $sale_price)[0]; $sale_price = explode('class="GBP show">', $sale_price)[1]; $sale_price = explode('', $sale_price)[0]; $sale_price = explode('£', $sale_price)[1]; $sale_price = floatval($sale_price); if($price == 0 && $sale_price>0){ $price = $sale_price; $sale_price = 0; } $sizes = explode(' itemSizeSelectWrapper">', $product)[1]; $sizes = explode('
Product Detail', $product)[1]; $description = explode('
', $description)[1]; $description = explode('
', $description)[0]; $description = trim(strip_tags($description, '


')); $size_guide = explode('

Size Guide

', $product)[1]; $size_guide = explode('
', $size_guide)[1]; $size_guide = explode('
', $size_guide)[0]; $size_guide = trim(strip_tags($size_guide, '


')); /* echo "

";
		var_dump($name);
		var_dump($sale_price); 
		var_dump($price); 
		var_dump($the_sizes); 
		var_dump($description); 
		var_dump($size_guide); 
		echo "

";

*/
$cats = explode('

', $cats)[0];
$cats = explode('

  • ', $cats);
    foreach($cats as $cat){
    $cat = explode('
  • ', $cat)[0];
    $cat = explode('>', $cat)[1];
    $cat = explode('<', $cat)[0]; $categories[] = trim($cat); } unset($categories[0]); if(count($categories)==4){ //$product_category = $categories[3]; $product_brand = $categories[3]; } else{ $product_category = $categories[3]; $product_brand = $categories[4]; } $gallery = explode('

    //unset($gallery[0]);
    $gallery_urls = array();

    foreach($gallery as $one_picture){
    //$source = explode('src="', $one_picture)[1];
    $source = explode('"', $one_picture)[0];

    if(strpos($source, '/images/products/originals')>-1){

    $gallery_urls[] = '/uploads'.$source;
    }

    }

    $post = array(
    'post_content' => $description,
    'post_status' => "publish",
    'post_title' => $name,
    'post_type' => "product",
    );

    //Create post
    $post_id = wp_insert_post( $post, $wp_error );

    echo "

    CLICK HERE

    ";

    update_post_meta( $post_id, 'original_url',$product_url);
    update_post_meta( $post_id, 'size_guide', $size_guide);
    $sku = explode('/item/', $product_url)[1];
    $sku = explode('/', $sku)[0];

    update_post_meta( $post_id, '_sku', $sku);

    update_post_meta( $post_id, '_product_attributes', array());

    update_post_meta( $post_id, '_regular_price',$price );
    if($sale_price>0){
    update_post_meta( $post_id, '_sale_price', $sale_price );
    }

    wp_set_object_terms( $post_id, 'variable', 'product_type' );

    echo "

    ";
    		$images = array(); //var_dump(strip_tags($gallery)); 
    		
    			
    			
    		
    			
    			$i = 0;
    			$product_gallery = array();
    		foreach($gallery_urls as $image){
    				
    				
    				$image = 'https://www.manifesto-clothing.co.uk/'.$image;
    				
    				$image_url = $image;
    				
    				$upload_dir = wp_upload_dir();
    
    				$image_data = file_get_contents( $image_url );
    
    				$filename = basename( $image_url );
    
    				if ( wp_mkdir_p( $upload_dir['path'] ) ) {
    				  $file = $upload_dir['path'] . '/' . $filename;
    				}
    				else {
    				  $file = $upload_dir['basedir'] . '/' . $filename;
    				}
    
    				file_put_contents( $file, $image_data );
    
    				$wp_filetype = wp_check_filetype( $filename, null );
    
    				$attachment = array(
    				  'post_mime_type' => $wp_filetype['type'],
    				  'post_title' => sanitize_file_name( $filename ),
    				  'post_content' => '',
    				  'post_status' => 'inherit'
    				);
    
    				$attach_id = wp_insert_attachment( $attachment, $file );
    				require_once( ABSPATH . 'wp-admin/includes/image.php' );
    				$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    				wp_update_attachment_metadata( $attach_id, $attach_data );
    				
    				$product_gallery[] = $attach_id;
    				wp_update_attachment_metadata( $attach_id, $attach_data ); 
    				if($i==0){
    					add_post_meta($post_id, '_thumbnail_id', $attach_id);
    				} 
    				
    				$i++; 
    			}
    			unset($product_gallery[0]);
    			var_dump(join(',', $product_gallery));
    			        update_post_meta($post_id, '_product_image_gallery', join(',', $product_gallery));
    
    			
    		var_dump($images);
    		echo "

    ";

    $the_data = array();
    $attributes = $the_sizes;

    // unset(count($oneAttribute)); // last empty

    $attributeName = "Size";

    //$attributeName = $attData[0];
    /*$attributeName = explode('<', $attributeName)[0]; $attributeName = rtrim(($attributeName)); $attributeName = ltrim($attributeName); $attributeName = trim($attributeName);*/ $product_id = $post_id; var_dump($variableAttributes); var_dump($attributeName); echo "test"; $fragrance_array = Array( 'name'=> $attributeName,
    'value'=> join('|', $the_sizes),
    'is_visible' => '1',
    'is_variation' => '1',
    'is_taxonomy' => '0'
    );
    $attributeName = sanitize_title($attributeName);
    wp_set_object_terms( $post_id, $the_sizes, 'pa_'.$attributeName );
    $the_data[$attributeName]=$fragrance_array;
    $result = update_post_meta( $post_id,'_product_attributes',$the_data);

    wp_set_object_terms( $post_id, $product_category, 'product_cat' );
    wp_set_object_terms( $post_id, $product_brand, 'pwb-brand' );
    wp_set_object_terms( $post_id, $product_brand, 'yith_product_brand' );

    var_dump($result);

    echo "trying variation";

    $parent_id = $post_id;
    $variation = array(
    'post_title' => $name . ' (variation)',
    'post_content' => '',
    'post_status' => 'publish',
    'post_parent' => $parent_id,
    'post_type' => 'product_variation'
    );

    $variation_id = wp_insert_post( $variation );
    update_post_meta( $variation_id, '_regular_price', $price );
    if($sale_price>0){
    update_post_meta( $variation_id, '_sale_price', $sale_price );
    }

    update_post_meta( $variation_id, '_stock_qty', 100000 );
    WC_Product_Variable::sync( $parent_id );

    echo "


    ";

    }

    }
    function removeBOM($data) {
    if (0 === strpos(bin2hex($data), 'efbbbf')) {
    return substr($data, 3);
    }
    return $data;
    }

    ?>

    Author: Wojciech Borowicz

    I hope this post will help you to do what you need.
    In case you want some assistance click here to get in touch 

    check out other blog posts