Extra options code samples

Introduction

Filters can be used to alter the output during printing. You can add filters to your theme’s functions.php or a custom plugin. More information about filters for WordPress.

With the filters on this page you can modify the behavior of WooCommerce DYMO Print in several ways. Filters are for advanced users only. Do not ask us to write or adjust these codes for you.

If you do not know how filters work, do not use them!

Back-up first! If you do something wrong, adding code to your functions.php can break your site!

Code: Change number of labels per order

This filter can only be used for order and order item labels. This filter does not work for product labels.

By default 1 label is printed per order. However you can change how many labels to be printed per order.

Example use

  1. Print 2 labels for every order
  2. Print 4 labels for orders with specific shipping method
  3. Print 2 labels for shipping purposes, print 1 label for billing purposes
  4. Print 3 labels for order with more than 5 products
  5. Print a label for each product inside an order
  6. Print an extra label if the total product weight is above 10 kg

Basic example: print 2 labels for every order

To print 2 labels for every order place the following code in your theme’s functions.php or in a custom plugin.

Definition of variables

Do not alter the filter name “wpf_woocommerce_dymo_labels“, otherwise the filter becomes quite useless.

The filter uses the following variables:

  • $amount = the current amount, by default 1. The amount needs to be returned to the plugin. Needs to be an integer.
  • $order = the order object of the current order.
  • $label = the current label, for example ‘shipping’. This is the key of the label and is always unique.

Advanced example: print an extra label if the total product weight is 10 kg or more

The following example shows advanced use of the filter. We first check if the default Shipping label is printed. After that we get the total weight of all products inside the order. If the total product weight is 10kg or more, we print an extra label. If not, we print 1 label.

Code: Overwrite order item attribute output

As from WooCommerce DYMO Print version 3.1.1. it’s possible to overwrite the output of order-item attribute data. By default the order-item attribute data looks like: label: value.

Some people wanted to overwrite the output, so they can add another label or a suffix to the value.

To overwrite order item attribute data  you need to choose the option “Order item attributes (requires WC 3.1)” option from the configuration dropdown.

Select: Order Item attributes (requires WC 3.1)

You also need to use a filter to overwrite the output.

Back-up first! If you do something wrong, adding code to your functions.php can break your site!

Add the following filter to your theme functions.php or a custom plugin.

There are 3 variables in this filter:

  • $output = default output as used in the plugin. Example: ‘Weight (g): 2’
  • $value = value from attribute data. Example: ‘2’
  • $key = label from attribute data. Example: ‘Weight (g)’

To remove the label (key) and add a suffix to the output of attributes use the following example code:

If your products have multiple attributes the varible $key could be used to identify the $key and return a specific output for it.

Code: Adjust Order ID output

With WooCommerce DYMO Print version 3.1.4. we’ve added the possibility to adjust the output of the ORDER ID. You can use this for example to add the ORDER ID to a custom string or an URL to the order edit page (admin).

Back-up first! If you do something wrong, adding code to your functions.php can break your site!

Add the following filter to your theme functions.php or a custom plugin.

There is 1 variable in this filter:

  • $orderid = the default order ID of this order

To change the output to the order edit page in admin use the following code:

Code: Adjust Product ID output

With WooCommerce DYMO Print Product add-on version 3.1.1. we’ve added the possibility to adjust the output of the PRODUCT ID. You can use this for example to add the PRODUCT ID to a custom string or an URL to the product edit page (admin).

Back-up first! If you do something wrong, adding code to your functions.php can break your site!

Add the following filter to your theme functions.php or a custom plugin.

There is 1 variable in this filter:

  • $product = the default product ID of this product

To change the output to the product edit page in admin use the following code:

Code: Output all order data and order item data with a custom PHP function

With WooCommerce DYMO Print version 3.1.8. we’ve added the possibility to output all order data or order item data with a custom PHP function. For this you need to choose the new setting “Custom PHP function”, see screenshot:

Go to Step 2 Change label settings and select option "Custom PHP function"

Back-up first! If you do something wrong, adding code to your functions.php can break your site!

Add the following filter to your theme functions.php or a custom plugin.

Code: Output all product data with a custom PHP function

With WooCommerce DYMO Print Product add-on version 3.1.8. we’ve added the possibility to output all product data with a custom PHP function. For this you need to choose the new setting “Custom PHP function”, see screenshot:

Go to settings Step 2: change label settings, select option "Custom PHP function"

Back-up first! If you do something wrong, adding code to your functions.php can break your site!

Add the following filter to your theme functions.php or a custom plugin.

Modify all label data for WooCommerce order or order-items

With the following filter you can alter all label data for WooCommerce order or order-items. You need to return all data as an array. Each element inside the array will print a new label.

So if you want to print 4 labels, the filter must return an array containing 4 elements.

Back-up first! If you do something wrong, adding code to your functions.php can break your site!

Why use this filter?

Use this filter if you want to modify the label output before printing. For example:

  • Print a label for each order item metadata
  • Print one label with a the address and a label with a tracking code from the same order in one click
  • Print a label if an order contains a specific product
  • Print an extra label if an order has a specific payment or shipping method
  • Print 3 labels if a customer is living in a specific country
  • Print multiple labels if the total weight of the order is above 1 kg
  • Print multiple labels if the order contains a product which is larger than 100 x 100 x 100 cm

All labels are printed in the same design. If you want to print multiple labels on different label sizes and in with different designs use label combinations.

Parameters (5)

  1. $output = original data (array)
  2. $object = name of DYMO label object (string)
  3. $order = WooCommerce order (object)
  4. $labelID = Name of the current label (string)
  5. $item = Order item for order items labels ($object)

Usage

The following example is for adding a hook callback.

Example

The following example gets the order, checks which items are in cart and prints product information for each item in the order on a separate label.

Download example DYMO .label-file.

Modify all label data for WooCommerce products

With the following filter you can alter all label data for WooCommerce products. You need to return all data as an array. Each element inside the array will print a new label.

So if you want to print 4 labels, the filter must return an array containing 4 elements.

Back-up first! If you do something wrong, adding code to your functions.php can break your site!

Why use this filter?

Use this filter if you want to modify the label output before printing. For example:

  • Print one label with product name and a separate label with product URL
  • Print an extra label if stock level is below 3
  • Print an extra label for all products with a weight above 1 kg
  • Print one label for stockmanagement and one label for on the product itself in a single click
  • Print multiple labels if product dimensions are above 100 x 100 x 100 cm

All labels are printed in the same design. If you want to print multiple labels on different label sizes and in with different designs use label combinations.

Parameters (5)

  1. $output = original data (array)
  2. $object = name of DYMO label object (string)
  3. $product = WooCommerce order (object)
  4. $variation= WooCommerce order (object – only for variable products)
  5. $labelID = Name of the current label (string)

Usage

The following example is for adding a hook callback.

Example

The following example gets the product, checks if the weight of the product is above 1kg and prints an extra label which contains weight.

Download example DYMO .label-file.

Sample labels

If you’re looking for sample labels, you’re on the wrong page. Click here to download our sample labels.

Questions about filters?

The filters on this page are for advanced users only. We can not write custom code for you for free. However if you’ve a specific wish for code or functions we can do custom development for you. Contact us for a quote.