Support » Plugin: DHL for WooCommerce » Non name (firstname lastname) on label when using dhl_postnum

  • Resolved gerd.neumann

    (@gerdneumann)


    Following the advice at https://wordpress.org/support/topic/post-number-is-missing-it-is-mandatory-for-packstation-delivery/#post-10752773 I added the following to our child theme:

    
    function fr_field_contains_postnumber($name) {
    	// postnumber is only numbers and at least 6 characters
    	// see https://www.giga.de/unternehmen/dhl/specials/dhl-postnummer-was-ist-das-anmeldung-und-funktion/
    	$contains = ctype_digit( $name ) && strlen($name) > 5;
    	return $contains;
    }
    
    add_filter('pr_shipping_dhl_label_args', 'fr_modify_post_number', 10, 2);
    function fr_modify_post_number($args, $order_id) {
    
    	if (! empty( $args['shipping_address']['company'] ) ) {
    		$companyName = $args['shipping_address']['company'];
    		if ( fr_field_contains_postnumber($companyName) ){
    			// $args['shipping_address']['dhl_postnum'] = (int) $companyName;
    			$args['shipping_address']['dhl_postnum'] = $companyName;
    		}
    	}
    
    	return $args;
    }
    
    

    This works so far that at least the error message Post Number is missing, it is mandatory for "Packstation" delivery. does not show up anymore and a PDF label is created.

    However, on the created PDF label the customer’s (recipient) name is missing. See this screenshot of the PDF:

    https://imgur.com/a/hjpOi8m

    As you can see, instead of the name the Postnumber is used twice.

    I debugged a bit and in the above filter the args['shipping_address'] still contains the correct name, first_name and last_name fields. They just do not seem to be outputted correctly on the PDF.

    Is this a bug in the plugin?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Shadi Manna

    (@shadim)

    What seems to happen is that the company name is also passed to DHL and it contains the post number so it seems to be printed twice. In your function, can you set the company number to an empty string at the end, like this:

    $args[‘shipping_address’][‘company’] = ”;

    Thread Starter gerd.neumann

    (@gerdneumann)

    Thanks, will try this. When company is emptied, will the name then be printed? Otherwise another workaround might be to set the $args[‘shipping_address’][‘company’] = $args[‘shipping_address’][‘name’] in order to get the name on the label. Not only for delivery but also for us so we see to whom a packet is shipping to (which is hard without a name but only a number on it)

    Besides this, during testing I asked myself if there a some postnumber test numbers. I do not like to use the some customer’s real postnumber and packstation. Are there some test number one could use. Kind of some sandbox / testing packstation and postnumber? I tried to search the DHL “entwickler” (developer) portal but did not find anything.

    Plugin Author Shadi Manna

    (@shadim)

    Unfortunately, there is not a sandbox to test with, but you can create the label and delete it after if you like.

    You can first try placing an empty string in ‘company’ and confirm ‘name’ is actually set as well as it should display. If it still does not print on the label let me know.

    Thread Starter gerd.neumann

    (@gerdneumann)

    Now it works, name is printed if company field is set to ''. Here’s the full code for reference. Maybe it is helpful to someone:

    
    // See https://wordpress.org/support/topic/post-number-is-missing-it-is-mandatory-for-packstation-delivery/#post-10752773
    
    function fr_field_contains_postnumber($name) {
    	// postnumber is only numbers and at least 6 characters
    	// see https://www.giga.de/unternehmen/dhl/specials/dhl-postnummer-was-ist-das-anmeldung-und-funktion/
    	$contains = ctype_digit( $name ) && strlen($name) > 5;
    	return $contains;
    }
    
    add_filter('pr_shipping_dhl_label_args', 'fr_modify_post_number', 10, 2);
    function fr_modify_post_number($args, $order_id) {
    
    	if (! empty( $args['shipping_address']['company'] ) ) {
    		$companyName = $args['shipping_address']['company'];
    		if ( fr_field_contains_postnumber($companyName) ){
    			$args['shipping_address']['dhl_postnum'] = $companyName;
    			// Also need to empty company name so that the customer name is printed,
    			// see https://wordpress.org/support/topic/non-name-firstname-lastname-on-label-when-using-dhl_postnum/#post-10787955
    			$args['shipping_address']['company'] = '';
    		}
    	}
    
    	return $args;
    }
    
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Non name (firstname lastname) on label when using dhl_postnum’ is closed to new replies.