Thread Starter
Nitrat
(@nitrat)
In order items post meta this field looks like
» Сумма страхования
How to extract in pdf invoice this field?
This code extract all fields – <?php echo $item[‘meta’];?>
But i need the one
Thread Starter
Nitrat
(@nitrat)
I found a solution whit code:
<?php
$items = $wpo_wcpdf->get_order_items();
if( sizeof( $items ) > 0 ) {
foreach( $items as $item ) {
if (isset($item['item']['item_meta']['Сумма страхования'])) {
echo $item['item']['item_meta']['Сумма страхования'][0] . '<br />';
}
if (isset($item['item']['item_meta']['Name'])) {
echo 'Name: ' . $item['item']['item_meta']['Name'][0] . '<br />';
}
}
}
?>
Thread Starter
Nitrat
(@nitrat)
But how to get the value of the srok (the number of months)? The code below don’t work
<?php
$items = $wpo_wcpdf->get_order_items();
if( sizeof( $items ) > 0 ) {
foreach( $items as $item ) {
$srok = $item['item']['item_meta']['Срок страхования, мес.'];
if ( $srok == '12') {
$final = date("d.m.Y", strtotime("+12 month -1 day", $billing_ot) );}
}
}
?>
Plugin Contributor
Ewout
(@pomegranate)
Looks like $billing_ot isn’t loaded anywhere in this code!
I would recommend using wpo_wcpdf_after_item_meta (which also gets the $item as a parameter) in combination with wc_get_order_item_meta()
Hope that helps!
Ewout
Thread Starter
Nitrat
(@nitrat)
$billing_ot loaded earlier (just the code not shown):
$billing_ot = get_post_meta($wpo_wcpdf->export->order->id,'_billing_field_474',true);
The code below shows the value of $srok but how to use it in logical operations:
<?php
$items = $wpo_wcpdf->get_order_items();
if( sizeof( $items ) > 0 ) {
foreach( $items as $item ) {
if (isset($item['item']['item_meta']['Сумма страхования'])) {
echo $item['item']['item_meta']['Сумма страхования'][0] . ' ₽';
}
if (isset($item['item']['item_meta']['Name'])) {
echo 'Name: ' . $item['item']['item_meta']['Name'][0] . '<br />';
}
}
}?>
Thread Starter
Nitrat
(@nitrat)
This code works:
<?php
$items = $wpo_wcpdf->get_order_items();
if( sizeof( $items ) > 0 ) {
foreach( $items as $item ) {
if (isset($item['item']['item_meta']['Срок страхования, мес.'])) {
$srok = $item['item']['item_meta']['Срок страхования, мес.'][0];}
echo $srok;
if ( $srok == '3') {
$final = date("d.m.Y", strtotime("+3 month -1 day", $billing_ot) );}
if ( $srok == '6') {
$final = date("d.m.Y", strtotime("+6 month -1 day", $billing_ot) );}
}
}?>
But date set as: 31-03-2016 to 31.03.1970
Why the year 1970, and not 2016?
Thread Starter
Nitrat
(@nitrat)
Wow, this code works:
<?php
$items = $wpo_wcpdf->get_order_items();
if( sizeof( $items ) > 0 ) {
foreach( $items as $item ) {
if (isset($item['item']['item_meta']['Срок страхования, мес.'])) {
$srok = $item['item']['item_meta']['Срок страхования, мес.'][0];}
echo $srok;
$invoice_date = get_post_meta($wpo_wcpdf->export->order->id,'_wcpdf_invoice_date',true);
if ( $srok == '3') {
$final = date_i18n( get_option( 'date_format' ), strtotime( $billing_ot . ' + 3 months -1 day') );}
if ( $srok == '6') {
$final = date_i18n( get_option( 'date_format' ), strtotime( $billing_ot . ' + 6 months -1 day') );}
if ( $srok == '9') {
$final = date_i18n( get_option( 'date_format' ), strtotime( $billing_ot . ' + 9 months -1 day') );}
}
}
?>
Last question. How to format 01-04-2016 to 01.04.2016 in pdf invoice?
And how to format 01.04.2016 to 01-04-2016 in pdf invoice?
Plugin Contributor
Ewout
(@pomegranate)
get_option( 'date_format' ) takes the setting from WordPress under Settings > General. You can use ‘d.m.Y’ or ‘d-m-Y’ instead too if you like.
Ewout
Thread Starter
Nitrat
(@nitrat)
No, this code…
$final = date_i18n( get_option( 'date_format' ), strtotime( $billing_ot . ' + 9 months -1 day') );}
It gives the date as 01-04-16
How to format $final to 01.04.06?
Thread Starter
Nitrat
(@nitrat)
It seems to work)
$search = array(".");
$replace = array("-");
$final = str_replace($search, $replace, $final);
Plugin Contributor
Ewout
(@pomegranate)
Hi Alexey, like I said, that date format call
get_option( 'date_format' )
is taking your WordPress settings. If you need a different format than in your wordpress settings, you can simply enter the format directly (instead of calling the option):
$final = date_i18n( 'd-m-Y', strtotime( $billing_ot . ' + 9 months -1 day') );}
Have a great weekend 🙂
Ewout