Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.91 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Product attributes
  4.  *
  5.  * @version     3.1.0
  6.  */
  7.  
  8. if ( ! defined( 'ABSPATH' ) ) {
  9.     exit;
  10. }
  11.  
  12. global $post;
  13. $current_id = $post->ID; // current product ID
  14. $permalink = get_permalink($current); // current product URL
  15.  
  16. $terms = get_the_terms( $current, 'product_cat' ); // current product category
  17.  
  18. foreach ($terms as $term) {
  19.     $product_cat_id = $term->term_id;
  20.     if( get_field( 'variations_from_cat', 'product_cat_'. $term->term_id ) ) {
  21.         $variations_from_cat = get_field( 'variations_from_cat', 'product_cat_'. $term->term_id ); // category product variations
  22.     }
  23.     break;
  24. }
  25.  
  26. if( get_field( 'use_product_variations' ) ) {
  27.     $use_variations = get_field( 'use_product_variations' ); // product has variations AND do not use category variations
  28. }
  29.  
  30. if( $use_variations[0]=='Y' ) {
  31.  
  32.     $variations_ids = array();
  33.     if( get_field( 'product_variations' ) ) {
  34.         $variations = get_field( 'product_variations' );
  35.         if( $variations ) {
  36.             foreach ( $variations as $variation ) {
  37.                 $variations_ids[] = $variation->ID;  // get product variation IDs
  38.             }
  39.         }
  40.     }
  41. }
  42.  
  43.  
  44.     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.
  45.         $args = array(
  46.             'numberposts'   => -1,
  47.             'post_type'     => 'product',
  48.             'post_status'           => 'publish',
  49.             'meta_query'    => array(
  50.                 array(
  51.                     'key'       => 'product_variations',
  52.                     'value'     => $current_id,
  53.                     'compare'   => 'LIKE'
  54.                 ),
  55.             )
  56.         );
  57.  
  58.         $the_query = new WP_Query( $args );
  59.         if( $the_query->have_posts() ) {
  60.  
  61.             while ( $the_query->have_posts() ){
  62.                 $the_query->the_post();
  63.  
  64.                 if( get_field( 'product_variations' ) ) {
  65.                     $variations = get_field( 'product_variations' );
  66.                     if( $variations ) {
  67.                         foreach ( $variations as $variation ) {
  68.                             $variations_ids[] = $variation->ID;  // get linked product variation IDs
  69.                         }
  70.                     }
  71.                 }
  72.  
  73.                 $variations_ids[] = get_the_id();
  74.             }
  75.         }
  76.     }
  77.  
  78.     if( !empty( $variations_ids ) && !in_array($current_id,$variations_ids ) ) {
  79.         $variations_ids[] = $current_id;
  80.         $variations_ids = array_unique($variations_ids);
  81.     }
  82.  
  83.  
  84. if( !empty( $variations_ids ) ) {
  85.     $query = array( 'post_type' => 'product', 'post__in' => $variations_ids ) ;
  86. } else if ( $variations_from_cat[0] == 'Y' ) {
  87.     $query = array(
  88.         'tax_query'             => array(
  89.             array(
  90.                 'taxonomy'      => 'product_cat',
  91.                 'field'         => 'term_id', //This is optional, as it defaults to 'term_id'
  92.                 'terms'         => $product_cat_id,
  93.                 'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
  94.             ),
  95.         )
  96.     );
  97.  
  98. } else { // show just THIS product variations
  99.     $query = array( 'p' => $current_id );
  100. }
  101. $args = array(
  102.     'post_type'             => 'product',
  103.     'post_status'           => 'publish',
  104.     'posts_per_page'        => -1
  105. );
  106. $args = (isset($query) && !empty($query) ) ? array_merge($args, $query) : $args;
  107.  
  108. $taxonomies = array();
  109. $temp_taxonomies  = get_taxonomies( array(), 'obects' );
  110. $i = 0;
  111.  
  112.  
  113.  
  114. foreach($temp_taxonomies as $key=>$tax) {
  115.  
  116.     if( strpos($tax->name, 'pa_') === 0 ) {
  117.         $taxonomies[$tax->name]['slug'] = $tax->name;
  118.         $taxonomies[$tax->name]['name'] = $tax->labels->singular_name;
  119.         $i++;
  120.     }
  121. }
  122. unset($temp_taxonomies);
  123.  
  124. $attr_terms = array();
  125. $current    = array();
  126. foreach($taxonomies as $key=>$taxonomy){
  127.     $attr_terms[$key] = get_terms ( array( 'taxonomy' => $key ) );
  128.     $current[$key]    = get_the_terms ( $current_id, $key );
  129. }
  130.  
  131. function searchForId($id, $array) {
  132.    foreach ($array as $key => $val) {
  133.        if ($val['term_id'] === $id) {
  134.            return $val['meta_value'];
  135.        }
  136.    }
  137.    return null;
  138. }
  139.  
  140. $all_products = new WP_Query($args);
  141.  
  142. $attributes_term_taxonomes = array();
  143. $products_attributes = array();
  144. $i=0;
  145. while ( $all_products->have_posts() ) {
  146.     $all_products->the_post();
  147.     $product_id = get_the_id();
  148.  
  149.     $products_attributes[$i]['url'] = get_permalink($product_id);
  150.     foreach($taxonomies as $key=>$taxonomy){
  151.         if ( get_the_terms ( $product_id, $key ) ) $products_attributes[$i]['values'][$key] = get_the_terms ( $product_id, $key );
  152.     }
  153.  
  154.     foreach($products_attributes[$i]['values'] as $value) {
  155.         if( !in_array( $value[0]->taxonomy, $attributes_term_taxonomes ) && $value[0]->taxonomy )
  156.             $attributes_term_taxonomes[] = $value[0]->taxonomy;
  157.     }
  158.  
  159.     $i++;
  160.  
  161. }
  162.  
  163.  
  164.  
  165.  
  166. $taxonomies_view = array();
  167. foreach($taxonomies as $key=>$taxonomy){
  168.     if( in_array( $key, $attributes_term_taxonomes ) )
  169.         $taxonomies_view[$key] = $taxonomy;
  170. }
  171. $taxonomies = $taxonomies_view;
  172. unset($taxonomies_view);
  173.  
  174. $array_sort = array( 'pa_memory', 'pa_model', 'pa_color' );
  175. foreach($array_sort as $key=>$arr) {
  176.     if( !in_array( $arr, $attributes_term_taxonomes ) ) unset($array_sort[$key]);
  177. }
  178. $taxonomies = array_merge(array_flip($array_sort),$taxonomies);
  179.  
  180. $all_products_ = new WP_Query($args);
  181. $attributes = array();
  182. $i=0;
  183. while ( $all_products_->have_posts() ) {
  184.     $all_products_->the_post();
  185.     $product_id = get_the_id();
  186.     $attr_value = $attr_key = array();
  187.     foreach($taxonomies as $key=>$tax){
  188.         $attr_value[$key] = get_the_terms( $product_id, $key );
  189.     }
  190.     $keys = array();
  191.     $keys = array_keys($attr_value);
  192.     $key_count = 0;
  193.  
  194.  
  195.  
  196.     foreach($attr_value as $key=>$value){
  197.         if( $value[0]->slug == $current[$key][0]->slug ) {
  198.             $attributes[$keys[$key_count+1]][$i]['name']    = $attr_value[$keys[$key_count+1]][0]->name;
  199.             $attributes[$keys[$key_count+1]][$i]['slug']    = $attr_value[$keys[$key_count+1]][0]->slug;
  200.             $attributes[$keys[$key_count+1]][$i]['url']         = get_permalink($product_id);
  201.             $attributes[$keys[$key_count+1]][$i]['description']     = $attr_value[$keys[$key_count+1]][0]->description;
  202.             $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;
  203.             $attributes[$keys[$key_count+1]][$i]['id']      = $attr_value[$keys[$key_count+1]][0]->term_id;
  204.             if( $current_id == $product_id )
  205.                 $attributes[$keys[$key_count+1]][$i]['current'] = true;
  206.         }
  207.         if( $attr_value[$keys[$key_count+1]][0]->slug == $current[$keys[$key_count+1]][0]->slug ) {
  208.             $attributes[$key][$i]['name']       = $value[0]->name;
  209.             $attributes[$key][$i]['slug']   = $value[0]->slug;
  210.             $attributes[$key][$i]['description']    = $value[0]->description;
  211.             $attributes[$key][$i]['id']         = $value[0]->term_id;
  212.             $attributes[$key][$i]['order']  = ($value[0]->meta_value) ? $value[0]->meta_value : 0;
  213.             $attributes[$key][$i]['url']    = get_permalink($product_id);
  214.             if( $current_id == $product_id )
  215.                 $attributes[$key][$i]['current'] = true;
  216.  
  217.         }
  218.         break;
  219.     }
  220.     $i++;
  221. }
  222.  
  223. $array_sort = array( 'pa_memory', 'pa_model', 'pa_color' );
  224.  
  225. foreach($array_sort as $key=>$arr){
  226.     if(!in_array($arr, array_keys($taxonomies))) unset($array_sort[$key]);
  227. }
  228.  
  229. $attributes = array_merge(array_flip($array_sort),$attributes);
  230.  
  231. function cmp($a, $b) {
  232.     if ($a['order'] == $b['order']) {
  233.         return 0;
  234.     }
  235.     return ($a['order'] < $b['order']) ? -1 : 1;
  236. }
  237.  
  238. foreach($attributes as $key=>$attribute){
  239.     uasort($attribute, 'cmp');
  240.     $attributes[$key] = $attribute;
  241. }
  242.  
  243. ?>
  244.  
  245. <?php
  246. foreach($taxonomies as $key => $attribute){
  247.     if($key == 'pa_color') {?>
  248.         <div id="product-attribute-color" class="catalog-filter-box">
  249.             <ul class="list-unstyled list-inline m-0">
  250.                 <li class="list-inline-item"><?php echo $attribute['name']; ?>:</li>
  251.     <?php foreach($attributes[$key] as $value) {
  252.         if($permalink == $value['url']){?>
  253.             <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>
  254.         <?php } else {?>
  255.             <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>
  256.     <?php } } ?>
  257.             </ul>
  258.         </div>
  259.     <?php
  260.     } else {?>
  261.         <div id="product-attribute-memory" class="product-attributes-box">
  262.             <ul class="list-unstyled list-inline m-0">
  263.                 <li class="list-inline-item"><strong><?php echo $attribute['name']; ?>:</strong></li>
  264.     <?php foreach($attributes[$key] as $value) {
  265.         if($permalink == $value['url']){?>
  266.             <li class="list-inline-item"><div<?php echo ($value['current']===true) ? ' class="active"' : ''; ?>><?php echo $value['name']; ?></div></li>
  267.         <?php } else {?>
  268.             <li class="list-inline-item"><a href="<?php echo $value['url']; ?>"<?php echo ($value['current']===true) ? ' class="active"' : ''; ?>><?php echo $value['name']; ?></a></li>
  269.     <?php } } ?>
  270.             </ul>
  271.         </div>
  272.     <?php
  273.     }
  274. }
  275.  
  276.  
  277. wp_reset_postdata();
  278. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement