• Resolved plzshareme

    (@plzshareme)


    I’d like to add a custom class based on my custom State form field.

    when the PDF loads, how can i set the if statement to show a certain table with a related class at the start of page 1?
    if <?php $wpo_wcpdf->custom_field(‘_billing_state’, ”, true); ?> is NY then show <table class=”pageny”>
    if <?php $wpo_wcpdf->custom_field(‘_billing_state’, ”, true); ?> is CT then show <table class=”pagect”>
    if <?php $wpo_wcpdf->custom_field(‘_billing_state’, ”, true); ?> is NJ then show <table class=”pagenj”>

    https://www.ads-software.com/plugins/woocommerce-pdf-invoices-packing-slips/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hi! The custom_field() method only displays the custom field, so you can’t use it for logic like that.
    There are many different approaches to this, without seeing your template code (with those tables), it’s hard to give you one answer, but I think this comes close to what you want:

    <style type="text/css">
    	table.page {
    		display: none;
    	}
    
    	table.page.<?php strtolower($this->order->billing_state); ?> {
    		display: block;
    	}
    </style>

    What this does is that it hides all the tables with the page class and then overrides this for the one that matches the billing state. This requires a small change in your html, so that the tables classes are like this:

    <table class="page ny">...</table>
    <table class="page ct">...</table>

    Note that this may not work if no state is selected. If you really want logic like your example, you could use something like this:

    <?php if ($this->order->billing_state == 'ny'): ?>
    	<table class="page ny">...</table>
    <?php endif ?>

    hope that helps!
    Ewout

    Thread Starter plzshareme

    (@plzshareme)

    Thank you Ewout! the last scenario did the trick

    i just added an elseif after the first if statement for each state i needed to define and it works great!

    Thread Starter plzshareme

    (@plzshareme)

    ah one more thing. i need to also check that the sku is a certain thing. how should that if statement look?

    <?php if ($this->order->billing_state == ‘NY’ && $item[sku] == ‘ny01’): ?>
    echo ‘<table class=”pageny”>’;
    <?php endif ?>

    Plugin Contributor Ewout

    (@pomegranate)

    I see why the first version didn’t work – I forgot an echo! For completeness sake I’ll put the correct code here:

    <style type="text/css">
    	table.page {
    		display: none;
    	}
    
    	table.page.<?php echo strtolower($this->order->billing_state); ?> {
    		display: block;
    	}
    </style>

    Since an order can have multiple items, it’s not possible to get the SKU out side of the order items table (or, more precisely, the items foreach loop).
    Inside the order items table you can call the SKU with $item[‘sku’]. This may not be set if the product doesn’t have an SKU, so you need to check that too.

    <?php if (isset($item['sku']) && $item['sku'] == 'ny01'); ?>

    Ewout

    Thread Starter plzshareme

    (@plzshareme)

    awesome, so here’s how i got it to work.

    I had to add above my “php if” statements this from the standard invoice template:

    <?php $items = $wpo_wcpdf->get_order_items(); if( sizeof( $items ) > 0 ) : foreach( $items as $item_id => $item ) : ?>
    	<?php do_action( 'wpo_wcpdf_before_item_meta', $wpo_wcpdf->export->template_type, $item, $wpo_wcpdf->export->order  ); ?>

    then immediately after i look up if the state is ‘one thing’ and the sku is ‘another thing’ then run this div class:

    <?php
    if ($this->order->billing_state == 'ny' && $item['sku'] == 'ny01'):
    	echo '<table class="page1">';
    
    elseif ():
    endif;
    ?><?php endforeach; endif; ?>

    Thank you Ewout for helping me with the logic & flow

    Plugin Contributor Ewout

    (@pomegranate)

    Fantastic, glad to hear it all works now ??

    Happy selling!
    Ewout

    Thread Starter plzshareme

    (@plzshareme)

    Ewout, one more question on this:

    I now need to switch my if statement to look for
    $wpo_wcpdf->custom_field[‘myfield7’] instead of $this->order->billing_state

    my statement looks like this but it’s not resolving. can you see what’s wrong here?

    if ($this->order->custom_field['myfield7'] == 'ny' && $item['sku'] == 'ny01'):
    	echo '<table class="page1">';
    Plugin Contributor Ewout

    (@pomegranate)

    I’ll answer this in the other thread you just opened about this issue ??
    https://www.ads-software.com/support/topic/php-if-custom-field-lookup-on-pageload?replies=1

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘set background image based on custom field’ is closed to new replies.