• Resolved strap

    (@strap)


    Hi,

    we have bought the complete package of addons for wpadverts and most of the things work great!

    We have the need to ask for vat number for invoices, but only during the payment (we don’t want to ask that during registration for every user).

    I have a question: i’ve tryied adding the VAT number as text field in the Paypal addon wpadverts-paypal-standard.php

                        array(
                            "name" => "adverts_email",
                            "type" => "adverts_field_text",
                            "order" => 10,
                            "label" => __( "Email", "wpadverts-paypal-standard" ),
                            "is_required" => true,
                            "validator" => array( 
                                array( "name" => "is_required" ),
                                array( "name" => "is_email" )
                            )
    					),
                        array(
                            "name" => "adverts_vat",
                            "type" => "adverts_field_text",
                            "order" => 10,
                            "label" => __( "VAT Number", "wpadverts-paypal-standard" ),
                            "is_required" => true,
                            "validator" => array( 
                                array( "name" => "is_required" ),
                            )
                        )
                    )
                ) // end payment-form

    with the hope that it would be also saved as metadata in the wp_postmeta table.

    Unfortunately from here on i’m lost on how to proceed. Also, i’m aware that would be better to have it independent of the paypal choice as payment gateway, but it seemed easier since we will only support paypal for the moment.

    I’m new to wordpress so sorry if my approach may be wrong.

    Can you please help with a way to associate that number to the post as metadata? this way i should be able to show it in admin panel for each payment. From that should be possible to develop something to automate invoices.

    Thanks a lot for the support,
    have a nice day.

    Stefano

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter strap

    (@strap)

    Hi,

    i’ve added the same field to wp-content/plugins/wpadverts/addons/payments/payments.php and ajax.php

    Now i’ve discovered that the fields get saved in wp_postmeta, relatively to the payment post_id. So nice!

    What i’m trying now is to get the fields pre-compiled if the user has filled them in his profile information. I’ve created the field with the custom fields addon for the “Author Profile” form.

    All good, what happens is that the field gets added to wp_postmeta and not wp_usermeta, so from payments.php the data are not found (since it uses get_userdata() row 2179).

    Hope it is clear enough 😛

    Thanks again

    
        if( $data["buyer_id"] > 0 ) {
    
            $user_info = get_userdata( $data["buyer_id"] );
    
        }
    
        if( empty( $data["buyer_name"] ) && $user_info ) {
    
            $data["buyer_name"] = trim( sprintf( "%s %s", $user_info->first_name, $user_info->last_name ) );
    
        }    
    
        if( empty( $data["buyer_email"] ) && $user_info ) {
    
            $data["buyer_email"] = $user_info->user_email;
    
        }
    	
        if( empty( $data["buyer_vat"] ) && $user_info ) {
    
            $data["buyer_vat"] = $user_info->user_vat;
    
        }
    
    • This reply was modified 6 years, 8 months ago by strap. Reason: code added
    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    1. it is best to add the VAT field using a filter or action, as your changes in the original files will be overwritten on the PayPal extension update.

    The code below will add the VAT field to the Payment Form

    
    add_filter( "adverts_form_load", function( $form ) {
        if( $form["name"] != "adverts-paypal-standard" ) {
            return $form;
        }
        $form["field"][] = array(
            "name" => "adverts_vat",
            "type" => "adverts_field_text",
            "order" => 10,
            "label" => __( "VAT Number", "wpadverts-paypal-standard" ),
            "is_required" => true,
            "validator" => array( 
                array( "name" => "is_required" ),
            )
        );
        return $form;
    }, 10 );
    

    You can add it in your theme functions.php file or even better create a new WordPress plugin and put the code snippet there.

    2. i understand you want to prefill the values in the payment form (the one to which you have added the VAT field)?

    If so then you can do that using “adverts_form_bind” filter, for example the code below will prefill the VAT field value with “VAT” text

    
    add_filter( "adverts_form_bind", function( $form ) {
        if( $form->get_scheme( "name" ) != "adverts-paypal-standard" ) {
            return $form;
        }
        if( $form->get_value( "adverts_vat" ) == "" ) {
            $form->set_value( "adverts_vat", "VAT" );
        }
        return $form;
    } );
    

    You will need to mix your code with mine and replace the line

    
    $form->set_value( "adverts_vat", "VAT" );
    

    with something like

    
    $form->set_value( "adverts_vat", $user_info->user_vat );
    

    One note, if you have the default VAT value saved in the Author profile (using WPAdverts Authors plugin), then the VAT value will be actually saved in the wp_postmeta.

    In this case to get a value of user_vat field stored in the current user Authors profile you would need to use the code below

    
    $args = array(
        'author'         =>  wp_get_current_user()->ID,
        'post_type'      => 'advert-author',
        'posts_per_page' => 1,
        'post_status'    => array( 'publish', 'advert-hidden' ),
    );
    
    $authors = get_posts( $args );
            
    if( isset( $authors[0] ) {
        $author = $authors[0];
        $user_vat = get_post_meta( $author->ID, "user_vat", true );
    } else {
        $author = null;
        $user_vat = null;
    }
    

    Hope this helps.

    Thread Starter strap

    (@strap)

    Hello Greg,

    thanks for the really helpful answer.

    I’ve created a custom plugin where i can put all the customizations to wpadverts for that particular website.

    Starting from your input, i’ve been able to add more fields as billing address and company name. Everything works!

    So i reverted to the stock paypal plugin and i’m ready for future updates.

    For payments history, i’ve tryied to understand if it was possible to avoid tampering into the payments addon code. Unfortunately, i was unable to understand how to proceed. I have a working solution for now, but in case of updates it will need maintenance. If you can advice for a clean solution, i would really appreciate.

    Now i’ve modfified to get the followings:

    payments history list https://postimg.cc/LJ3WMxYJ

    payment history edit https://postimg.cc/CdvD9kxw

    Thanks a lot!
    Have a nice day

    P.S. i’m also adding the custom plugin code for others reference:

    
    <?php
    /*
     * Plugin Name:
     * Plugin URI:
     * Description:
     * Author:
     */
     
    add_filter( "adverts_form_load", function( $form ) {	
        if( $form["name"] != "adverts-paypal-standard" ) {
    		return $form;
        }
    	
    	$form["field"][] = array(
            "name" => "adverts_billingadd_name",
            "type" => "adverts_field_text",
            "order" => 10,
            "label" => __( "Company Name / Full Name", "wpadverts-paypal-standard" ),
            "is_required" => true,
            "validator" => array( 
                array( "name" => "is_required" ),
            )
        );
    	
        $form["field"][] = array(
            "name" => "adverts_billingadd_vat",
            "type" => "adverts_field_text",
            "order" => 10,
            "label" => __( "VAT Number", "wpadverts-paypal-standard" ),
            "is_required" => true,
            "validator" => array( 
                array( "name" => "is_required" ),
            )
        );
    	
    	$form["field"][] = array(
            "name" => "adverts_billingadd_street",
            "type" => "adverts_field_text",
            "order" => 10,
            "label" => __( "Street Name", "wpadverts-paypal-standard" ),
            "is_required" => true,
            "validator" => array( 
                array( "name" => "is_required" ),
            )
        );
    	
    	$form["field"][] = array(
            "name" => "adverts_billingadd_streetn",
            "type" => "adverts_field_text",
            "order" => 10,
            "label" => __( "Street Number", "wpadverts-paypal-standard" ),
            "is_required" => true,
            "validator" => array( 
                array( "name" => "is_required" ),
            )
        );
    	
    	$form["field"][] = array(
            "name" => "adverts_billingadd_postcode",
            "type" => "adverts_field_text",
            "order" => 10,
            "label" => __( "Postal Code", "wpadverts-paypal-standard" ),
            "is_required" => true,
            "validator" => array( 
                array( "name" => "is_required" ),
            )
        );
    	
    	$form["field"][] = array(
            "name" => "adverts_billingadd_city",
            "type" => "adverts_field_text",
            "order" => 10,
            "label" => __( "City", "wpadverts-paypal-standard" ),
            "is_required" => true,
            "validator" => array( 
                array( "name" => "is_required" ),
            )
        );
    	
    	$form["field"][] = array(
            "name" => "adverts_billingadd_state",
            "type" => "adverts_field_text",
            "order" => 10,
            "label" => __( "State", "wpadverts-paypal-standard" ),
            "is_required" => true,
            "validator" => array( 
                array( "name" => "is_required" ),
            )
        );
    	
        return $form;
    }, 10 );
    
    add_filter( "adverts_form_bind", function( $form ) {
        if( $form->get_scheme( "name" ) != "adverts-paypal-standard" ) {
            return $form;
        }
    	
    	$args = array(
    		'author'         =>  wp_get_current_user()->ID,
    		'post_type'      => 'advert-author',
    		'posts_per_page' => 1,
    		'post_status'    => array( 'publish', 'advert-hidden' ),
    	);
    
    	$authors = get_posts( $args );
    			
    	if( isset( $authors[0] ) ) {
    		$author = $authors[0];
    		$user_billingadd_name = get_post_meta( $author->ID, "user_billingadd_name", true );
    		$user_billingadd_vat = get_post_meta( $author->ID, "user_billingadd_vat", true );
    		$user_billingadd_street = get_post_meta( $author->ID, "user_billingadd_street", true );
    		$user_billingadd_streetn = get_post_meta( $author->ID, "user_billingadd_streetn", true );
    		$user_billingadd_postcode = get_post_meta( $author->ID, "user_billingadd_postcode", true );
    		$user_billingadd_city = get_post_meta( $author->ID, "user_billingadd_city", true );
    		$user_billingadd_state = get_post_meta( $author->ID, "user_billingadd_state", true );
    	} else {
    		$author = null;
    		$user_billingadd_name = null;
    		$user_billingadd_vat = null;
    		$user_billingadd_street = null;
    		$user_billingadd_streetn = null;
    		$user_billingadd_postcode = null;
    		$user_billingadd_city = null;
    		$user_billingadd_state = null;
    	}
    	
        if( $form->get_value( "adverts_billingadd_name" ) == "" ) {
            $form->set_value( "adverts_billingadd_name", $user_billingadd_name );
        }
    	if( $form->get_value( "adverts_billingadd_vat" ) == "" ) {
            $form->set_value( "adverts_billingadd_vat", $user_billingadd_vat );
        }
    	if( $form->get_value( "adverts_billingadd_street" ) == "" ) {
            $form->set_value( "adverts_billingadd_street", $user_billingadd_street );
        }
    	if( $form->get_value( "adverts_billingadd_streetn" ) == "" ) {
            $form->set_value( "adverts_billingadd_streetn", $user_billingadd_streetn );
        }
    	if( $form->get_value( "adverts_billingadd_postcode" ) == "" ) {
            $form->set_value( "adverts_billingadd_postcode", $user_billingadd_postcode );
        }
    	if( $form->get_value( "adverts_billingadd_city" ) == "" ) {
            $form->set_value( "adverts_billingadd_city", $user_billingadd_city );
        }
    	if( $form->get_value( "adverts_billingadd_state" ) == "" ) {
            $form->set_value( "adverts_billingadd_state", $user_billingadd_state );
        }
    	
        return $form;
    } );
     
     ?>
    
    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    1. in the Payment Edit panel there is a bug, the VAT field (and other fields) you added to the PayPal form in the frontend should also show in wp-admin when editing the payment.

    This will be fixed in next release, if you need the fix now you can email me using contact form here https://wpadverts.com/contact/ i will send you back a file which fixes it.

    2. additional columns you can add to the payments history list using adext_payment_list_thead and adext_payment_list_tbody filters

    
    add_action( 'adext_payment_list_thead', function( ) {
      echo '<th style="" class="" scope="col">Custom Column #1</th>';
      echo '<th style="" class="" scope="col">Custom Column #2</th>';
    } );
    add_action( 'adext_payment_list_tbody', function( $item ) {
      echo sprintf( '<td>%s</td>', $item->ID );
      echo sprintf( '<td>%s</td>', get_post_meta( $item->ID, 'adverts_vat', true ) );
    } );
    
    Thread Starter strap

    (@strap)

    Hello Greg,

    thanks again for the really quick support, your code works perfectly, i’ve written you an email for the custom fix.

    Bye,
    Stefano

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    thanks, i got your message, you should receive a reply with the fix attached soon.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Ask for VAT number with payment’ is closed to new replies.