Advertisement
designerken

Make array from categories in woo

Aug 24th, 2014
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.00 KB | None | 0 0
  1. <?php
  2. /**
  3.  * My Setting class
  4. **/
  5.     if ( ! class_exists( 'WC_my_setting' ) ) {
  6.  
  7.         class WC_my_setting {
  8.  
  9.             public function __construct() {
  10.  
  11.                 add_action( 'wp_enqueue_scripts', array( $this, 'setup_styles' ) ); // Enqueue the styles
  12.                 add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'woocommerce_do_something_on_frontend' ), 30 );    
  13.                
  14.                 // --------------------------------------------------------------------
  15.                 // Not sure where to place this code to pull in product categories needed
  16.                 // --------------------------------------------------------------------
  17.  
  18.                 $categories =  $product->get_categories();
  19.                 if ( $categories ) {
  20.                   foreach ( $categories as $cat ) {
  21.                     $category_options[ $cat->slug ] = $cat->name;
  22.                   }
  23.                 }
  24.  
  25.                 // --------------------------------------------------------------------
  26.                 // This is how I need the array formatted
  27.                 // --------------------------------------------------------------------
  28.  
  29.                 // $category_options = array(
  30.                 // 'clothing' => __( 'Clothing', 'woocommerce' ),
  31.                 // 'hoodies' => __( 'Hoodies', 'woocommerce' ),
  32.                 // 't-shirts' => __( 'T-shirts', 'woocommerce' ),
  33.                 // 'music' => __( 'Music', 'woocommerce' ),
  34.                 // 'albums' => __( 'Albums', 'woocommerce' ),
  35.                 // 'singles' => __( 'Singles', 'woocommerce' ),
  36.                 // 'posters' => __( 'Posters', 'woocommerce' ),
  37.                 // );
  38.  
  39.                 // Init settings
  40.                 $this->settings = array(
  41.                     array(
  42.                         'name' => __( 'My Setting', 'woocommerce-my-setting' ),
  43.                         'type' => 'title',
  44.                         'id' => 'wc_my_setting_options'
  45.                     ),
  46.                     array(
  47.                         'name'      => __( 'Name', 'woocommerce-my-setting'' ),
  48.                         'desc'      => '',
  49.                         'id'        => 'wc_my_setting_category',
  50.                         'type'      => 'multiselect',
  51.                         'options'   => $category_options
  52.                     ),
  53.  
  54.                     array( 'type' => 'sectionend', 'id' => 'woocommerce-my-setting' ),
  55.                 );
  56.  
  57.  
  58.                 // Default options
  59.                 add_option( 'wc_my_setting_category', '' );
  60.  
  61.  
  62.                 // Admin
  63.                 add_action( 'woocommerce_settings_image_options_after', array( $this, 'admin_settings' ), 20 );
  64.                 add_action( 'woocommerce_update_options_catalog', array( $this, 'save_admin_settings' ) );
  65.                 add_action( 'woocommerce_update_options_products', array( $this, 'save_admin_settings' ) );
  66.             }
  67.  
  68.                     /*-----------------------------------------------------------------------------------*/
  69.             /* Class Functions */
  70.             /*-----------------------------------------------------------------------------------*/
  71.            
  72.             // Load the settings
  73.             function admin_settings() {
  74.                 woocommerce_admin_fields( $this->settings );
  75.             }
  76.  
  77.  
  78.             // Save the settings
  79.             function save_admin_settings() {
  80.                 woocommerce_update_options( $this->settings );
  81.             }
  82.  
  83.  
  84.             // Setup styles
  85.             function setup_styles() {
  86.                 if ( apply_filters( 'woocommerce_enqueue_styles', true ) ) {
  87.                     wp_enqueue_style( 'wc-my-setting-styles', plugins_url( '/assets/css/style.css', __FILE__ ) );
  88.                 }
  89.             }
  90.  
  91.         }
  92.  
  93.         $WC_nb = new WC_my_setting();
  94.     }
  95. }
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement