| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
// Exit if accessed directly. |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
if ( ! function_exists( 'qi_blocks_is_woo_page' ) ) { |
| 9 |
/** |
| 10 |
* Function that check WooCommerce pages |
| 11 |
* |
| 12 |
* @param string $page |
| 13 |
* |
| 14 |
* @return bool |
| 15 |
*/ |
| 16 |
function qi_blocks_is_woo_page( $page ) { |
| 17 |
switch ( $page ) { |
| 18 |
case 'shop': |
| 19 |
return function_exists( 'is_shop' ) && is_shop(); |
| 20 |
case 'single': |
| 21 |
return is_singular( 'product' ); |
| 22 |
case 'cart': |
| 23 |
return function_exists( 'is_cart' ) && is_cart(); |
| 24 |
case 'checkout': |
| 25 |
return function_exists( 'is_checkout' ) && is_checkout(); |
| 26 |
case 'account': |
| 27 |
return function_exists( 'is_account_page' ) && is_account_page(); |
| 28 |
case 'category': |
| 29 |
return function_exists( 'is_product_category' ) && is_product_category(); |
| 30 |
case 'tag': |
| 31 |
return function_exists( 'is_product_tag' ) && is_product_tag(); |
| 32 |
case 'any': |
| 33 |
return ( |
| 34 |
function_exists( 'is_shop' ) && is_shop() || |
| 35 |
is_singular( 'product' ) || |
| 36 |
function_exists( 'is_cart' ) && is_cart() || |
| 37 |
function_exists( 'is_checkout' ) && is_checkout() || |
| 38 |
function_exists( 'is_account_page' ) && is_account_page() || |
| 39 |
function_exists( 'is_product_category' ) && is_product_category() || |
| 40 |
function_exists( 'is_product_tag' ) && is_product_tag() || |
| 41 |
function_exists( 'is_product_taxonomy' ) && is_product_taxonomy() |
| 42 |
); |
| 43 |
case 'archive': |
| 44 |
return ( function_exists( 'is_shop' ) && is_shop() ) || ( function_exists( 'is_product_category' ) && is_product_category() ) || ( function_exists( 'is_product_tag' ) && is_product_tag() ) || ( function_exists( 'is_product_taxonomy' ) && is_product_taxonomy() ); |
| 45 |
default: |
| 46 |
return false; |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! function_exists( 'qi_blocks_woo_get_global_product' ) ) { |
| 52 |
/** |
| 53 |
* Function that return global WooCommerce object |
| 54 |
* |
| 55 |
* @return object |
| 56 |
*/ |
| 57 |
function qi_blocks_woo_get_global_product() { |
| 58 |
global $product; |
| 59 |
|
| 60 |
return $product; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
if ( ! function_exists( 'qi_blocks_woo_get_main_shop_page_id' ) ) { |
| 65 |
/** |
| 66 |
* Function that return main shop page ID |
| 67 |
* |
| 68 |
* @param int $page_id |
| 69 |
* |
| 70 |
* @return int |
| 71 |
*/ |
| 72 |
function qi_blocks_woo_get_main_shop_page_id( $page_id = 0 ) { |
| 73 |
// Get page id from options table. |
| 74 |
$shop_id = get_option( 'woocommerce_shop_page_id' ); |
| 75 |
|
| 76 |
if ( ! empty( $shop_id ) ) { |
| 77 |
$page_id = $shop_id; |
| 78 |
} |
| 79 |
|
| 80 |
return $page_id; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ( ! function_exists( 'qi_blocks_get_additional_product_query_args' ) ) { |
| 85 |
/** |
| 86 |
* Function that return additional query arguments |
| 87 |
* |
| 88 |
* @param array $atts - options value |
| 89 |
* |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
function qi_blocks_get_additional_product_query_args( $atts ) { |
| 93 |
$args = qi_blocks_get_additional_query_args( $atts ); |
| 94 |
|
| 95 |
if ( ! empty( $atts['filterBy'] ) ) { |
| 96 |
switch ( $atts['filterBy'] ) { |
| 97 |
case 'on_sale': |
| 98 |
$args['no_found_rows'] = 1; |
| 99 |
$args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() ); |
| 100 |
break; |
| 101 |
case 'featured': |
| 102 |
$args['tax_query'] = WC()->query->get_tax_query(); |
| 103 |
|
| 104 |
$args['tax_query'][] = array( |
| 105 |
'taxonomy' => 'product_visibility', |
| 106 |
'terms' => 'featured', |
| 107 |
'field' => 'name', |
| 108 |
'operator' => 'IN', |
| 109 |
'include_children' => false, |
| 110 |
); |
| 111 |
break; |
| 112 |
case 'top_rated': |
| 113 |
$args['meta_key'] = '_wc_average_rating'; |
| 114 |
$args['order'] = 'DESC'; |
| 115 |
$args['orderby'] = 'meta_value_num'; |
| 116 |
break; |
| 117 |
case 'best_selling': |
| 118 |
$args['meta_key'] = 'total_sales'; |
| 119 |
$args['order'] = 'DESC'; |
| 120 |
$args['orderby'] = 'meta_value_num'; |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return $args; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if ( ! function_exists( 'qi_blocks_get_product_list_holder_classes' ) ) { |
| 130 |
/** |
| 131 |
* Function that return element holder classes |
| 132 |
* |
| 133 |
* @param array $atts - options value |
| 134 |
* |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
function qi_blocks_get_product_list_holder_classes( $atts ) { |
| 138 |
$classes = qi_blocks_get_block_holder_classes( 'product-list', $atts, 'qi-blocks-woo-block' ); |
| 139 |
|
| 140 |
$classes[] = ! empty( $atts['columnClasses'] ) ? $atts['columnClasses'] : ''; |
| 141 |
$classes[] = ! empty( $atts['itemLayout'] ) ? 'qodef-item-layout--' . $atts['itemLayout'] : ''; |
| 142 |
$classes[] = ! empty( $atts['imageHover'] ) ? 'qodef-image--hover-' . $atts['imageHover'] : ''; |
| 143 |
$classes[] = ! empty( $atts['imageZoomOrigin'] ) ? 'qodef-image--hover-from-' . $atts['imageZoomOrigin'] : ''; |
| 144 |
|
| 145 |
return implode( ' ', $classes ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if ( ! function_exists( 'qi_blocks_woo_get_product_categories' ) ) { |
| 150 |
/** |
| 151 |
* Function that render product categories |
| 152 |
* |
| 153 |
* @param string $before |
| 154 |
* @param string $after |
| 155 |
* |
| 156 |
* @return string |
| 157 |
*/ |
| 158 |
function qi_blocks_woo_get_product_categories( $before = '', $after = '' ) { |
| 159 |
$product = qi_blocks_woo_get_global_product(); |
| 160 |
|
| 161 |
return ! empty( $product ) ? wc_get_product_category_list( $product->get_id(), '<span class="qodef-category-separator"></span>', $before, $after ) : ''; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if ( ! function_exists( 'qi_blocks_get_out_of_stock_mark' ) ) { |
| 166 |
/** |
| 167 |
* Function for adding out of stock template for product |
| 168 |
* |
| 169 |
* @return string which contains html content |
| 170 |
*/ |
| 171 |
function qi_blocks_get_out_of_stock_mark() { |
| 172 |
return '<span class="qodef-block-woo-product-mark qodef-out-of-stock">' . esc_html__( 'Sold', 'qi-blocks' ) . '</span>'; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if ( ! function_exists( 'qi_blocks_woo_set_sale_flash' ) ) { |
| 177 |
/** |
| 178 |
* Function that override on sale template for product |
| 179 |
* |
| 180 |
* @return string which contains html content |
| 181 |
*/ |
| 182 |
function qi_blocks_woo_set_sale_flash() { |
| 183 |
$product = qi_blocks_woo_get_global_product(); |
| 184 |
|
| 185 |
if ( ! empty( $product ) && $product->is_on_sale() && $product->is_in_stock() ) { |
| 186 |
$sale_label = esc_html__( 'Sale', 'qi-blocks' ); |
| 187 |
|
| 188 |
return '<span class="qodef-block-woo-product-mark qodef-woo-onsale">' . $sale_label . '</span>'; |
| 189 |
} |
| 190 |
|
| 191 |
return ''; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if ( ! function_exists( 'qi_blocks_generate_add_to_cart_button_params' ) ) { |
| 196 |
/** |
| 197 |
* Function for generating add to cart button params |
| 198 |
* |
| 199 |
* @param array $atts - options value |
| 200 |
* |
| 201 |
* @return array |
| 202 |
*/ |
| 203 |
function qi_blocks_generate_add_to_cart_button_params( $atts ) { |
| 204 |
$product = qi_blocks_woo_get_global_product(); |
| 205 |
$params = array(); |
| 206 |
|
| 207 |
if ( $product ) { |
| 208 |
$args = array( |
| 209 |
'quantity' => 1, |
| 210 |
'class' => implode( |
| 211 |
' ', |
| 212 |
array_filter( |
| 213 |
array( |
| 214 |
'button', |
| 215 |
'product_type_' . $product->get_type(), |
| 216 |
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '', |
| 217 |
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '', |
| 218 |
) |
| 219 |
) |
| 220 |
), |
| 221 |
'attributes' => array( |
| 222 |
'data-product_id' => $product->get_id(), |
| 223 |
'data-product_sku' => $product->get_sku(), |
| 224 |
'aria-label' => $product->add_to_cart_description(), |
| 225 |
'rel' => 'nofollow', |
| 226 |
), |
| 227 |
); |
| 228 |
|
| 229 |
if ( isset( $args['attributes']['aria-label'] ) ) { |
| 230 |
$args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] ); |
| 231 |
} |
| 232 |
|
| 233 |
if ( ! empty( $args['class'] ) ) { |
| 234 |
$atts['custom_class'] = $args['class']; |
| 235 |
} |
| 236 |
|
| 237 |
if ( count( $args['attributes'] ) ) { |
| 238 |
$params['custom_attrs'] = $args['attributes']; |
| 239 |
$params['custom_attrs']['data-quantity'] = $args['quantity']; |
| 240 |
} |
| 241 |
|
| 242 |
$params['button_link'] = array( |
| 243 |
'url' => $product->add_to_cart_url(), |
| 244 |
); |
| 245 |
|
| 246 |
$params['buttonText'] = $product->add_to_cart_text(); |
| 247 |
|
| 248 |
$params['button_classes'] = qi_blocks_button_get_holder_classes( $atts ); |
| 249 |
$params['icon_classes'] = qi_blocks_button_get_icon_classes( $atts ); |
| 250 |
$params['data_attrs'] = array_merge( array( 'target' => '_self' ), $params['custom_attrs'] ); |
| 251 |
} |
| 252 |
|
| 253 |
return $params; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if ( ! function_exists( 'qi_blocks_woo_product_get_rating_html' ) ) { |
| 258 |
/** |
| 259 |
* Function that return ratings templates |
| 260 |
* |
| 261 |
* @param string $html - contains html content |
| 262 |
* @param float $rating |
| 263 |
* |
| 264 |
* @return string |
| 265 |
*/ |
| 266 |
function qi_blocks_woo_product_get_rating_html( $html, $rating ) { |
| 267 |
|
| 268 |
if ( ! empty( $rating ) ) { |
| 269 |
$product_rating = apply_filters( |
| 270 |
'qi_blocks_filter_woocommerce_product_rating', |
| 271 |
'<svg class="qodef-m-star-item" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="15" x="0px" y="0px" viewBox="0 0 16.2 15.2" xml:space="preserve"><g><g><path d="M16.1,5.8l-5,3.5l1.9,5.7l-4.9-3.6l-4.9,3.6l1.9-5.7l-5-3.5h6.1l1.9-5.7L10,5.8H16.1z"/></g></g></svg>' |
| 272 |
); |
| 273 |
|
| 274 |
$html = '<div class="qodef-m-inner">'; |
| 275 |
$html .= '<div class="qodef-m-star qodef--initial">' . str_repeat( $product_rating, 5 ) . '</div>'; |
| 276 |
$html .= '<div class="qodef-m-star qodef--active" style="width:' . ( ( $rating / 5 ) * 100 ) . '%">'; |
| 277 |
$html .= str_repeat( $product_rating, 5 ); |
| 278 |
$html .= '</div>'; |
| 279 |
$html .= '</div>'; |
| 280 |
} |
| 281 |
|
| 282 |
return $html; |
| 283 |
} |
| 284 |
} |
| 285 |
|