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.

add_filter('woocommerce_dymo_amount_per_order',function($amount,$order,$label) {
	return 2;
}
,10,3);

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.

add_filter('woocommerce_dymo_amount_per_order',function($amount,$order,$label) {

	if($label=='shipping') { // Only change amount if label is 'shipping'

		$weight=0; // Set default weight to 0
		
		if ( sizeof( $order->get_items() ) > 0 ) { // Check if order contains items
			foreach( $order->get_items() as $item ) { // loop through all items
				if ( $item['product_id'] > 0 ) { // Check if item is product
					$_product = $order->get_product_from_item( $item ); // Create product from item
					if ( ! $_product->is_virtual() ) { // if product is not virtual
						$weight += $_product->get_weight() * $item['qty']; // Get weight of product * item quantity
					}
				}
			}
		}

		if ( $weight >= 10 ) { // If weigth is greater then 10
			$amount=2; // Set amount to print 2 labels
		} else {
			$amount=1; // Set amount to print 1 label
		}
		
	}
	
	return $amount; // Return the amount to the plugin. Must be integer.
}
,10,3);

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.

// Overwrite attribute data output
add_filter('wpf_woocommerce_dymo_item_attr_fix',function($output,$value,$key) {
    return $output; // Default output
},10,3);

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:

add_filter('wpf_woocommerce_dymo_item_attr_fix',function($output,$value,$key) {
    return $value.' grams'; // Outputs value + suffix, example '2 grams'
},10,3);

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.

/*
 * Adjust order ID output
 */
add_filter('wpf_woocommerce_dymo_orderid','wpf_overwrite_orderid',99,1);
function wpf_overwrite_orderid($order_id) {
     return $order_id;
}

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:

/* 
 * Adjust order ID output to return order edit page URL
 */ 
add_filter('wpf_woocommerce_dymo_orderid','wpf_overwrite_orderid',99,1);
function wpf_overwrite_orderid($order_id) {
     return get_admin_url().'post.php?post='.$order_id.'&action=edit';
}

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.

/*
 * Adjust product ID output
 */
add_filter('wpf_woocommerce_dymo_productid','wpf_overwrite_productid',99,1);
function wpf_overwrite_productid($product_id) {
     return $product_id;
}

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:

/* 
 * Adjust product ID output to return product edit page URL
 */ 
add_filter('wpf_woocommerce_dymo_productid','wpf_overwrite_productid',99,1);
function wpf_overwrite_productid($product_id) {
     return get_admin_url().'post.php?post='.$product_id.'&action=edit';
}

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.

/* 
 * Output custom order or order item data
 * Needs 3 parameters: 
 * - $order_id: The ID of the current order
 * - $label: The name of the label, for example: "billing" or "shipping"
 * - $object: The name of je label-object. for example: "ADDRESS_1"
*/
add_filter('wc_dymo_order_function', 'wpf_dymo_order_output',10,3);
function wpf_dymo_order_output($order_id,$label,$object) {

	/*
	Only return data if label name is "shipping" for DYMO Label object-name is "ADDRESS_1"
	Change label name and object-name to your own needs
	*/
	if($label=='shipping' && $object=='ADDRESS_1') {
		
		//Get order from order ID
		$order=wc_get_order($order_id);
		$products='';
		
		//Check if order contains items
		if ( sizeof( $order->get_items() ) > 0 ) {
			
			//Loop through all order items
			foreach ($order->get_items() as $item) {
				
				//Check if the item is a product
				if ( $item['product_id'] > 0 ) {
					
					//Get product price and format it for printing with function DYMO_format_price
					$product_price=DYMO_format_price($item['line_total'],$order->get_currency());
					
					//Get product data from order item
					$product=wc_get_product($item['product_id']);
				
					//Get product name from product
					$product_title=$product->get_title();	
					
					//Get product SKU					
					$product_sku=stripslashes($product->get_sku());
					
					//Get order item quantity
					$product_qty=stripslashes($item['qty']);
					
					//Collect all product data and create a new line for this product, use | to create a line-break, adjust for your own needs
					$products.=$product_title.' ('.$product_sku.') |'.$product_qty.' x '.DYMO_format_price(($item['line_total']/$product_qty),$order->get_currency()).' = '.$product_price.'|';
					
					/*
					//Other example:
					if($product_qty==1) {
						$products.='You have purchased '.$product_qty.' item of '.$product_title.' for just '.$product_price.'|';
					} else {
						$products.='You have purchased '.$product_qty.' items of '.$product_title.' for just '.$product_price.'|';
					}
					*/
					
				}
			}
		}
		
		//Return a list (string) of all product inside this order, make sure it's well formatted before printing.
		return $products;
	} else {
		//Return order_id if is not correct label and object
		return '';
	}
}

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.

