• Resolved amch

    (@amch)


    Hi Ewout,

    I want to make some changes to our invoices, specifically hide the product weight, and instead print tariff number & country of origin. (_dhl_hs_code, _dhl_manufacture_country).

    I found plenty of info that you had previously shared, but none of the scripts worked for me.

    I put this in the php – it had no effect.

    /* dont show weight on invoice*/
    function wpo_wcpdf_custom_styles ( $document_type, $document ) {
        ?>
        .meta .weight { display:none !important; }    <?php
    }

    Another attempt- with no effect

    add_action(‘wpo_wcpdf_after_item_meta’, ‘wpo_wcpdf_show_custom_attribute’, 10, 3);
    function wpo_wcpdf_show_custom_attribute($document_type, $item, $order)
    {
    if( !empty($order) && $document_type == ‘invoice’ ) {
    $product = wc_get_product($item[‘product_id’]);
    $attributes = $product->get_attributes();
    foreach( $attributes as $type => $attribute ) {
    if( $type == ‘_dhl_hs_code’ ) { // attribute type
    echo ‘<dl class=”meta”>’;
    echo ‘<dt class=”_dhl_hs_code”>’.$attribute[‘name’].’:</dt><dd”>’.$attribute[‘options’][0].'</dd>’;
    echo ‘</dl>’;
    }
    }
    }
    }

    And this script was rejected, the ‘return’ in line 4 caused an error:

    add_action( ‘wpo_wcpdf_after_item_meta’, ‘wpo_wcpdf_show_product_attributes’, 10, 3 );
    function wpo_wcpdf_show_product_attributes ( $template_type, $item, $order ) {
        if(empty($item[‚product’])) return;
        $document = wcpdf_get_document( $template_type, $order );
        printf(‘<div class=„product-attribute“>_dhl_hs_code: %s</div>’, $document->get_product_attribute(‚_dhl_hs_code‚, $item[‘product’]));
    }

    Can you tell me what I am doing wrong? As you can see I have no experience in coding and don’t know how to modify the commands.

    Thanks!
    Anja MC

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor alexmigf

    (@alexmigf)

    Hello @amch

    Please try adding the code snippet below to your theme functions.php file:

    add_action( 'wpo_wcpdf_custom_styles', function( $document_type, $document ) {
    	?>
    	.product .item-meta,
    	.product .weight {
    		display: none;
    	}
    	<?php
    }, 10, 2 );

    If you never worked with actions/filters please read this documentation page: How to use filters

    Thread Starter amch

    (@amch)

    Excellent, this worked. The weight is finally off the invoice.

    Could you also provide the solution to display other attributes instead?
    (_dhl_hs_code, _dhl_manufacture_country)

    I used the snippets I found in your documentation, but they
    a) had no effect
    (https://wordpress.org/support/topic/displaying-additional-information-custom-product-attribute-to-invoice/) or
    b) produced an error message.
    (https://docs.wpovernight.com/woocommerce-pdf-invoices-packing-slips/displaying-product-attributes)

    Thank you! Appreciate the help.
    Anja MC

    Plugin Contributor Darren Peyou

    (@dpeyou)

    Hi @amch, thanks for hanging on!

    I’m sorry you’re getting an error. What does it look like? Could you post the error message?

    I believe this thread answers your question perfectly:
    https://wordpress.org/support/topic/cant-we-show-custom-field-from-product-to-invoice-pdf/#post-12966531

    So to summarize, in my mind the easiest thing for you to circumvent your current errors would be to use the Premium Extension of our plugin. This will offer you the most flexibility & would solve your issue immediately.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hello @amch,

    Please try this code snippet in order to display your DHL Code:

    add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_show_product_attributes', 10, 3 );
    function wpo_wcpdf_show_product_attributes ( $template_type, $item, $order ) {
    	if(empty($item['product'])) return;
    	$document = wcpdf_get_document( $template_type, $order );
    	$dhl_code = $document->get_product_attribute('_dhl_hs_code', $item['product']);
    	if (!empty($dhl_code)) {
    		printf('<div class="product-attribute">DHL code: %s</div>', $dhl_code);
    	}
    } 

    If work, please follow the same method to display any other field.

    Hope it helps!

    Thread Starter amch

    (@amch)

    HI Yordan,

    thanks for coming back to me and providing the snippet. For some reason your snippet does not display anything, but I found another snippet that does print my code.

    Is it possible to change my snippet (see below) so that the field is printed next to the article number, both in the same line? What do I have to change?
    (I would like to show you a screenshot of an invoice but don’t know how to enter it here)

    Thanks, Anja

    add_action( ‘wpo_wcpdf_after_item_meta’, ‘wpo_wcpdf_product_custom_field’, 10, 3 );
    function wpo_wcpdf_product_custom_field ( $template_type, $item, $order ) {
    if (empty($item[‘product’])) return;

    $field_name = ‘_dhl_hs_code’;
    $_dhl_hs_code = $item[‘product’]->get_meta($field_name,true,’edit’);
    if (!empty($_dhl_hs_code)) {
    echo ‘<div class=”_dhl_hs_code”>ZTN: ‘.$_dhl_hs_code.'</div>’;
    }

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hello @amch,

    I’m afraid this is not a product meta, but an order meta.

    If you don’t have code skills, using the Premium Templates customizer will be helps you to achieve what you want to do. With the customizer you’ll be able to add a Product column and the placeholder {{product_custom_field::custom_field_name}}, this way:

    Premium Template custom Field placeholder

    If you want to keep trying with code, please read the documentation.

    In any case, you need to differentiate whether the field you are trying to display is a metadata from the order or the product.

    Hope it helps!

    Thread Starter amch

    (@amch)

    Thanks for the support and explanation.
    I found a work-around.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘hide/display product attributes with action hooks – not working’ is closed to new replies.