Skip to content

Instantly share code, notes, and snippets.

@vimes1984
Created January 26, 2015 21:05
Show Gist options
  • Save vimes1984/cfc7c34bc251439ed233 to your computer and use it in GitHub Desktop.
Save vimes1984/cfc7c34bc251439ed233 to your computer and use it in GitHub Desktop.
<?php
/**
* Dance Energy Generic
*
* @package dance-energy-generic
* @author Christopher Churchill <churchill.c.j@gmail.com>
* @license GPL-2.0+
* @link http://buildawebdoctor.com
* @copyright 8-27-2014 BAWD
*/
/**
* ajaxclass class.
*
* @package FindPartner
* @author Christopher Churchill <churchill.c.j@gmail.com>
*/
class ajaxclass{
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
*
* @var string
*/
protected $version = "1.0.0";
/**
* Unique identifier for your plugin.
*
* Use this value (not the variable name) as the text domain when internationalizing strings of text. It should
* match the Text Domain file header in the main plugin file.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_slug = "GQ-blends";
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
/**
* Slug of the plugin screen.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_screen_hook_suffix = null;
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*
* @since 1.0.0
*/
private function __construct() {
// Load plugin text domain
//shop page and categories
add_action('wp_ajax_nopriv_cats_loop', array($this, 'cats_loop') );
add_action( 'wp_ajax_cats_loop', array($this, 'cats_loop') );
add_action('wp_ajax_nopriv_cats_child_loop', array($this, 'cats_child_loop') );
add_action( 'wp_ajax_cats_child_loop', array($this, 'cats_child_loop') );
add_action('wp_ajax_nopriv_shop_page_loop', array($this, 'shop_page_loop') );
add_action( 'wp_ajax_shop_page_loop', array($this, 'shop_page_loop') );
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn"t been set, set it now.
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
/****
*
*Cart Page
***/
public function cats_loop() {
// Include the client library
$catreturn = array();
$categories = get_terms( 'product_cat', 'orderby=count&hide_empty=0' );
foreach ($categories as $key => $value) {
if($value->parent == '0'){
$catreturn[$key] = $value;
}
}
$catreturn = json_encode( $catreturn );
echo $catreturn;
die(); // this is required to return a proper result
}
/**
*
*/
public function cats_child_loop() {
// Include the client library
$catreturn = array();
$categories = get_terms( 'product_cat', 'orderby=count&hide_empty=0' );
foreach ($categories as $key => $value) {
if($value->parent != '0'){
$catreturn[$key] = $value;
}
}
$catreturn = json_encode( $catreturn );
echo $catreturn;
die(); // this is required to return a proper result
}
/**
*
*/
public function shop_page_loop() {
global $product;
$request_body = file_get_contents( 'php://input' );
$args = array( 'post_type' => 'product', 'product_cat' => $request_body, 'posts_per_page' => '-1', 'post_status'=> 'publish' );
$decodeit = json_decode( $request_body );
$query = new WP_Query( $args );
$justmeta = array( );
$justcats = array( );
$justterms = array( );
$prodsandmeta = array( );
foreach ($query->posts as $key => $value) {
global $product;
$classes = '';
$classesterm = '';
# code...
$prodsandmeta[$key] = $value;
$post_id = $value->ID;
$extended = new WC_Product($post_id);
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'single-post-thumbnail' );
$getprodmet = get_post_meta( $post_id );
$categories = get_the_terms( $post_id, 'product_cat' );
//var_dump($categories);
$posttags = wp_get_object_terms($post_id, 'product_tag');
foreach ($posttags as $subtermkey => $subtermvalue) {
$classes = $classes . ' ' . $subtermvalue->slug;
$justterms = $subtermvalue;
}
foreach ($getprodmet as $subkey => $subvalue){
$justmeta[str_replace('-', '_', $subkey)] = $subvalue[0];
}
foreach ($categories as $subcatkey => $subcatvalue) {
$classes = $classes . ' ' . $subcatvalue->slug;
$justcats[$subcatkey] = $subcatvalue;
}
// var_dump($prodsandmeta);
$prods_atts = $this->get_attributes($post_id, $extended->get_attributes());
$prodsandmeta[$key]->product_att = $prods_atts;
$prodsandmeta[$key]->classes = $classes;
//$prodsandmeta[$key]->classesterm = $classesterm;
$prodsandmeta[$key]->product_meta = $justmeta;
$prodsandmeta[$key]->prod_cat = $justcats;
$prodsandmeta[$key]->prod_term = $justterms;
$prodsandmeta[$key]->permalink = get_permalink( $post_id );
if( $image === false ){
$prodsandmeta[$key]->prod_img = '/wp-content/plugins/woocommerce/assets/images/placeholder.png';
}else{
$prodsandmeta[$key]->prod_img = $image[0];
}
//var_dump($key);
}
$backtoangular = json_encode( $prodsandmeta );
echo $backtoangular;
die(); // this is required to return a proper result
}
/**
*
*/
public function get_products_atts($post_id = 0){
$key = 0;
$extended = new WC_Product($post_id);
$attributes = $extended->get_attributes();
foreach ($attributes as $attribute) {
# code...
$values = wc_get_product_terms( $post_id, $attribute['name'] );
foreach ($values as $keysub => $valuesub) {
# code...
$prods_atts[$attribute['name']][$valuesub] = $valuesub;
}
$key++;
}
return $prods_atts;
}
/**
*
*/
public function get_attributes($post_id, $attributes){
$key = 0;
$attrs = array();
foreach ( $attributes as $attribute ){
# code...
$attrs[$key] = new stdClass();
if ( $attribute['is_taxonomy'] ) {
$values = wc_get_product_terms( $post_id, $attribute['name'], array( 'fields' => 'names' ) );
$attrs[$key]->value = ( apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values ) );
$attrs[$key]->name = wc_attribute_label( $attribute['name'] );
} else {
// Convert pipes to commas and display values
$values = array_map( 'trim', explode( WC_DELIMITER, $attribute['value'] ) );
$attrs[$key]->name = wc_attribute_label( $attribute['name'] );
$attrs[$key]->value = apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
$key++;
}
return $attrs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment