lundi 29 décembre 2014

Setting two attributes as variation in woo commerce via php


"Product with Variations222", 'post_content' => "product post content goes here...", 'post_status' => "publish", 'post_excerpt' => "product excerpt content...", 'post_name' => "test_prod_vars2", //name/slug 'post_type' => "product" ); //Create product/post: $new_post_id = wp_insert_post( $post, $wp_error ); $logtxt = "PrdctID: $new_post_id\n"; //make product type be variable: wp_set_object_terms ($new_post_id,'variable','product_type'); //add category to product: $cat_ids = array( 46, 48,47 ); wp_set_object_terms( $new_post_id, $cat_ids, 'product_cat'); //################### Add size attributes to main product: #################### //Array for setting attributes $avail_attributes = array( '2xl', 'xl', 'lg', 'md', 'sm' ); $avail_attributes1 = array( '1', '2', '3', '4', '5' ); wp_set_object_terms($new_post_id, $avail_attributes, 'pa_size'); wp_set_object_terms($new_post_id, $avail_attributes1, 'pa_size1'); $thedata = Array('pa_size'=>Array( 'name'=>'pa_size', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' ),'pa_size1'=>Array( 'name'=>'pa_size1', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $new_post_id,'_product_attributes',$thedata); //########################## Done adding attributes to product ################# //set product values: update_post_meta( $new_post_id, '_stock_status', 'instock'); update_post_meta( $new_post_id, '_weight', "0.06" ); update_post_meta( $new_post_id, '_sku', "skutest1"); update_post_meta( $new_post_id, '_stock', "100" ); update_post_meta( $new_post_id, '_visibility', 'visible' ); //###################### Add Variation post types for sizes ############################# //insert 5 variations post_types for 2xl, xl, lg, md, sm: $i=1; while ($i 'Variation #' . $i . ' of 5 for prdct#'. $new_post_id, 'post_name' => 'product-' . $new_post_id . '-variation-' . $i, 'post_status' => 'publish', 'post_parent' => $new_post_id,//post is a child post of product post 'post_type' => 'product_variation',//set post type to product_variation 'guid'=>home_url() . '/?product_variation=product-' . $new_post_id . '-variation-' . $i ); //Insert ea. post/variation into database: $attID = wp_insert_post( $my_post ); $logtxt .= "Attribute inserted with ID: $attID\n"; //set IDs for product_variation posts: $variation_id = $new_post_id + 1; $variation_two = $variation_id + 1; $variation_three = $variation_two + 1; $variation_four = $variation_three + 1; $variation_five = $variation_four + 1; //Create 2xl variation for ea product_variation: update_post_meta($variation_id, 'attribute_pa_size', '2xl'); update_post_meta($variation_id, '_price', 21.99); update_post_meta($variation_id, '_regular_price', '21.99'); //add size attributes to this variation: wp_set_object_terms($variation_id, $avail_attributes, 'pa_size'); $thedata = Array('pa_size'=>Array( 'name'=>'2xl', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_id,'_product_attributes',$thedata); update_post_meta($variation_id, 'attribute_pa_size1', '1'); update_post_meta($variation_id, '_price', 21.99); update_post_meta($variation_id, '_regular_price', '21.99'); //add size attributes to this variation: wp_set_object_terms($variation_id, $avail_attributes, 'pa_size1'); $thedata = Array('pa_size'=>Array( 'name'=>'1', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_id,'_product_attributes',$thedata); //Create xl variation for ea product_variation: update_post_meta( $variation_two, 'attribute_pa_size', 'xl'); update_post_meta( $variation_two, '_price', 20.99 ); update_post_meta( $variation_two, '_regular_price', '20.99'); //add size attributes: wp_set_object_terms($variation_two, $avail_attributes, 'pa_size'); $thedata = Array('pa_size'=>Array( 'name'=>'xl', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_two,'_product_attributes',$thedata); update_post_meta( $variation_two, 'attribute_pa_size1', '2'); update_post_meta( $variation_two, '_price', 20.99 ); update_post_meta( $variation_two, '_regular_price', '20.99'); //add size attributes: wp_set_object_terms($variation_two, $avail_attributes, 'pa_size1'); $thedata = Array('pa_size1'=>Array( 'name'=>'2', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_two,'_product_attributes',$thedata); //Create lg variation for ea product_variation: update_post_meta( $variation_three, 'attribute_pa_size', 'lg'); update_post_meta( $variation_three, '_price', 18.99 ); update_post_meta( $variation_three, '_regular_price', '18.99'); wp_set_object_terms($variation_three, $avail_attributes, 'pa_size'); $thedata = Array('pa_size'=>Array( 'name'=>'lg', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_three,'_product_attributes',$thedata); //Create lg variation for ea product_variation: update_post_meta( $variation_three, 'attribute_pa_size1', '3'); update_post_meta( $variation_three, '_price', 18.99 ); update_post_meta( $variation_three, '_regular_price', '18.99'); wp_set_object_terms($variation_three, $avail_attributes, 'pa_size1'); $thedata = Array('pa_size1'=>Array( 'name'=>'3', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_three,'_product_attributes',$thedata); //Create md variation for ea product_variation: update_post_meta( $variation_four, 'attribute_pa_size', 'md'); update_post_meta( $variation_four, '_price', 18.99 ); update_post_meta( $variation_four, '_regular_price', '18.99'); wp_set_object_terms($variation_four, $avail_attributes, 'pa_size'); $thedata = Array('pa_size'=>Array( 'name'=>'md', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_four,'_product_attributes',$thedata); update_post_meta( $variation_four, 'attribute_pa_size1', '4'); update_post_meta( $variation_four, '_price', 18.99 ); update_post_meta( $variation_four, '_regular_price', '18.99'); wp_set_object_terms($variation_four, $avail_attributes, 'pa_size1'); $thedata = Array('pa_size1'=>Array( 'name'=>'4', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_four,'_product_attributes',$thedata); //set_featured_image($variation_four,'http://www.hatzipavlou.gr/test6/images/green.jpg'); //echo "aaa".$variation_four; //Create sm variation for ea product_variation: update_post_meta( $variation_five, 'attribute_pa_size', 'sm'); update_post_meta( $variation_five, '_price', 18.99 ); update_post_meta( $variation_five, '_regular_price', '18.99'); wp_set_object_terms($variation_five, $avail_attributes, 'pa_size'); $thedata = Array('pa_size'=>Array( 'name'=>'sm', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_five,'_product_attributes',$thedata); update_post_meta( $variation_five, 'attribute_pa_size1', '5'); update_post_meta( $variation_five, '_price', 18.99 ); update_post_meta( $variation_five, '_regular_price', '18.99'); wp_set_object_terms($variation_five, $avail_attributes, 'pa_size1'); $thedata = Array('pa_size1'=>Array( 'name'=>'5', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_five,'_product_attributes',$thedata); update_post_meta( $variation_five, 'attribute_pa_size1', 4); update_post_meta( $variation_five, 'attribute_pa_size1', '5'); update_post_meta( $variation_five, '_price', 18.99 ); update_post_meta( $variation_five, '_regular_price', '18.99'); wp_set_object_terms($variation_five, $avail_attributes, 'pa_size1'); $thedata = Array('pa_size1'=>Array( 'name'=>'4', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' )); update_post_meta( $variation_five,'_product_attributes',$thedata); $i++; }//end while i is less than or equal to 5(for 5 size variations) //############################ Done adding variation posts ############################ echo $new_post_id; $resultfoto ='http://www.hatzipavlou.gr/test6/images/green.jpg'; echo 'aaa'.$resultfoto; //$image_url='http://www.hatzipavlou.gr/test6/images/green.jpg'; $upload_dir = wp_upload_dir(); $image_data = file_get_contents($resultfoto); //echo($image_data); $filename = basename($resultfoto); if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename; else $file = $upload_dir['basedir'] . '/' . $filename; file_put_contents($file, $image_data); echo "aaa"; $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' ); //$post_id=80295; //$post_id=(int)$new_post_id; $attach_id = wp_insert_attachment( $attachment, $file, $new_post_id ); require_once(ABSPATH . 'wp-admin/includes/image.php'); set_post_thumbnail( $new_post_id, $attach_id ); //add product image: //require_once 'inc/add_pic.php'; require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); $thumb_url = "http://datakey.gr/art/test.jpg"; // Download file to temp location $tmp = download_url( $thumb_url ); echo $tmp; // Set variables for storage // fix file name for query strings preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches); $file_array['name'] = basename($matches[0]); $file_array['tmp_name'] = $tmp; // If error storing temporarily, unlink if ( is_wp_error( $tmp ) ) { @unlink($file_array['tmp_name']); $file_array['tmp_name'] = ''; $logtxt .= "Error: download_url error - $tmp\n"; }else{ $logtxt .= "download_url: $tmp\n"; } //use media_handle_sideload to upload img: $thumbid = media_handle_sideload( $file_array, $new_post_id, 'gallery desc' ); // If error storing permanently, unlink if ( is_wp_error($thumbid) ) { @unlink($file_array['tmp_name']); //return $thumbid; $logtxt .= "Error: media_handle_sideload error - $thumbid\n"; }else{ $logtxt .= "ThumbID: $thumbid\n"; } set_post_thumbnail($new_post_id, $thumbid); set_post_thumbnail($variation_five, $thumbid); echo $new_post_id." ".$thumbid; //optionally add second image: require_once(ABSPATH . "wp-admin" . '/includes/image.php'); $thumb_url2 = "http://datakey.gr/art/test.jpg"; // Download file to temp location $tmp2 = download_url( $thumb_url2 ); echo $tmp2; $logtxt .= "download_url2: $tmp2\n"; echo $logtx; // fix file name for query strings preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url2, $matches); $file_array2['name'] = basename($matches[0]); $file_array2['tmp_name'] = $tmp2; $imgID = media_handle_sideload( $file_array2, $new_post_id, 'desc' ); $logtxt .= "ThumbID2: $imgID\n"; update_post_meta( $new_post_id, '_product_image_gallery', $imgID); //prosthikh timhs gia na ksexwrisei auta pou anoikoun sthn idia kathgoria dhladh to also availabe $ran=rand(); $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM wp_postmeta where meta_value='$ran'" ); while($user_count>0){ $ran=rand(); $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM wp_postmeta where meta_value='$ran'" ); } add_post_meta( $new_post_id, 'product_atr',$ran); echo "id".$new_post_id; wp_set_post_terms($new_post_id, 'meaning2','product_tag',true); //append to log file(file shows up in wp-admin folder): $fh2 = fopen($insertLog, 'a') or die("can't open log file to append"); fwrite($fh2, $logtxt); fclose($fh2); //}//end addaprdct function function set_featured_image($post_id,$filename) { $wp_filetype = wp_check_filetype(basename($filename), null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $filename, $post_id ); // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once(ABSPATH . "wp-admin" . '/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); if (wp_update_attachment_metadata( $attach_id, $attach_data )) { // set as featured image return update_post_meta($post_id, '_thumbnail_id', $attach_id); } } ?>



Aucun commentaire:

Enregistrer un commentaire