• Resolved rocketscream

    (@rocketscream)


    Hi,

    Currently using v1.6.5 with WC3.0.6.
    I have added the following code that used to work to pull custom attribute and show them in the invoice for both simple & variable product. Now it only works with simple product.
    <dt class="sku"><?php _e( 'HS Tariff:', 'wpo_wcpdf' ); ?></dt><dd class="sku"><?php $wpo_wcpdf->product_attribute('HS Tariff', $item['product']); ?></dd>

    When used for variable product, it will show nothing (blank). Is this something related to the changes in WC3.0.6 in accessing attributes variable product or the plugin itself?

    Many thanks for the awesome work.

    • This topic was modified 7 years, 10 months ago by rocketscream.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter rocketscream

    (@rocketscream)

    Okay figure it out from this topic.

    But as any order will consist of both simple and variable product, I would need to check whether an order item is a simple or variable product. Then I notice that variable product has a non-zero “variation_id” and simple product has a zero “variation_id”. So, here’s a quick and dirty implementation of 2 custom attribute (Description and HS Tariff):

    <dt class="description"><?php _e( 'Description:', 'wpo_wcpdf' ); ?></dt>
    	<dd class="description">
    	<?php 
    	if($item['variation_id'] != '0'){
    		$wpo_wcpdf->product_attribute('Description', $item['product']->parent); 
    	}
    	else{
    		$wpo_wcpdf->product_attribute('Description', $item['product']); 
        }
    	?>
    	</dd>
    <dt class="hs_tariff"><?php _e( 'HS Tariff:', 'wpo_wcpdf' ); ?></dt>
        <dd class="hs_tariff">
        <?php 
    	if($item['variation_id'] != '0'){
    		$wpo_wcpdf->product_attribute('HS Tariff', $item['product']->parent); 
    	}
    	else{
    		$wpo_wcpdf->product_attribute('HS Tariff', $item['product']); 
    	}
    	?>
    	</dd>
    • This reply was modified 7 years, 10 months ago by rocketscream.
    Plugin Contributor Ewout

    (@pomegranate)

    Hi! Glad to hear you found a solution.
    Yes, WC3.0 is a bit of a pain in this aspect. Here’s a more compact version of your code, hope that helps!

    
    <?php $wpo_wcpdf->product_attribute('Description', !empty($item['product']) && $item['product']->is_type( 'variation' ) ? $item['product']->parent : $item['product'] ); ?>
    

    for completeness sake I recommend adding a check to see if the product still exists though!

    
    <?php
    if (!empty($item['product'])) {
    	$wpo_wcpdf->product_attribute('Description', $item['product']->is_type( 'variation' ) ? $item['product']->parent : $item['product'] );					
    }
    ?>
    

    Hope that helps!
    Ewout

    Thread Starter rocketscream

    (@rocketscream)

    Hi Ewout,

    Thank you so much for giving a cleaner line of code even though I have fixed it.
    Will try it out.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Product Attribute No Longer Showing for Variable Product with WC3.0’ is closed to new replies.