= array( ['label' => esc_html__('Image', 'wpr-addons'), 'type' => 'image'], ['label' => esc_html__('Name', 'wpr-addons'), 'type' => 'text'], ['label' => esc_html__('Rating', 'wpr-addons'), 'type' => 'text'], ['label' => esc_html__('Description', 'wpr-addons'), 'type' => 'text'], ['label' => esc_html__('Price', 'wpr-addons'), 'type' => 'text'], ['label' => esc_html__('SKU', 'wpr-addons'), 'type' => 'text'], ['label' => esc_html__('Stock Status', 'wpr-addons'), 'type' => 'text'], ['label' => esc_html__('Dimensions', 'wpr-addons'), 'type' => 'text'], ['label' => esc_html__('Weight', 'wpr-addons'), 'type' => 'text'] ); $all_attributes = array(); foreach ( $compare as $product_id ) { $product = wc_get_product( $product_id ); if ( ! $product ) { continue; } $attributes = $product->get_attributes(); foreach ( $attributes as $attribute ) { $attribute_name = wc_attribute_label($attribute->get_name()); if ( !in_array($attribute_name, $all_attributes) ) { $all_attributes[] = $attribute_name; } } } foreach ( $all_attributes as $attribute_name ) { $table_data[] = array('label' => $attribute_name, 'type' => 'text'); } foreach ( $table_data as $row ) { echo ''; echo '' . $row['label'] . ''; foreach ( $compare as $key => $product_id ) { $product = wc_get_product( $product_id ); if ( ! $product ) { continue; } echo ''; switch ( $row['type'] ) { case 'image': echo '' . $product->get_image() . ''; echo '
' . $this->render_product_add_to_cart( $settings, $product ) . '
'; break; case 'text': if( in_array(strtolower($row['label']), [strtolower(__('Description', 'wpr-addons')), strtolower(__('SKU', 'wpr-addons')) ]) ) { echo $product->get_data()[strtolower($row['label'])]; } else if ( strtolower($row['label']) == strtolower(__('Name', 'wpr-addons')) ) { echo ''. $product->get_data()['name'] .''; } else if ( strtolower($row['label']) == strtolower(__('Price', 'wpr-addons')) ) { echo $product->get_price_html(); } else if ( strtolower($row['label']) ==strtolower(__('Rating', 'wpr-addons')) ) { echo $this->render_product_rating($product); } else if ( strtolower($row['label']) == strtolower(__('Stock Status', 'wpr-addons')) ) { $stock_status = $product->get_stock_status(); echo $stock_status == 'instock' ? esc_html__('In Stock', 'wpr-addons') : esc_html__('Out of Stock', 'wpr-addons'); } else if ( strtolower($row['label']) == strtolower(__('Dimensions', 'wpr-addons')) ) { if ( $product->has_dimensions() ) { $dimensions = sprintf( '%s', wc_format_dimensions( $product->get_dimensions( false ) ) ); echo $dimensions; } } else if ( strtolower($row['label']) == strtolower(__('Weight', 'wpr-addons')) ) { if ( $product->get_weight() ) { $weight = sprintf( '%s %s', $product->get_weight(), get_option( 'woocommerce_weight_unit' ) ); echo $weight; } } else { $attributes = $product->get_attributes(); $attribute_name = wc_attribute_label(strtolower($row['label'])); foreach ($product->get_attributes($product_id) as $attr) { if ( strtolower($attr['name']) === strtolower($row['label']) ) { echo $attr['value']; } } // Product Attributes // $attribute_name = $this->format_variable($attribute_name); $attribute_name = $this->get_taxonomy_from_label($attribute_name); if (isset($attributes[$attribute_name])) { // Get the value(s) of the 'dimensions' attribute for the product $attributes_value = $attributes[$attribute_name]->get_options(); $attributes_value_array = []; // Loop through the values and output them foreach ($attributes_value as $value) { $term = get_term($value); $attributes_value_array[] = $term->name; } echo implode(' | ', $attributes_value_array); } } break; } echo ''; } echo ''; } // Close the table echo ''; echo ''; return ob_get_clean(); } public function format_variable($variable) { $variable = strtolower($variable); // Convert to lowercase $variable = str_replace(' ', '-', $variable); // Replace spaces with hyphens return $variable; } public function render_product_rating($product) { // $rating_count = $product->get_rating_count(); // $rating_amount = floatval( $product->get_average_rating() ); // $round_rating = (int)$rating_amount; // $rating_icon = '☆'; // echo '
'; // for ( $i = 1; $i <= 5; $i++ ) { // if ( $i <= $rating_amount ) { // echo ''. $rating_icon .''; // } elseif ( $i === $round_rating + 1 && $rating_amount !== $round_rating ) { // echo ''. $rating_icon .''; // } else { // echo ''. $rating_icon .''; // } // } // echo '
'; // Another option $rating = $product->get_average_rating(); $count = $product->get_rating_count(); return wc_get_rating_html( $rating, $count ); } // Render Add To Cart public function render_product_add_to_cart( $settings, $product ) { // If NOT a Product if ( is_null( $product ) ) { return; } ob_start(); // Get Button Class $button_class = implode( ' ', array_filter( [ 'product_type_'. $product->get_type(), $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '', $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '', ] ) ); $attributes = [ 'rel="nofollow"', 'class="'. esc_attr($button_class) .' wpr-button-effect '. (!$product->is_in_stock() && 'simple' === $product->get_type() ? 'wpr-atc-not-clickable' : '').'"', 'aria-label="'. esc_attr($product->add_to_cart_description()) .'"', 'data-product_id="'. esc_attr($product->get_id()) .'"', 'data-product_sku="'. esc_attr($product->get_sku()) .'"', ]; $button_HTML = ''; $page_id = get_queried_object_id(); // Button Text if ( 'simple' === $product->get_type() ) { $button_HTML .= $settings['element_addcart_simple_txt']; if ( 'yes' === get_option('woocommerce_enable_ajax_add_to_cart') ) { array_push( $attributes, 'href="'. esc_url( get_permalink( $page_id ) .'/?add-to-cart='. get_the_ID() ) .'"' ); } else { array_push( $attributes, 'href="'. esc_url( get_permalink() ) .'"' ); } } elseif ( 'grouped' === $product->get_type() ) { $button_HTML .= $settings['element_addcart_grouped_txt']; array_push( $attributes, 'href="'. esc_url( $product->get_permalink() ) .'"' ); } elseif ( 'variable' === $product->get_type() ) { $button_HTML .= $settings['element_addcart_variable_txt']; array_push( $attributes, 'href="'. esc_url( $product->get_permalink() ) .'"' ); } else { array_push( $attributes, 'href="'. esc_url( $product->get_product_url() ) .'"' ); $button_HTML .= get_post_meta( get_the_ID(), '_button_text', true ) ? get_post_meta( get_the_ID(), '_button_text', true ) : 'Buy Product'; } // Button HTML echo ''. $button_HTML .''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return \ob_get_clean(); } } new WPR_Count_Wishlist_Compare_Items();