Add product attribute columns to Woocommerce backend grid
If you have to extend the backend product grid of Woocommerce by an custom attribute , you have to follow the steps below to add product attribute columns to Woocommerce backend grid: Register and sort the new columns via a filter hook (‘manage_edit-product_columns’):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php add_filter( 'manage_edit-product_columns', 'add_columns_to_product_grid', 10, 1 ); const BACKEND_PRODUCT_GRID_FIELD_SORTORDER = [ 'cb', 'thumb', 'name', 'pa_size_text', 'sku', 'is_in_stock', 'price', 'product_cat', 'product_tag', 'featured', 'product_type', 'date', 'stats', 'likes' ]; /** * Registers new columns for the backend products grid of Woocommerce. * Additionally it sorts the fields after * self::BACKEND_PRODUCT_GRID_FIELD_SORTORDER. Fields not included in * self::BACKEND_PRODUCT_GRID_FIELD_SORTORDER will be attached to the end of * the array. * * @param array $aColumns - the current Woocommerce backend grid columns * * @return array - the extended backend grid columns array */ public function add_columns_to_product_grid( $aColumns ) { $aColumns['pa_size_text'] = __( 'Unit size', 'sheldon_misc' ); #unset($aColumns['thumb']); $aReturn = []; foreach ( self::BACKEND_PRODUCT_GRID_FIELD_SORTORDER as $sKey ) { if ( isset( $aColumns[ $sKey ] ) ) { $aReturn[ $sKey ] = $aColumns[ $sKey ]; } } /** * search additional unknown fields and attache them to the end */ foreach ( $aColumns as $sKey => $sField ) { if ( ! isset( $aReturn[ $sKey ] ) ) { $aReturn[ $sKey ] = $sField; } } return $aReturn; } |
Add an action hook to fill in the respective value of each row […]