• Resolved mazikeen

    (@mazikeen)


    I was able to display a custom field on the “ADD & EDIT DISCOUNT PAGE” using this codes:

    function site_count_add_field() {
      echo 
      '<tr>
       <th scope="row" valign="top">
       <label for="edd-max-uses">Site Count</label>
       </th>
       <td>
       <input type="text" id="edd-site-count" name="sitecount" value=""  style="width: 40px;"/>
       <p class="description">The amount of free activation in this coupon. Leave blank for 0.</p>
       </td>
       </tr>';
    }
    add_action('edd_add_discount_form_before_max_uses', 'site_count_add_field');
    
    function site_count_edit_field($discount_id, $discount) {
      echo '
       <tr>
       <th scope="row" valign="top">
       <label for="edd-max-uses">Site Count</label>
       </th>
       <td>
       <input type="text" id="edd-site-count" name="sitecount" value="" style="width: 40px;"/>
       <p class="description">The amount of free activation in this coupon. Leave blank for 0.</p>
       </td>
       </tr>';
    }
    add_action('edd_edit_discount_form_before_max_uses', 'site_count_edit_field');

    However, it won’t save anything whatever i tried to enter. Anybody know which step should i do next to make it work?

    • This topic was modified 3 years, 1 month ago by mazikeen.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Mihai Joldis

    (@misulicus)

    Hey @mazikeen

    You need the functionality to save the actual data that you are adding.
    For that you can use the edd_post_insert_discount and edd_post_update_discount actions in EDD.

    You can find them in the includes/class-edd-discount.php file. At the start of your saving function you should validate the edd-discount-nonce that is being sent via the $_POST variable.

    Once you validate it and have the data you could do something like:

    
    function custom_discount_data( $meta, $discount_id ) {
    	$discount = new EDD_Discount( $discount_id );
    
    	if ( ! isset( $_POST['edd-discount-nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['edd-discount-nonce'] ) ), 'edd_discount_nonce' ) ) {
    		return;
    	}
    	
    	if ( isset( $_POST['sitecount'] )  ) {
    		$count = intval( $_POST['sitecount'] );
    		
    		$discount->update_meta( 'your_custom_meta_key_here', $count );
    	}
    
    }
    add_action( 'edd_post_insert_discount', 'custom_discount_data', 10, 2 );
    add_action( 'edd_post_update_discount', 'custom_discount_data', 10, 2 );
    

    You would need to edit your site_count_edit_field function and retrieve any stored sitecount values and show that in the input field.
    For that you can use the get_meta method from the $discount class:
    $discount->get_meta( ‘your_custom_meta_key_here’, true );

    The code above is untested but it should get you started.

    Regards

    Thread Starter mazikeen

    (@mazikeen)

    @mihai it worked perfectly and i was able to extend my codes even further. Im newbie and such explanation helps me learn and understand more about php & wordpress, Thank you very much!! !

    Plugin Support Mihai Joldis

    (@misulicus)

    Happy to help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add & save fields on discount?’ is closed to new replies.