vendredi 27 février 2015

How to place an array in a class [on hold]


I cannot figure out how to pull in the categories for the products from the backend as an array to load into a class. I can code a loop to pull in the data on a normal page, but cant figure out how to do it in a class.


You can see that I am trying to populate the $productcats with all the categories of the products. I can code it in manually, I just cannot seem to get it to work dynamically pulling in from the database.



if ( ! class_exists( 'WC_my_sweet_plugin' ) ) {

class WC_my_sweet_plugin {
public function __construct() {
add_action( 'woocommerce_before_shop_loop_item_title', array( $this, 'woocommerce_show_product_loop_my_sweet_plugin' ), 30 );

//Manually added array - how to pull this data in is my problem.
$productcats = array(
'clothing' => __( 'Clothing', 'woocommerce-my-sweet-plugin' ),
'hoodies' => __( 'Hoodies', 'woocommerce-my-sweet-plugin' ),
't-shirts' => __( 'T-shirts', 'woocommerce-my-sweet-plugin' ),
'music' => __( 'Music', 'woocommerce-my-sweet-plugin' ),
'albums' => __( 'Albums', 'woocommerce-my-sweet-plugin' ),
'singles' => __( 'Singles', 'woocommerce-my-sweet-plugin' ),
'posters' => __( 'Posters', 'woocommerce-my-sweet-plugin' ),
);

// Init settings
$this->settings = array(
array(
'name' => __( 'My Sweet Plugin', 'woocommerce-my-sweet-plugin' ),
'type' => 'title',
'id' => 'wc_my_sweet_plugin_options'
),
array(
'name' => __( 'Product Category', 'woocommerce-my-sweet-plugin' ),
'desc' => '<br/>' . __( 'Select the product categories you want the Free Shipping badge to appear on.', 'woocommerce-my-sweet-plugin' ),
'id' => 'wc_my_sweet_plugin_category',
'css' => 'min-width:400px;',
'desc_tip' => __( "A cool tool tip that is displayed on hover.", 'woocommerce-my-sweet-plugin' ),
'class' => 'wc-enhanced-select',
'type' => 'multiselect',
'options' => $productcats
),
array( 'type' => 'sectionend', 'id' => 'wc_my_sweet_plugin_options' ),
);


// Default options
add_option( 'wc_my_sweet_plugin_category', '' );


// Admin
add_action( 'woocommerce_settings_image_options_after', array( $this, 'admin_settings' ), 20 );
add_action( 'woocommerce_update_options_catalog', array( $this, 'save_admin_settings' ) );
add_action( 'woocommerce_update_options_products', array( $this, 'save_admin_settings' ) );
}


I have tried adding this after the __construct() just to see if I can pull in the categories but I only get undefined variable & get property of non-object errors:



$category_ids = (array) get_post_meta( $post->ID, 'product_categories', true );
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );

if ( $categories ) foreach ( $categories as $cat ) {
echo '<pre>' . esc_attr( $cat->term_id ) . '"' . selected( in_array( $cat->term_id, $category_ids ), true, false ) . '>' . esc_html( $cat->name ) . '</pre>';
}




Aucun commentaire:

Enregistrer un commentaire