• Resolved sacconi

    (@sacconi)


    At the moment I have a specific field for each agency (author) to define price of final cleaning. This price sometimes is different from apartment to apartment (from post to post). So What I want to do is keeping printing “final cleaning” at author level, but to these 2 words I want to add the price of final cleaning in euro using a selector in the post editor (custom number meta box)

    The function for the final cleaning is

    $pulizia_esc = get_the_author_meta( 'pulizia_esc', $post->post_author );
    if ( ! empty( $pulizia_esc)) {
       $pulizia_esc = '<div class="pulizia_esc">'. $pulizia_esc .'</div>';
    }
    

    Is it possible to add the value of the meta key I will use for the amount of final cleaning (i.e: amount_cleaning) and add it in the function?

    $pulizia_esc will be no longer only = get_the_author_meta( 'pulizia_esc', $post->post_author );

    but we have to add ideally (‘amount_cleaning’, and here define the place of the meta data,I suppose, maybe just $post )

    if it were a simple addition I would write: $pulizia_esc +amount_cleaning €

    € is a postfix

    This is where I go to operate in each post (in red): https://ibb.co/7xhBHpB

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter sacconi

    (@sacconi)

    I tryed with this but I failed:

    $pulizia_esc = get_the_author_meta( 'pulizia_esc', $post->post_author ), get_post_meta( $post->ID, "function_pulizia", true ) ;
    if ( ! empty( $pulizia_esc)) {
       $pulizia_esc = '<div class="pulizia_esc">'. $pulizia_esc .'</div>';
    }

    @sacconi can I just confirm what I understand you want to do here.

    1. You are storing a meta value on the author with the key pulizia_esc
    2. You also want to store a meta value on the post with the key function_pulizia
    3. Based on your description, they are all Euro values (eg amount in Euro)
    4. You want to be able to output the total of pulizia_esc on the author and function_pulizia on the post and echo this in the div with class pulizia_esc

    In that case, something like this should work:

    $pulizia_esc = intval( get_the_author_meta( 'pulizia_esc', $post->post_author ) ) + intval( get_post_meta( $post->ID, "function_pulizia", true ) );
    if ( $pulizia_esc !== 0 ) {
    	$pulizia_esc = '<div class="pulizia_esc">' . $pulizia_esc . '</div>';
    }

    What this code does is convert the two meta values to integers using PHP intval function, and then add the two integers together using the +, to get the total.

    Then the conditional checks if the value is not zero, and if it isn’t, performs the string concatenation using the . with the integer value. When you concatenate an integer to a string, PHP will convert the integer back to a string.

    What I would suggest as a small improvement is to also set $pulizia_esc to an empty string if the total is zero, so that you don’t get 0 output to the screen

    $pulizia_esc = intval( get_the_author_meta( 'pulizia_esc', $post->post_author ) ) + intval( get_post_meta( $post->ID, "function_pulizia", true ) );
    if ( $pulizia_esc !== 0 ) {
    	$pulizia_esc = '<div class="pulizia_esc">' . $pulizia_esc . '</div>';
    } else {
    	$pulizia_esc = '';
    }

    I would also point out that if you plan to include decimals (i.e. cents) to the values, you might want to use floatval and not intval.

    Last note, to make the above code cleaner and less prone to error, I would define a custom function to perform the calculation and return the string.

    function generate_pulizia( $post ) {
    	$pulizia_total = intval( get_the_author_meta( 'pulizia_esc', $post->post_author ) ) + intval( get_post_meta( $post->ID, "function_pulizia", true ) );
    	if ( $pulizia_total === 0 ) {
    		// If the total is 0 return an empty string.
    		return '';
    	}
    	// Otherwise return the total.
    	return '<div class="pulizia_esc">' . $pulizia_total . '</div>';
    }

    And then call that function wherever you need to generate the output

    $pulizia_esc = generate_pulizia( $post );
    Thread Starter sacconi

    (@sacconi)

    I think I shouldnt have used the word “addition” because I’m trying to concatenate a TEXT and a NUMBER (not 2 numbers). I should read “pulizia finale:” (final cleaning), this is the output from ( get_the_author_meta( ‘pulizia_esc’, $post->post_author ) ), but it’s not a number. For this reason probably now I read correctly only the output of get_post_meta( $post->ID, “function_pulizia”, true ) ), which is a number…

    Important to insert also if ( ! empty( $pulizia_esc)) , it has no relationship with a numeric value

    But we are reaching the result, the post meta is printed in the correct position in the post

    Ah. In that case, you can concatenate a string of text with a number using .

    $pulizia_esc = get_the_author_meta( 'pulizia_esc', $post->post_author ) . get_post_meta( $post->ID, "function_pulizia", true ) ;

    As PHP is generally expecting to output text to a browser, it will juggle the type of the number to a string when concatenating it to another string.

    If you want to make sure the int is a string, you can use strval or string casting.

    It’s worth noting that the meta data value in the database is possibly stored as a string anyway, even if you’ve set it as a number, as the data type for that field is longtext.

    With regards to using empty, there are some arguments to be made that one should not use it, and rather use something else more specific, but that’s up to each developer to decide.

    I hope this is all helpful.

    Thread Starter sacconi

    (@sacconi)

    Ok, it’s working fine! I just would try to ameliorate the esthetic, how can I put a space betwen and after . $pulizia_esc .€.

    in

    $pulizia_esc = '<div class="pulizia_esc">'. $pulizia_esc .€.'</div>';
    

    the € is not output correcly, I dont know why…

    So what you will need to do there is place the Euro symbol inside single quotation marks as well, to indicate it’s a string (text).

    $pulizia_esc = '<div class="pulizia_esc">' . $pulizia_esc . '€' . '</div>';

    Anything with a $ in front of it in PHP is a variable, so you don’t need quotation marks, but any text strings need quotation marks.

    Thread Starter sacconi

    (@sacconi)

    Even with quotation marks it cant output the symbol of euro. Another question, how can I put a blank space among the 2 values of the 2 functions? I mean between get_the_author_meta and get_post_meta in $pulizia_esc = get_the_author_meta( 'pulizia_esc', $post->post_author ) . get_post_meta( $post->ID, "function_pulizia", true ) ;

    It should output the euro symbol. Here is an example of it working on my site: https://tinyurl.com/ynssoaph

    This was the code I used

    $pulizia_esc = 'My Text';
    $pulizia_esc = '<div class="pulizia_esc">' . $pulizia_esc . '€' . '</div>';
    echo $pulizia_esc;

    To add a space between the two values, you can concatenate a space, again in quotation marks:

    $pulizia_esc = get_the_author_meta( 'pulizia_esc', $post->post_author ) . ' ' . get_post_meta( $post->ID, "function_pulizia", true ) ;
    Thread Starter sacconi

    (@sacconi)

    It’s allright, thank you

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘connecting more variables’ is closed to new replies.