Skip to content

Instantly share code, notes, and snippets.

@Yame-
Created June 16, 2015 21:53
Show Gist options
  • Save Yame-/b02b73a670667d7336f4 to your computer and use it in GitHub Desktop.
Save Yame-/b02b73a670667d7336f4 to your computer and use it in GitHub Desktop.
Add a WooCommerce product with Contact Form 7
<?php
/* Capture CF7 data */
add_action( 'wpcf7_mail_sent', 'wpcf7_capture_data' );
function wpcf7_capture_data( $contact_form ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
// The 999 represents our CF7 form ID
if( $posted_data['_wpcf7'] == 999 ) {
$args = array(
'post_author' => 1,
'post_content' => $posted_data['game-content'],
'post_status' => "draft", // (Draft | Pending | Publish)
'post_title' => $posted_data['game-title'],
'post_parent' => '',
'post_type' => "product"
);
// Create a simple WooCommerce product
$post_id = wp_insert_post( $args );
// Setting the product type
wp_set_object_terms( $post_id, 'simple', 'product_type' );
// Setting the product price
update_post_meta( $post_id, '_price', $posted_data['game-price'] );
update_post_meta( $post_id, '_regular_price', $posted_data['game-price'] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment