Installeer Gravity forms + Custom post types. (gemakkelijk taxonomies inladen, etc)

Maak een post titel en vink aan dat er gekoppeld moet worden aan producten. Voeg eventueel custom fields toe zoals downloadable, etc. (zie voorbeeld JSON). hiernoem naar .json

formulier-aanmaken-product

Het product kan worden aangepast na het verzenden van het formulier. Zo kan de zichtbaarheid worden aangepast en kunnen er downloads worden gekoppeld.

add_action( 'gform_after_submission_4', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {
    // stel zichtbaarheid in
    $terms = array( 'exclude-from-search', 'exclude-from-catalog' ); // for hidden..
	wp_set_post_terms( $entry['post_id'], $terms, 'product_visibility', false );
	
	// voeg download toe
	$upload_path = GFFormsModel::get_upload_path( $entry[ 'form_id' ] );
	$upload_url = GFFormsModel::get_upload_url( $entry[ 'form_id' ] );
	$bestandsnaam_formatted = preg_replace('/[^A-Za-z0-9]/', "", rgar( $entry, '1' ));
	$file_name = 'persoonlijk-product-'.$bestandsnaam_formatted;
	//echo 'naam bestand = '.$file_name.'<br/>';
	$bestand = rgar( $entry, '23' );
	//echo 'bestand = '.$bestand.'<br/>';
	$download_id = md5( $bestand );
	//echo 'download_id = '.$download_id.'<br/>';
	// Creating an empty instance of a WC_Product_Download object
	$pd_object = new WC_Product_Download();
	
	// Set the data in the WC_Product_Download object
	$pd_object->set_id( $download_id );
	$pd_object->set_name( $file_name );
	$pd_object->set_file( $bestand );
	
	// Get an instance of the WC_Product object (from a defined product ID)
	$product = wc_get_product( $entry['post_id'] ); // <=== Be sure it's the product ID
	//echo $product;
	// Get existing downloads (if they exist)
	$downloads = $product->get_downloads();
	// Add the new WC_Product_Download object to the array
	$downloads[$download_id] = $pd_object;
	
	// Set the complete downloads array in the product
	$product->set_downloads($downloads);
	$product->save();
	//echo '<br/> product klaar';
	//echo $product;
}

 

Geef een reactie 0