In our example we want to create a 'product page' for this unique new product and allow the user to configure any other selectables like size and color before going to the shopping basket.
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
</head>
<body>
<?php
require( dirname(__FILE__) . '/wp-load.php' );
/**
* Add a product to the cart.
*
* @param string $product_id contains the id of the product to add to the cart
* @param string $quantity contains the quantity of the item to add
* @param int $variation_id
* @param array $variation attribute values
* @param array $cart_item_data extra cart item data we want to pass into the item
* @return bool
*/
global $woocommerce;
$my_post = array(
'post_title' => 'Persoonlijke Wijndoos Ontwerp - ' . $_REQUEST['sku'] ,
'post_content' => 'Variablele wijndoosontwerp' . $_REQUEST['sku'] .'.',
'post_status' => 'publish',
'post_author' => 1,
'post_type' =>'product'
);
// Insert the post into the product database
$product_ID = wp_insert_post( $my_post );
if ( $product_ID ){
$category_ids = array((int)66);
wp_set_post_terms( $product_ID, $category_ids, 'product_cat');
add_post_meta($product_ID, '_sku', $_REQUEST['sku'] );
add_post_meta($product_ID, '_regular_price', 6.95 );
add_post_meta($product_ID, '_price', 6.95 );
add_post_meta($product_ID, '_stock_status', 'instock' );
// Too Cart
/*if ($woocommerce->cart ){
$woocommerce->cart->add_to_cart( $product_ID );
exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );
}*/
// Image
$image_url = "http://xebius.odoa.eu/Previews/".$_REQUEST['tmb'];
$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, $product_ID );
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 );
set_post_thumbnail( $product_ID, $attach_id );
// Too Product Page
wp_safe_redirect( get_permalink($product_ID), 302 );
exit;
}
?>
</body>
</html>