Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Product attributes
- *
- * @version 3.1.0
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- global $post;
- $current_id = $post->ID; // current product ID
- $permalink = get_permalink($current); // current product URL
- $terms = get_the_terms( $current, 'product_cat' ); // current product category
- foreach ($terms as $term) {
- $product_cat_id = $term->term_id;
- if( get_field( 'variations_from_cat', 'product_cat_'. $term->term_id ) ) {
- $variations_from_cat = get_field( 'variations_from_cat', 'product_cat_'. $term->term_id ); // category product variations
- }
- break;
- }
- if( get_field( 'use_product_variations' ) ) {
- $use_variations = get_field( 'use_product_variations' ); // product has variations AND do not use category variations
- }
- if( $use_variations[0]=='Y' ) {
- $variations_ids = array();
- if( get_field( 'product_variations' ) ) {
- $variations = get_field( 'product_variations' );
- if( $variations ) {
- foreach ( $variations as $variation ) {
- $variations_ids[] = $variation->ID; // get product variation IDs
- }
- }
- }
- }
- if($use_variations[0]=='Y' && empty($variations_ids)){ // if product uses variations BUT doesn't have variation IDs linked. We search for items with this product linked.
- $args = array(
- 'numberposts' => -1,
- 'post_type' => 'product',
- 'post_status' => 'publish',
- 'meta_query' => array(
- array(
- 'key' => 'product_variations',
- 'value' => $current_id,
- 'compare' => 'LIKE'
- ),
- )
- );
- $the_query = new WP_Query( $args );
- if( $the_query->have_posts() ) {
- while ( $the_query->have_posts() ){
- $the_query->the_post();
- if( get_field( 'product_variations' ) ) {
- $variations = get_field( 'product_variations' );
- if( $variations ) {
- foreach ( $variations as $variation ) {
- $variations_ids[] = $variation->ID; // get linked product variation IDs
- }
- }
- }
- $variations_ids[] = get_the_id();
- }
- }
- }
- if( !empty( $variations_ids ) && !in_array($current_id,$variations_ids ) ) {
- $variations_ids[] = $current_id;
- $variations_ids = array_unique($variations_ids);
- }
- if( !empty( $variations_ids ) ) {
- $query = array( 'post_type' => 'product', 'post__in' => $variations_ids ) ;
- } else if ( $variations_from_cat[0] == 'Y' ) {
- $query = array(
- 'tax_query' => array(
- array(
- 'taxonomy' => 'product_cat',
- 'field' => 'term_id', //This is optional, as it defaults to 'term_id'
- 'terms' => $product_cat_id,
- 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
- ),
- )
- );
- } else { // show just THIS product variations
- $query = array( 'p' => $current_id );
- }
- $args = array(
- 'post_type' => 'product',
- 'post_status' => 'publish',
- 'posts_per_page' => -1
- );
- $args = (isset($query) && !empty($query) ) ? array_merge($args, $query) : $args;
- $taxonomies = array();
- $temp_taxonomies = get_taxonomies( array(), 'obects' );
- $i = 0;
- foreach($temp_taxonomies as $key=>$tax) {
- if( strpos($tax->name, 'pa_') === 0 ) {
- $taxonomies[$tax->name]['slug'] = $tax->name;
- $taxonomies[$tax->name]['name'] = $tax->labels->singular_name;
- $i++;
- }
- }
- unset($temp_taxonomies);
- $attr_terms = array();
- $current = array();
- foreach($taxonomies as $key=>$taxonomy){
- $attr_terms[$key] = get_terms ( array( 'taxonomy' => $key ) );
- $current[$key] = get_the_terms ( $current_id, $key );
- }
- function searchForId($id, $array) {
- foreach ($array as $key => $val) {
- if ($val['term_id'] === $id) {
- return $val['meta_value'];
- }
- }
- return null;
- }
- $all_products = new WP_Query($args);
- $attributes_term_taxonomes = array();
- $products_attributes = array();
- $i=0;
- while ( $all_products->have_posts() ) {
- $all_products->the_post();
- $product_id = get_the_id();
- $products_attributes[$i]['url'] = get_permalink($product_id);
- foreach($taxonomies as $key=>$taxonomy){
- if ( get_the_terms ( $product_id, $key ) ) $products_attributes[$i]['values'][$key] = get_the_terms ( $product_id, $key );
- }
- foreach($products_attributes[$i]['values'] as $value) {
- if( !in_array( $value[0]->taxonomy, $attributes_term_taxonomes ) && $value[0]->taxonomy )
- $attributes_term_taxonomes[] = $value[0]->taxonomy;
- }
- $i++;
- }
- $taxonomies_view = array();
- foreach($taxonomies as $key=>$taxonomy){
- if( in_array( $key, $attributes_term_taxonomes ) )
- $taxonomies_view[$key] = $taxonomy;
- }
- $taxonomies = $taxonomies_view;
- unset($taxonomies_view);
- $array_sort = array( 'pa_memory', 'pa_model', 'pa_color' );
- foreach($array_sort as $key=>$arr) {
- if( !in_array( $arr, $attributes_term_taxonomes ) ) unset($array_sort[$key]);
- }
- $taxonomies = array_merge(array_flip($array_sort),$taxonomies);
- $all_products_ = new WP_Query($args);
- $attributes = array();
- $i=0;
- while ( $all_products_->have_posts() ) {
- $all_products_->the_post();
- $product_id = get_the_id();
- $attr_value = $attr_key = array();
- foreach($taxonomies as $key=>$tax){
- $attr_value[$key] = get_the_terms( $product_id, $key );
- }
- $keys = array();
- $keys = array_keys($attr_value);
- $key_count = 0;
- foreach($attr_value as $key=>$value){
- if( $value[0]->slug == $current[$key][0]->slug ) {
- $attributes[$keys[$key_count+1]][$i]['name'] = $attr_value[$keys[$key_count+1]][0]->name;
- $attributes[$keys[$key_count+1]][$i]['slug'] = $attr_value[$keys[$key_count+1]][0]->slug;
- $attributes[$keys[$key_count+1]][$i]['url'] = get_permalink($product_id);
- $attributes[$keys[$key_count+1]][$i]['description'] = $attr_value[$keys[$key_count+1]][0]->description;
- $attributes[$keys[$key_count+1]][$i]['order'] = ($attr_value[$keys[$key_count+1]][0]->meta_value) ? $attr_value[$keys[$key_count+1]][0]->meta_value : 0;
- $attributes[$keys[$key_count+1]][$i]['id'] = $attr_value[$keys[$key_count+1]][0]->term_id;
- if( $current_id == $product_id )
- $attributes[$keys[$key_count+1]][$i]['current'] = true;
- }
- if( $attr_value[$keys[$key_count+1]][0]->slug == $current[$keys[$key_count+1]][0]->slug ) {
- $attributes[$key][$i]['name'] = $value[0]->name;
- $attributes[$key][$i]['slug'] = $value[0]->slug;
- $attributes[$key][$i]['description'] = $value[0]->description;
- $attributes[$key][$i]['id'] = $value[0]->term_id;
- $attributes[$key][$i]['order'] = ($value[0]->meta_value) ? $value[0]->meta_value : 0;
- $attributes[$key][$i]['url'] = get_permalink($product_id);
- if( $current_id == $product_id )
- $attributes[$key][$i]['current'] = true;
- }
- break;
- }
- $i++;
- }
- $array_sort = array( 'pa_memory', 'pa_model', 'pa_color' );
- foreach($array_sort as $key=>$arr){
- if(!in_array($arr, array_keys($taxonomies))) unset($array_sort[$key]);
- }
- $attributes = array_merge(array_flip($array_sort),$attributes);
- function cmp($a, $b) {
- if ($a['order'] == $b['order']) {
- return 0;
- }
- return ($a['order'] < $b['order']) ? -1 : 1;
- }
- foreach($attributes as $key=>$attribute){
- uasort($attribute, 'cmp');
- $attributes[$key] = $attribute;
- }
- ?>
- <?php
- foreach($taxonomies as $key => $attribute){
- if($key == 'pa_color') {?>
- <div id="product-attribute-color" class="catalog-filter-box">
- <ul class="list-unstyled list-inline m-0">
- <li class="list-inline-item"><?php echo $attribute['name']; ?>:</li>
- <?php foreach($attributes[$key] as $value) {
- if($permalink == $value['url']){?>
- <li class="list-inline-item"><div title="<?php echo $value['name']; ?>"<?php echo ($value['current']===true) ? ' class="active"' : ''; ?>><span<?php echo ($value['description']) ? ' style="background-color:'. $value['description'] .'"' : ''; ?> class="color-box <?php echo $value['slug']; ?>"><?php echo $value['name']; ?></span></div></li>
- <?php } else {?>
- <li class="list-inline-item"><a title="<?php echo $value['name']; ?>" href="<?php echo $value['url']; ?>"<?php echo ($value['current']===true) ? ' class="active"' : ''; ?>><span<?php echo ($value['description']) ? ' style="background-color:'. $value['description'] .'"' : ''; ?> class="color-box <?php echo $value['slug']; ?>"><?php echo $value['name']; ?></span></a></li>
- <?php } } ?>
- </ul>
- </div>
- <?php
- } else {?>
- <div id="product-attribute-memory" class="product-attributes-box">
- <ul class="list-unstyled list-inline m-0">
- <li class="list-inline-item"><strong><?php echo $attribute['name']; ?>:</strong></li>
- <?php foreach($attributes[$key] as $value) {
- if($permalink == $value['url']){?>
- <li class="list-inline-item"><div<?php echo ($value['current']===true) ? ' class="active"' : ''; ?>><?php echo $value['name']; ?></div></li>
- <?php } else {?>
- <li class="list-inline-item"><a href="<?php echo $value['url']; ?>"<?php echo ($value['current']===true) ? ' class="active"' : ''; ?>><?php echo $value['name']; ?></a></li>
- <?php } } ?>
- </ul>
- </div>
- <?php
- }
- }
- wp_reset_postdata();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement