Skip to content

Instantly share code, notes, and snippets.

@WPprodigy
Last active August 27, 2015 10:54
Show Gist options
  • Save WPprodigy/34a45dc2686e1f9b4ba8 to your computer and use it in GitHub Desktop.
Save WPprodigy/34a45dc2686e1f9b4ba8 to your computer and use it in GitHub Desktop.
An example plugin showing how WooCommerce Product Fees can be extended
<?php
/**
* Plugin Name: WooCommerce Product Fees Prefix Addition
* Description: Add a prefix to the additional fees that is shown at checkout in front of the fee name.
* Version: 1.0
* Author: Caleb Burks
* Author URI: http://calebburks.com
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'woocommerce_product_fees_add_settings_group_one', 'wc_fees_add_settings' );
function wc_fees_add_settings() {
// Text Field - Fee Prefix
woocommerce_wp_text_input( array( 'id' => 'product-fee-prefix', 'label' => __( 'Fee Prefix', 'woocommerce-product-fees' ), 'data_type' => 'text', 'placeholder' => __('Product Prefix', 'placeholder', 'woocommerce-product-fees'), 'desc_tip' => 'true', 'description' => __( 'This will be shown in front of the fee name.', 'woocommerce-product-fees' ) ) );
}
add_action( 'woocommerce_process_product_meta', 'wc_fees_save_settings' );
function wc_fees_save_settings( $post_id ) {
// Text Field - Fee Prefix
$product_fee_prefix_text_field = $_POST['product-fee-prefix'];
if( ! empty( $product_fee_prefix_text_field ) ) {
update_post_meta( $post_id, 'product-fee-prefix', esc_attr( $product_fee_prefix_text_field ) );
}
}
add_filter( 'woocommerce_product_fees_filter_fee_data', 'wc_fees_add_prefix_to_name' );
function wc_fees_add_prefix_to_name( $fee ) {
$fee_prefix = get_post_meta( $fee['product_id'], 'product-fee-prefix', true );
$fee['name'] = $fee_prefix . $fee['name'];
return $fee;
}
@WPprodigy
Copy link
Author

wc_fees_add_settings() creates a new text input here: http://cld.wthms.co/1l8mh/1N7bzYUO

wc_fees_save_settings() saves the settings using the WooCommerce hook. Be sure to include $post_id as an attribute in the function.

And then wc_fees_add_prefix_to_name() filters the prefix to be in front of the name: http://cld.wthms.co/1gtpt/3Aj480k8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment