Skip to content

Instantly share code, notes, and snippets.

@mahype
Last active December 14, 2016 13:47
Show Gist options
  • Save mahype/280aac194ae0e73901c074eac376bc14 to your computer and use it in GitHub Desktop.
Save mahype/280aac194ae0e73901c074eac376bc14 to your computer and use it in GitHub Desktop.
How to add shipment classes fields to WooCommerce
<?php
/**
* Extend WooCommerce Shipping Classes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WC_Extend_Shippig_Classes {
/**
* Construct
*/
public function __construct() {
// WooCommerce since 2.6
add_filter( 'woocommerce_shipping_classes_columns', array( $this, 'add_shipping_class_columns' ) );
add_action( 'woocommerce_shipping_classes_column_extension-parcel-myvalue', array( $this, 'add_shipping_class_row_myvalue' ) );
add_action( 'woocommerce_shipping_classes_save_class', array( $this, 'save_class' ), 10, 2 );
add_filter( 'woocommerce_get_shipping_classes', array( $this, 'extend_terms' ), 10, 1 );
// WooCommerce until 2.5
add_action( 'product_shipping_class_edit_form_fields', array( $this, 'shipping_class_edit_form_fields' ), 10, 2 );
add_action( 'edited_product_shipping_class', array( $this, 'shipping_class_edit_form_fields_save' ), 10, 1 );
add_action( 'product_shipping_class_add_form_fields', array( $this, 'shipping_class_add_form_fields' ), 10, 1 );
add_action( 'create_product_shipping_class', array( $this, 'shipping_class_edit_form_fields_save' ), 10, 1 );
}
/**
* Adding extension columns to shipping classes (since WooCommerce 2.6)
*
* @param $columns
*
* @return array
*/
public function add_shipping_class_columns( $columns ) {
$extension_columns = array(
'extension-parcel-myvalue' => __( 'My Value', 'woocommerce-extension' ),
);
$columns = array_merge( $columns, $extension_columns );
return $columns;
}
/**
* Adding field for myvalue (since WooCommerce 2.6)
*/
public function add_shipping_class_row_myvalue() {
?>
<div class="view">{{ data.myvalue }}</div>
<div class="edit"><input type="text" name="myvalue[{{ data.term_id }}]" data-attribute="myvalue" alue="{{ data.myvalue }}" placeholder="<?php esc_attr_e( '0', 'woocommerce' ); ?>"/>
</div>
<?php
}
/**
* Saving class data (since WooCommerce 2.6)
*
* @param $term_id
* @param $data
*/
public function save_class( $term_id, $data ) {
if ( isset( $data[ 'myvalue' ] ) ) {
$parcel_myvalue = wc_clean( $data[ 'myvalue' ] );
}
update_option( 'shipping_class_' . $term_id . '_extension_myvalue', $parcel_myvalue );
}
/**
* Extending terms with data from extension fields (since WooCommerce 2.6)
*
* @param WP_Term[] $shipping_classes
*
* @return WP_Term[] $shipping_classes
*/
public function extend_terms( $shipping_classes ) {
foreach ( $shipping_classes AS $key => $shipping_class ) {
$term_id = $shipping_class->term_id;
$shipping_classes[ $key ]->myvalue = get_option( 'shipping_class_' . $term_id . '_extension_myvalue' );
}
return $shipping_classes;
}
/**
* Selecting Parcel for shipping class on editing Shipment Class (until WooCommerce 2.5)
*
* @param $tag
* @param $taxonomy
*/
public function shipping_class_edit_form_fields( $tag, $taxonomy ) {
$term_id = $_GET[ 'tag_ID' ]; // $tag doesn't work really, so use $_GET[ 'tag_ID' ]
$myvalue = get_option( 'shipping_class_' . $term_id . '_extension_myvalue' );
$html = '<tr class="form-field extension-parcel-form-field">';
$html .= '<th scope="row" colspan="2">';
$html .= '<h3>' . __( 'My Settings', 'woocommerce-extension' ) . '</h3>';
$html .= '</th>';
$html .= '</tr>';
$html .= '<tr class="form-field extension-parcel-form-field">';
$html .= '<th scope="row">';
$html .= '<label for="extension_parcel_myvalue">' . __( 'My Value', 'woocommerce-extension' ) . '</label>';
$html .= '</th>';
$html .= '<td>';
$html .= '<input type="text" name="extension_parcel_myvalue" value="' . $myvalue . '" /> ';
$html .= '</td>';
$html .= '</tr>';
echo $html;
}
/**
* Selecting Parcel for shipping class on adding Shipment Class (until WooCommerce 2.5)
*/
public function shipping_class_add_form_fields() {
$html = '<h4>' . __( 'Shipment Settings', 'woocommerce-extension' ) . '</h4>';
$html .= '<div class="form-field shipment-settings">';
$html .= '<label for="extension_parcel_myvalue">' . __( 'My Value', 'woocommerce-extension' ) . '</label>';
$html .= '<input type="text" name="extension_parcel_myvalue" />';
$html .= '</div>';
echo $html;
}
/**
* Saving Shipping Class data on editing Shipment Class (until WooCommerce 2.5)
*
* @param int $term_id Term ID
*/
public function shipping_class_edit_form_fields_save( $term_id ) {
$parcel_myvalue = $_POST[ 'extension_parcel_myvalue' ];
update_option( 'shipping_class_' . $term_id . '_extension_myvalue', $parcel_myvalue );
}
}
$extend_classes = new WC_Extend_Shippig_Classes();
@nitesh2sagar
Copy link

hey,
I am unable to understand what "{{ data.myvalue }}" this is doing
in the line

{{ data.myvalue }}

is it javascript ( or html)

I will be thankful to you if you can explain it to me

My Email-id: [email protected]

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