Support

Account

Home Forums General Issues Get sub field for PDF generator

Helping

Get sub field for PDF generator

  • Hello, I have some code from a previous dev working on this who made a PDF generator for creating licenses sent to the purchasers email, includes info from a woocommerce order, and contains some info from an ACF form field and subfield, specifically the price. Getting the price to the form works fine.

    I added a new row to the form’s field sub-field name type_usage as I want the PDF to contain usage information. type_price is also in this section and is used in the PDF. I tried calling type_usage the same way but nothing comes up and I’ve been stuck.

    Here’s the code to the PDF generator

    /**
     * Generate song license PDF
     * @param integer $order_id
     * @param integer $product_id
     * @param integer $license_number
     */
    function generate_song_license_pdf($order_id, $product_id) {
    	
    	require_once('page-licenses-pdf.php');
    
    	$order = wc_get_order( $order_id );
    	$data = $order->get_data(); // order data
    	$song_info = get_transient('song_info_'.$product_id);
    	$license_number = get_post_meta($order_id, 'mp_license', true);
    	$license_types = implode(', ', $song_info['license_types']);
      $product_price = get_post_meta($product_id, '_price',true);
      $type_usage = get_post_meta($product_id, '_usage',true); //test 1
      $order_date_created = $data['date_created']->date('Y-m-d');
      $usage_subfield = get_sub_field('type_usage'); //test 2
    
    	//add the new array to the user meta data
    
      $html = "
    <h1><u>COMPANY &mdash; LICENSE AGREEMENT</u></h1>
    <h1>Here is the TYPE USAGE: ".$type_usage." or ".$usage_subfield."</h1>
    <p>DATE: <strong>".$order_date_created."</strong> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LICENSE No. <strong>".$license_number."&nbsp; &nbsp;&nbsp;</strong></p>
    <p>In consideration of the payment and the receipt by the undersigned of the sum of $".$product_price." and other good and valuable considerations as well as all license fees prior to the effectiveness hereof, COMPANY (&ldquo;Licensor&rdquo;) does hereby grant to <strong>".$data['billing']['first_name'] ." " . $data['billing']['last_name'] ."</strong> (&ldquo;Licensee&rdquo;) as agency of the production specified in paragraph #2 hereof, the non-exclusive, irrevocable right, license, privilege and authority to use the recorded musical selections(s) hereinafter specified, for the sole, limited and restricted purpose of mechanically reproducing such recorded selection(s) only in the specific production hereinafter designated as an integral part thereof, all in accordance with terms, conditions and limitations hereinafter set forth.</p>
    <ol>

    It’s also been added to my Get License Type Function

    /** REST API Licenses's endpoints */
    add_action('rest_api_init', function () {
    	//license-types/(?P<type>\+s)
    	register_rest_route( 'music-factory/v1', 'license-types/(?P<type>[a-zA-Z0-9-]+)',array(
    				  'methods'  => 'GET',
    				  'callback' => 'get_license_variants_by_type'
    		));
      });
    /**
     * Get License types 
     * @param $request
     * @return JSON $response
     * 
     */
      function get_license_variants_by_type($request) {
    
    	$post_id = get_id_by_slug('licenses');
    	$types = [];
    
    	if( have_rows($request['type'], $post_id) ) {
    		
    		while( have_rows($request['type'], $post_id)) : the_row();
    
    			$types[] = [
    				'type_name'=> get_sub_field('type_name', $post_id), 
            'type_price'=> get_sub_field('type_price', $post_id),
            'type_usage'=> get_sub_field('type_usage', $post_id),
           
    			];
    
    		endwhile;
    	}
    
    	$response = new WP_REST_Response($types);
        $response->set_status(200);
    
        return $response;
      }
    
    /**
     * Get License types 
     * @param $request
     * @return JSON $response
     * 
     */
    function get_project_types() {
    
    	$post_id = get_id_by_slug('licenses');
    	$project_type = sanitize_text_field($_POST['project_type']);
    	$types = [];
    	
    	if( have_rows($project_type, $post_id) ) {
    		
    		while( have_rows($project_type, $post_id)) : the_row();
    
    			$types[] = [
    				'type_name'=> get_sub_field('type_name', $post_id), 
    				'type_price'=> get_sub_field('type_price', $post_id),
            'type_description'=> get_sub_field('type_description', $post_id),
            'type_usage'=> get_sub_field('type_usage', $post_id),
           
    			];
    
    		endwhile;
    	}
    
    	echo json_encode($types);
    
    	wp_die();
    }
    add_action( 'wp_ajax_mb_request_quote', 'mb_request_quote' );
    add_action( 'wp_ajax_nopriv_mb_request_quote', 'mb_request_quote' );
    add_action('wp_ajax_mb_ajax_add_to_cart', 'mb_ajax_add_to_cart');
    add_action('wp_ajax_nopriv_mb_ajax_add_to_cart', 'mb_ajax_add_to_cart');
    add_action( 'wp_ajax_get_project_types', 'get_project_types' );
    add_action( 'wp_ajax_nopriv_get_project_types', 'get_project_types' );
    

    Any help is appreciated, thank you!

  • Hello, I have some code from a previous dev working on this who made a PDF generator for creating licenses sent to the purchasers email, includes info from a woocommerce order, and contains some info from an ACF form field and subfield, specifically the price. Getting the price to the form works fine.

    I added a new row to the form’s field sub-field name type_usage as I want the PDF to contain usage information. type_price is also in this section and is used in the PDF. I tried calling type_usage the same way but nothing comes up and I’ve been stuck.

    Here’s the code to the PDF generator

    /**
     * Generate song license PDF
     * @param integer $order_id
     * @param integer $product_id
     * @param integer $license_number
     */
    function generate_song_license_pdf($order_id, $product_id) {
    	
    	require_once('page-licenses-pdf.php');
    
    	$order = wc_get_order( $order_id );
    	$data = $order->get_data(); // order data
    	$song_info = get_transient('song_info_'.$product_id);
    	$license_number = get_post_meta($order_id, 'mp_license', true);
    	$license_types = implode(', ', $song_info['license_types']);
      $product_price = get_post_meta($product_id, '_price',true);
      $type_usage = get_post_meta($product_id, '_usage',true); //test 1
      $order_date_created = $data['date_created']->date('Y-m-d');
      $usage_subfield = get_sub_field('type_usage'); //test 2
    
    	//add the new array to the user meta data
    
      $html = "
    <h1><u>COMPANY &mdash; LICENSE AGREEMENT</u></h1>
    <h1>Here is the TYPE USAGE: ".$type_usage." or ".$usage_subfield."</h1>
    <p>DATE: <strong>".$order_date_created."</strong> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LICENSE No. <strong>".$license_number."&nbsp; &nbsp;&nbsp;</strong></p>
    <p>In consideration of the payment and the receipt by the undersigned of the sum of $".$product_price." and other good and valuable considerations as well as all license fees prior to the effectiveness hereof, COMPANY (&ldquo;Licensor&rdquo;) does hereby grant to <strong>".$data['billing']['first_name'] ." " . $data['billing']['last_name'] ."</strong> (&ldquo;Licensee&rdquo;) as agency of the production specified in paragraph #2 hereof, the non-exclusive, irrevocable right, license, privilege and authority to use the recorded musical selections(s) hereinafter specified, for the sole, limited and restricted purpose of mechanically reproducing such recorded selection(s) only in the specific production hereinafter designated as an integral part thereof, all in accordance with terms, conditions and limitations hereinafter set forth.</p>
    <ol>

    It’s also been added to my Get License Type Function

    /** REST API Licenses's endpoints */
    add_action('rest_api_init', function () {
    	//license-types/(?P<type>\+s)
    	register_rest_route( 'music-factory/v1', 'license-types/(?P<type>[a-zA-Z0-9-]+)',array(
    				  'methods'  => 'GET',
    				  'callback' => 'get_license_variants_by_type'
    		));
      });
    /**
     * Get License types 
     * @param $request
     * @return JSON $response
     * 
     */
      function get_license_variants_by_type($request) {
    
    	$post_id = get_id_by_slug('licenses');
    	$types = [];
    
    	if( have_rows($request['type'], $post_id) ) {
    		
    		while( have_rows($request['type'], $post_id)) : the_row();
    
    			$types[] = [
    				'type_name'=> get_sub_field('type_name', $post_id), 
            'type_price'=> get_sub_field('type_price', $post_id),
            'type_usage'=> get_sub_field('type_usage', $post_id),
           
    			];
    
    		endwhile;
    	}
    
    	$response = new WP_REST_Response($types);
        $response->set_status(200);
    
        return $response;
      }
    
    /**
     * Get License types 
     * @param $request
     * @return JSON $response
     * 
     */
    function get_project_types() {
    
    	$post_id = get_id_by_slug('licenses');
    	$project_type = sanitize_text_field($_POST['project_type']);
    	$types = [];
    	
    	if( have_rows($project_type, $post_id) ) {
    		
    		while( have_rows($project_type, $post_id)) : the_row();
    
    			$types[] = [
    				'type_name'=> get_sub_field('type_name', $post_id), 
    				'type_price'=> get_sub_field('type_price', $post_id),
            'type_description'=> get_sub_field('type_description', $post_id),
            'type_usage'=> get_sub_field('type_usage', $post_id),
           
    			];
    
    		endwhile;
    	}
    
    	echo json_encode($types);
    
    	wp_die();
    }
    add_action( 'wp_ajax_mb_request_quote', 'mb_request_quote' );
    add_action( 'wp_ajax_nopriv_mb_request_quote', 'mb_request_quote' );
    add_action('wp_ajax_mb_ajax_add_to_cart', 'mb_ajax_add_to_cart');
    add_action('wp_ajax_nopriv_mb_ajax_add_to_cart', 'mb_ajax_add_to_cart');
    add_action( 'wp_ajax_get_project_types', 'get_project_types' );
    add_action( 'wp_ajax_nopriv_get_project_types', 'get_project_types' );
    

    Any help is appreciated, thank you!

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.