• Resolved spshni

    (@spshni)


    Hi,

    I’m trying to display different value on my invoice depending on the order shipping country.

    I have tried to add : $country = $this->get_shipping_country(); inside my template but its work only when this modified piece of code is added :

    public function get_shipping_country() {
    		// always prefer parent shipping country for refunds
    		if ( $this->is_refund( $this->order ) ) {
    			// temporarily switch order to make all filters / order calls work correctly
    			$refund = $this->order;
    			$this->order = $this->get_refund_parent( $this->order );
    			$country = apply_filters( 'wpo_wcpdf_shipping_country', $this->order->get_shipping_country(), $this );
    			// switch back & unset
    			$this->order = $refund;
    			unset($refund);
    		} elseif ( $country = $this->order->get_shipping_country() ) {
    			// regular shop_order
    			$country = apply_filters( 'wpo_wcpdf_shipping_country', $country, $this );
    		} else {
    			// no address
    			// use fallback for packing slip
    			if ( apply_filters( 'wpo_wcpdf_shipping_country_fallback', ( $this->get_type() == 'packing-slip' ), $this ) ) {
    				$country = $this->get_billing_country();
    			} else{
    				$country = apply_filters( 'wpo_wcpdf_shipping_country', __('N/A', 'woocommerce-pdf-invoices-packing-slips' ), $this );
    
    			}
    		}
    
    		return $country;
    	}
    public function shipping_country() {
    		echo $this->get_shipping_country();
    	}

    My issue is that I need to put this code inside woocommerce-pdf-invoices-packing-slips/includes/documents/abstract-wcpdf-order-document-methods.php file otherwise it do not work. So of course it’s overridden every update.

    So how can I achieve to make works $country = $this->get_shipping_country(); in my invoice template by adding a piece of code inside template-functions.php ?

    Thanks for your help,

    Best Regards.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @spshni,

    Sorry for the late reply. Could you please tell me in details what are you trying to do, so I can offer you advice to achieve it?

    Thread Starter spshni

    (@spshni)

    Hi @yordansoares,

    I want to display different text on invoice depending on the order shipping country.

    Here is an example if buyer is from USA or UK :

    <?php
    
    $country = $this->get_shipping_country();
    
    if ( $country === "US" || country === "UK" ) { ?>
    
    <p>You will need to pay additionnal customs fees when parcel arrive in your country.</p>
    
    <?php } ?>

    But as I said, for $country = $this->get_shipping_country() to work I need to add the entire code in my first post inside woocommerce-pdf-invoices-packing-slips/includes/documents/abstract-wcpdf-order-document-methods.php file. But it is of course overriden every update.

    So my question is : What code can I add to template-functions.php so that $country = $this->get_shipping_country() works without modifying plugin file ?

    Best Regards.

    • This reply was modified 2 years, 8 months ago by spshni.
    Plugin Contributor Yordan Soares

    (@yordansoares)

    Where do you want to display these custom texts (e.g. after the customer notes, after the order table, etc.)?

    Thread Starter spshni

    (@spshni)

    I have added these texts in div just before wpo_wcpdf_after_order_details line inside invoice.php.

    • This reply was modified 2 years, 8 months ago by spshni.
    Plugin Contributor Yordan Soares

    (@yordansoares)

    Try with this code snippet:

    /**
     * PDF Invoices & Packing Slips for WooCommerce:
     * Add custom texts, based on the billing country, after the order details
     */
    add_action( 'wpo_wcpdf_after_order_details', function( $document_type, $order ) {
      if ( $document_type == 'invoice' ) {
        if ( $billing_country = $order->get_billing_country() ) {
          switch ( $billing_country ) {
            /* If the country is United States or United Kingdom */
            case 'US':
            case 'GB':
              echo '<p>You will need to pay additional  customs fees when parcel arrive in your country.</p>';
              break;                
            default:
            # code...
            break;
          }
        }
      }	
    }, 10, 2 );

    If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters

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

The topic ‘Get shipping country in invoice template’ is closed to new replies.