/* 
 * Output custom product data
 * Needs 3 parameters: 
 * - $product_id: The ID of the current product
 * - $label: The name of the label, for example: "product"
 * - $object: The name of je label-object. for example: "TEXT_1"
*/
add_filter('wc_dymo_product_function', 'wpf_dymo_product_output',10,3);
function wpf_dymo_product_output($product_id,$label,$object) {
	/*
	Only return data if label name is "product" for DYMO Label object-name is "TEXT_1"
	Change label name and object-name to your own needs
	*/
	if($label=='product' && $object=='TEXT_1') {
		
		//Get product from product ID
		$product=wc_get_product($product_id);
				
		//Get product name from product
		$product_title=$product->get_title();	
				
		//Get product SKU and format it					
		$product_sku=stripslashes($product->get_sku());
		if($product_sku!="") { $sku='(SKU: '.$product_sku.') ';}
				
		//Collect all product data and return it in your own format
		return sprintf(__('Get more info about %s %s at | %s','YOUR_THEME'),$product_title,$sku,get_permalink($product_id));

	} else {
		
		//Return product_id if is not correct label and object
		return '';
		
	}
}

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.

// Modify label data output, must return an array()
function filter_wc_dymo_order($output,$object,$order,$labelID,$item) {

    //$output = original data (array)
    //$object = name of DYMO label object (string)
    //$order = WooCommerce order (object)
    //$labelID = Name of the current label (string)
    //$item = Order item for order items labels ($object)

    // make filter magic happen here...

    return $output;

}

// add the filter
add_filter('wc_dymo_order','filter_wc_dymo_order',20,5);

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 label data output, must return an array()
function filter_wc_dymo_order($output,$object,$order,$labelID,$item) {

    //$output = original data (array)
    //$object = name of DYMO label object (string)
    //$order = WooCommerce order (object)
    //$labelID = Name of the current label (string)
    //$item = Order item for order items labels ($object)
    
	// Change output only if labelID=='shipping'
	if($labelID=='shipping') {
		
		// Delete original output
		$output=array();
		
		// Loop through all items from the order and create new output
		// Print every item on a separate label
		foreach($order->get_items() as $item_id => $item) {
			
			// Get product from product ID or variation ID
			$product_variation_id = $item->get_variation_id();
			
			if($product_variation_id >0) {
				$product = wc_get_product($product_variation_id);
			} else {
				$product = wc_get_product($item->get_product_id());
			}
			
			// Get name, SKU, URL and short description for this product
			$item_name = $item->get_name();
			$item_sku = 'SKU: '.$product->get_sku();
			$url= get_permalink($item->get_product_id());
			$excerpt = wordwrap($product->get_description(),70,"\n"); // wrap short description every 70 characters
						
			// Format output array			
			$output[]=array(
				'NAME'=> $item_name,
				'SKU' => $item_sku,
				'URL' => $url,
				'EXTRA' => $excerpt,
			);
		}
	}
	
	return $output;
}

// add the filter
add_filter('wc_dymo_order','filter_wc_dymo_order',20,5);

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.

// Modify label data output, must return an array()
function filter_wc_dymo_product($output,$object,$product,$variation,$labelID) {

    //$output = original data (array)
    //$object = name of DYMO label object (string)
    //$product = WooCommerce product (object)
    //$variation = WooCommerce variation (object - optional)
    //$labelID = name of hte current label (string)

    // make filter magic happen here...

    return $output;

}

// add the filter
add_filter('wc_dymo_product','filter_wc_dymo_product',20,5);

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.

// Modify label data output, must return an array()
function filter_wc_dymo_product($output,$object,$product,$variation,$labelID) {

    //$output = original data (array)
    //$object = name of DYMO label object (string)
    //$product = WooCommerce product (object)
    //$variation = WooCommerce variation (object - optional)
    //$labelID = name of hte current label (string)

    // Change output only if labelID=='product'
    if($labelID=='product') {
		
		// Check if product is variation or not and get weight from product OR variation
		if(!empty($variation)) {
			$weight=$variation->get_weight();
		} else {
			$weight=$product->get_weight();
		}		
		
		// if weight is above 1 kg
		if($weight > 1) {
			
			// Add a "heavy weight" warning on the original label
			$output[0]['WEIGHT'] = 'Heavy weight: '.$weight.' kg';
			
			// Print 1 extra label with the same data, but a different warning
			$output[1]=$output[0];
			$output[1]['WEIGHT'] = '!!! Watch your back: heavy weight product !!!';
			
		}
		
	}

    return $output;

}

// add the filter
add_filter('wc_dymo_product','filter_wc_dymo_product',20,5);

Overwrite product label variation separator

By default all variations on product labels are printed in a single line. If you have many variations (or long variation names) the text will be printed very small. To start each variation on a new line you can use the following filter:


add_filter('wc_dymo_product_variation_separator','prefix_overwrite_variation_separator');
function prefix_overwrite_variation_separator($default) {
	return '|';
}

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 unfortunately can not write custom code for you.