I'm using WordPress 3.9.1 and the latest version of WooCommerce (2.1.10) and I'm trying to create a "Pay your invoice" page where people put their invoice number and amount and they go directly to the checkout page.
The way I'm doing it now:
I have a page with a form where people put in the amount:
<form action="#" onsubmit="location.href = 'http://protexfs.co/invoicepage/?date=' + this.elements.date.value; return false;">
<input type="text" name="date">
<input type="submit" value="Go">
</form>
The submit button leads to a page that automatically generates a Woocommerce product for the same amount and adds it to the cart and automatically redirects to the checkout page (I use the Insert PHP plugin for all my PHP needs):
[insert_php]
//empty cart
global $woocommerce;
$woocommerce->cart->empty_cart();
// Remove default cart message
$woocommerce->clear_messages();
//price
$invoiceprice = filter_input(INPUT_GET,"date",FILTER_SANITIZE_STRING);
//Generate title
$timestampedtitle = "Date: ".date("d/m/Y")." Amount: £".$invoiceprice;
//Generate message
$message = date_timestamp_get(date_create())." Date: ".date('m/d/Y h:i:s a', time())." Invoice amount: £".$invoiceprice;
$post = array(
'post_author' => '2',
'post_status' => "publish",
'post_title' => $timestampedtitle,
'post_content' => $message,
'post_parent' => '',
'post_type' => "product",
//'post_status' => 'private',
);
//Create post
$post_id = wp_insert_post( $post, $wp_error );
if($post_id){
$attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true);
add_post_meta($post_id, '_thumbnail_id', $attach_id);
}
wp_set_object_terms($post_id, 'simple', 'product_type');
update_post_meta( $post_id, '_visibility', 'search' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, '_virtual', 'yes');
update_post_meta( $post_id, '_regular_price', $invoiceprice );
update_post_meta( $post_id, '_sale_price', $invoiceprice );
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta( $post_id, '_weight', "" );
update_post_meta( $post_id, '_length', "" );
update_post_meta( $post_id, '_width', "" );
update_post_meta( $post_id, '_height', "" );
update_post_meta($post_id, '_sku', "");
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', $invoiceprice );
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', "" );
update_post_meta( $post_id, '_et_pb_page_layout', 'et_full_width_page' );
if( $woocommerce->cart ) {
$woocommerce->cart->add_to_cart( $post_id, $quantity=1 );}
$url = $woocommerce->cart->get_checkout_url();
header("Location: $url");
[/insert_php]
This seems to work perfectly as long as I am logged in. However, if the user is not logged in, the product still gets generated but can't be added to the cart, showing the following message:"Sorry, this product cannot be purchased." (which ruins the whole thing).
The weird thing is that any other product I create through the WooCommerce interface can be accessed by the guests (so if I have a publicly available product and I programatically change its price and add it to the cart, it works -> but that creates problems when 2 people click the button at the same time).
Before you ask, I have enabled Guest checkout in the WooCommerce setting.
Any ideas on how to fix this? (Or possibly a completely different way to achieve my goal?)
Vlad