Created
September 30, 2021 14:15
-
-
Save lucasstark/606035c511d1088be74b610b6d4792d7 to your computer and use it in GitHub Desktop.
Add sortable Has Pricing Rules Admin Column for Dynamic Pricing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WC_Dynamic_Pricing_Admin_Product_Column { | |
private static $instance; | |
public static function register() { | |
if ( self::$instance == null ) { | |
self::$instance = new WC_Dynamic_Pricing_Admin_Product_Column(); | |
} | |
} | |
protected function __construct() { | |
add_filter( 'manage_product_posts_columns', [ $this, 'set_has_pricing_rules_column' ] ); | |
add_action( 'manage_product_posts_custom_column', [ $this, 'render_has_pricing_rules_column' ], 10, 2 ); | |
add_filter( 'manage_edit-product_sortable_columns', [ $this, 'sort_has_pricing_rules_column' ], 10, 1 ); | |
add_action( 'pre_get_posts', [ $this, 'order_by_has_pricing_rules' ] ); | |
} | |
public function set_has_pricing_rules_column( $columns ) { | |
$columns['wc_has_pricing_rules'] = __( 'Pricing Rules', 'woocommerce-dynamic-pricing' ); | |
return $columns; | |
} | |
public function render_has_pricing_rules_column( $column, $post_id ) { | |
switch ( $column ) { | |
case 'wc_has_pricing_rules' : | |
$product = wc_get_product( $post_id ); | |
$rules = $product->get_meta( '_pricing_rules' ); | |
if ( $rules && count( $rules ) ) { | |
echo count( $rules ) . ' pricing rules'; | |
} else { | |
_e( 'No pricing rules', 'woocommerce-dynamic-pricing' ); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
public function sort_has_pricing_rules_column( $columns ) { | |
$columns['wc_has_pricing_rules'] = 'pricing_rules'; | |
return $columns; | |
} | |
function order_by_has_pricing_rules( $query ) { | |
if ( !is_admin() ) { | |
return; | |
} | |
$orderby = $query->get( 'orderby' ); | |
if ( 'pricing_rules' == $orderby ) { | |
$query->set( 'meta_key', '_pricing_rules' ); | |
$query->set( 'compare', 'exits' ); | |
} | |
} | |
} | |
WC_Dynamic_Pricing_Admin_Product_Column::register(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment