julien731

Add SKU to Product Name

Mar 2nd, 2016
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. /**
  2.  * Create a workaround class for backward compatibility
  3.  */
  4. if ( ! function_exists( 'WPAS' ) ) {
  5.  
  6.     class WPAS_Workaround {
  7.         public $products_sync;
  8.     }
  9.  
  10.     function WPAS() {
  11.         return new WPAS_Workaround();
  12.     }
  13.  
  14. }
  15.  
  16. add_filter( 'wpas_hierarchical_taxonomy_dropdown_options_label', 'wpas_custom_product_label', 10, 3 );
  17. /**
  18.  * Add SKU after product name in the products dropdown
  19.  *
  20.  * @param string $label The option label
  21.  * @param WP_Term $term The term object
  22.  * @param mixed $value Option value if any set
  23.  *
  24.  * @return string
  25.  */
  26. function wpas_custom_product_label( $label, $term, $value ) {
  27.  
  28.     global $wpas_product_sync;
  29.  
  30.     if ( 'product' !== $term->taxonomy ) {
  31.         return $label;
  32.     }
  33.  
  34.     $is_sync = WPAS()->products_sync->is_synced_term( $term->term_id );
  35.  
  36.     if ( false === $is_sync ) {
  37.         return $label;
  38.     }
  39.  
  40.     $sku = get_post_meta( $is_sync, '_sku', true );
  41.     $label = "$label - $sku";
  42.  
  43.     return $label;
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment