Operator IF
-
Hi!
I need to use a logical operation in the following situation. If the product ID = 931, the variable final = final1. If the product ID = 932, the variable = final=final2. I did so, but does not work:<?php
$product_id = $item[‘product_id’];
if ( $product_id == 931 ) {$final = $final;}
else ( $product_id = 932 ) {$final = $final2;}
?>https://www.ads-software.com/plugins/woocommerce-pdf-invoices-packing-slips/
-
You have the wrong syntax, it has nothing to do with the plugin. The right, really basic php syntax for condition is:
if (this) {then do that;} else {do this if the condition is not met;}
Alternatively you can use
if (condition) {do this;} elseif (this second condition is met) {do this alternative;} else {do this if none of both conditions were met;}Flyer is correct. Basically you’re saying:
- get the product id
- if the product id is 931, final = final
- otherwise make product id 932 (which will be true), then final = final2
step 3 is not proper php syntax. Note the difference between == (compare if equal) and = (make equal/ new value)
What you want:
<?php $product_id = $item['product_id']; if ( $product_id == 931 ) { $final = $final; } elseif ( $product_id == 932 ) { $final = $final2; } else { // what if product_id is neither 931/932? } ?>
This code dont work.
$product_id not determined. I’m trying to just display it:
<div><?php echo “Товар ID: “; echo $product_id;?></div>
But the empty space.
I have two products with different validity period (for insurance) – 931 (year) and 932 (3 months). I need depending on the product display Validity of the contract, as well as change the conclusion of some fields.Where did you place this code exactly? It needs to be in the items loop, otherwise the $item value is not available.
In the list of order items it will work properly, you can check that with:<div><?php echo "Товар ID: ".$item['product_id'];?></div>
No, dont display
That’s all code:<?php global $wpo_wcpdf; ?> <?php $billing_first_name = get_post_meta($wpo_wcpdf->export->order->id,'_billing_first_name',true); $billing_last_name = get_post_meta($wpo_wcpdf->export->order->id,'_billing_last_name',true); $billing_company = get_post_meta($wpo_wcpdf->export->order->id,'_billing_company',true); $billing_address = get_post_meta($wpo_wcpdf->export->order->id,'_billing_address_1',true); $billing_address2 = get_post_meta($wpo_wcpdf->export->order->id,'_billing_address_2',true); $billing_city = get_post_meta($wpo_wcpdf->export->order->id,'_billing_city',true); $billing_postcode = get_post_meta($wpo_wcpdf->export->order->id,'_billing_postcode',true); $billing_country = get_post_meta($wpo_wcpdf->export->order->id,'_billing_country',true); $billing_state = get_post_meta($wpo_wcpdf->export->order->id,'_billing_state',true); $billing_email = get_post_meta($wpo_wcpdf->export->order->id,'_billing_email',true); $billing_phone = get_post_meta($wpo_wcpdf->export->order->id,'_billing_phone',true); $billing_paymethod = get_post_meta($wpo_wcpdf->export->order->id,'_payment_method',true); $woocommerce_totals = $wpo_wcpdf->get_woocommerce_totals(); $time = get_post_meta($wpo_wcpdf->export->order->id, '_billing_booking_delivery_t',true) ; $auto = get_post_meta($wpo_wcpdf->export->order->id, '_billing_auto',true) ; $final12 = date("d-m-Y", strtotime("+12 month -1 day", $time) ); $final3 = date("d-m-Y", strtotime("+3 month -1 day", $time) ); $product_id = $item['product_id']; if ( $product_id == 934 ) { $final = $final2; } elseif ( $product_id == 931 ) { $final = $final3; } else { // what if product_id is neither 931/932? } ?> <table class="head container"> <tr> <td class="header"> <?php if( $wpo_wcpdf->get_header_logo_id() ) { $wpo_wcpdf->header_logo(); } else { _e( 'Invoice', 'wpo_wcpdf' ); } ?> </td> <td class="shop-info"> <div class="shop-name"><h3><?php echo "ИНДИВИДУАЛЬНЫЙ ПОЛИС";?></h3></div> <div><?php echo "страхования от несчастных случаев "; ?></div> <div><?php echo "№ ";$wpo_wcpdf->invoice_number(); echo " от ";$wpo_wcpdf->invoice_date(); ?></div> </td> </tr> <span><?php echo "Настоящий Полис удостоверяет факт заключения договора страхования на условиях, изложенных на лицевой стороне настоящего документа и в Правилах страхования граждан от несчастных случаев, утвержденных приказом утвержденных Приказом ООО СК ?Наша гарантия? (далее Правила страхования).";?></span> <div><hr></div> <table class="order-details"><tbody> <tr> <td> <?php echo "1. Страховщик:" ; ?></td> <td> <?php echo "ООО Страховая компания ?Наша гарантия?, 115184, г. Москва, ул. Большая Ордынка, д. 59, стр. 2, тел./факс: (495) 642-00-21, ИНН 7743514834, лицензия от 12.09.2014г. СЛ № 3919"; ?> </td></tr> <tr> <td> <?php echo "2. Страхователь:"; ?></td> <td> <?php echo $billing_company; echo " ";echo $billing_first_name; echo " ";echo $billing_last_name;?> </td> <td> <?php echo "Дата рождения: "; echo $billing_address;?> </td></tr> </tbody></table> <div><?php echo "Паспорт: "; echo $billing_state;?></div> <div><?php echo "Адрес: "; echo $billing_address2;?></div> <div><?php echo "Стоимость полиса: "; echo $woocommerce_totals['order_total']['value'];?></div> <div><?php echo "Срок действия договора с: "; $wpo_wcpdf->custom_field('_billing_booking_delivery'); ?></div> <div><?php echo "Срок окончания договора: "; echo $final;?></div> <div><?php echo "Товар ID: ".$item['product_id'];?></div> <div><?php echo "Автошкола: "; echo $auto;?></div> </tr> </table><!-- head container --> <table class="notes container"> <tr> <td colspan="3"> <div class="notes-shipping"> <?php if ( $wpo_wcpdf->get_shipping_notes() ) : ?> <h3><?php _e( 'Customer Notes', 'wpo_wcpdf' ); ?></h3> <?php $wpo_wcpdf->shipping_notes(); ?> <?php endif; ?> </div> </td> </tr> </table><!-- notes container --> <?php if ( $wpo_wcpdf->get_footer() ): ?> <div id="footer"> <?php $wpo_wcpdf->footer(); ?> </div><!-- #letter-footer --> <?php endif; ?>
like I said, the items loop (foreach) is missing. There is no $items where you called it!
<?php $items = $wpo_wcpdf->get_order_items(); if( sizeof( $items ) > 0 ) { foreach( $items as $item ) { $product_id = $item['product_id']; if ( $product_id == 934 ) { $final = date("d-m-Y", strtotime("+12 month -1 day", $time) ); } elseif ( $product_id == 931 ) { $final = date("d-m-Y", strtotime("+3 month -1 day", $time) ); } else { // what if product_id is neither 931/932? } printf('<div>Срок окончания договора: %s</div>', $final); printf('<div>Товар ID: %s</div>', $product_id); } } ?>
This is basic PHP.
Gorgeous! It works! Thank you!
- The topic ‘Operator IF’ is closed to new replies.