| 1 |
<?php |
| 2 |
/** |
| 3 |
* Product methods. |
| 4 |
* |
| 5 |
* @package Orderable/Classes |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Products class. |
| 12 |
*/ |
| 13 |
class Orderable_Products { |
| 14 |
/** |
| 15 |
* Init |
| 16 |
*/ |
| 17 |
public static function run() { |
| 18 |
add_filter( 'woocommerce_format_price_range', array( __CLASS__, 'format_price_range' ), 10, 3 ); |
| 19 |
add_filter( 'woocommerce_product_is_visible', array( __CLASS__, 'set_product_visibility' ), 10, 2 ); |
| 20 |
add_action( 'template_redirect', array( __CLASS__, 'products_404' ) ); |
| 21 |
add_filter( 'woocommerce_cart_item_permalink', array( __CLASS__, 'disable_cart_link' ), 10, 3 ); |
| 22 |
add_filter( 'woocommerce_product_query_tax_query', array( __CLASS__, 'remove_hidden_categories_from_products_query' ), 10, 2 ); |
| 23 |
add_filter( 'get_terms_args', array( __CLASS__, 'remove_hidden_categories_from_terms_query' ), 10, 2 ); |
| 24 |
add_filter( 'wp_sitemaps_posts_query_args', array( __CLASS__, 'remove_hidden_products_from_sitemap' ), 10, 2 ); |
| 25 |
add_filter( 'wp_sitemaps_taxonomies_query_args', array( __CLASS__, 'remove_hidden_categories_from_sitemap' ), 10, 2 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Format price range. |
| 30 |
* |
| 31 |
* @param string $price |
| 32 |
* @param float $from |
| 33 |
* @param float $to |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
public static function format_price_range( $price, $from, $to ) { |
| 38 |
return sprintf( '%s: %s', __( 'From', 'iconic' ), wc_price( $from ) ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get products, sorted by category. |
| 43 |
* |
| 44 |
* @param array $args |
| 45 |
* |
| 46 |
* @return array |
| 47 |
*/ |
| 48 |
public static function get_products_by_category( $args = array() ) { |
| 49 |
// Disable this filter as we don't want to disable categories during our own calls to them. |
| 50 |
remove_filter( 'get_terms_args', array( __CLASS__, 'remove_hidden_categories_from_terms_query' ), 10 ); |
| 51 |
|
| 52 |
$categories = ! empty( $args['categories'] ) ? $args['categories'] : array(); |
| 53 |
$categories = is_string( $categories ) ? array_filter( explode( ',', $categories ) ) : $categories; |
| 54 |
$has_categories = ! empty( $categories ); |
| 55 |
|
| 56 |
$categories = self::order_categories_by_menu_order( $categories ); |
| 57 |
|
| 58 |
$orderby = ! defined( 'ORDERABLE_PRO_VERSION' ) || empty( $_GET['order_by'] ) ? '' : sanitize_text_field( wp_unslash( $_GET['order_by'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 59 |
|
| 60 |
if ( empty( $orderby ) ) { |
| 61 |
$orderby = ! defined( 'ORDERABLE_PRO_VERSION' ) || empty( $args['sort'] ) ? 'menu_order' : $args['sort']; |
| 62 |
} |
| 63 |
|
| 64 |
$products = array(); |
| 65 |
|
| 66 |
/** |
| 67 |
* Filter description. |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* @hook orderable_flatten_products_by_category_level |
| 71 |
* @param string $flatten_level The flatten level. Default: `all`. |
| 72 |
* @param array $args The layout args. |
| 73 |
* @return string New value |
| 74 |
*/ |
| 75 |
$flatten_level = apply_filters( 'orderable_flatten_products_by_category_level', 'all', $args ); |
| 76 |
|
| 77 |
if ( 'all' === $flatten_level ) { |
| 78 |
$products[] = array( |
| 79 |
'category' => array(), |
| 80 |
'products' => array(), |
| 81 |
); |
| 82 |
|
| 83 |
$categories_slug = array_filter( |
| 84 |
array_map( |
| 85 |
function( $category_id ) { |
| 86 |
$category = get_term_by( 'id', $category_id, 'product_cat' ); |
| 87 |
|
| 88 |
return empty( $category->slug ) ? false : $category->slug; |
| 89 |
}, |
| 90 |
$categories |
| 91 |
) |
| 92 |
); |
| 93 |
|
| 94 |
$products[0]['products'] = self::get_products( |
| 95 |
array( |
| 96 |
'category' => $categories_slug, |
| 97 |
'orderby' => $orderby, |
| 98 |
'limit' => 500, |
| 99 |
) |
| 100 |
); |
| 101 |
|
| 102 |
return $products; |
| 103 |
} |
| 104 |
|
| 105 |
if ( $has_categories ) { |
| 106 |
foreach ( $categories as $category_id ) { |
| 107 |
$category = get_term( $category_id, 'product_cat' ); |
| 108 |
|
| 109 |
if ( is_wp_error( $category ) || empty( $category ) ) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
$products[ $category_id ] = array( |
| 114 |
'category' => array( |
| 115 |
'name' => $category->name, |
| 116 |
'slug' => $category->slug, |
| 117 |
'description' => $category->description, |
| 118 |
'depth' => 0, |
| 119 |
'children' => null, |
| 120 |
), |
| 121 |
'products' => array(), |
| 122 |
); |
| 123 |
|
| 124 |
$children_categories = get_terms( |
| 125 |
array( |
| 126 |
'taxonomy' => 'product_cat', |
| 127 |
'orderby' => 'menu_order', |
| 128 |
'hide_empty' => true, |
| 129 |
'parent' => $category->term_id, |
| 130 |
) |
| 131 |
); |
| 132 |
|
| 133 |
if ( ! empty( $children_categories ) ) { |
| 134 |
$products[ $category_id ]['category']['children'] = array(); |
| 135 |
|
| 136 |
foreach ( $children_categories as $child_category ) { |
| 137 |
$category_products = self::get_products( |
| 138 |
array( |
| 139 |
'limit' => 500, |
| 140 |
'category' => array( $child_category->slug ), |
| 141 |
'orderby' => $orderby, |
| 142 |
) |
| 143 |
); |
| 144 |
|
| 145 |
if ( empty( $category_products ) ) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
if ( 'children' === $flatten_level ) { |
| 150 |
$products_in_category = empty( $products[ $category_id ]['products'] ) ? array() : $products[ $category_id ]['products']; |
| 151 |
$products[ $category_id ]['products'] = array_merge( $products_in_category, $category_products ); |
| 152 |
|
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
$products[ $category_id ]['category']['children'][ $child_category->term_id ] = array( |
| 157 |
'category' => array( |
| 158 |
'name' => $child_category->name, |
| 159 |
'description' => $child_category->description, |
| 160 |
'depth' => 1, |
| 161 |
'parent' => $category_id, |
| 162 |
), |
| 163 |
'products' => $category_products, |
| 164 |
); |
| 165 |
} |
| 166 |
} else { |
| 167 |
$category_products = self::get_products( |
| 168 |
array( |
| 169 |
'limit' => 500, |
| 170 |
'category' => array( $category->slug ), |
| 171 |
'orderby' => $orderby, |
| 172 |
) |
| 173 |
); |
| 174 |
|
| 175 |
if ( ! empty( $category_products ) ) { |
| 176 |
$products[ $category_id ]['products'] = $category_products; |
| 177 |
|
| 178 |
// Add parent attribute if parent is a root category. |
| 179 |
if ( in_array( $category->parent, $categories, true ) ) { |
| 180 |
$products[ $category_id ]['category']['parent'] = $category->parent; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} else { |
| 186 |
$category_products = self::get_products( |
| 187 |
array( |
| 188 |
'limit' => 500, |
| 189 |
'orderby' => $orderby, |
| 190 |
) |
| 191 |
); |
| 192 |
|
| 193 |
if ( ! empty( $category_products ) ) { |
| 194 |
$products[] = array( |
| 195 |
'category' => null, |
| 196 |
'products' => $category_products, |
| 197 |
); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
// Remove categories with no products. |
| 202 |
foreach ( $products as $key => $product_collection ) { |
| 203 |
if ( ! empty( $product_collection['products'] ) || ! empty( $product_collection['category']['children'] ) ) { |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
unset( $products[ $key ] ); |
| 208 |
} |
| 209 |
|
| 210 |
// Turn this back on to re-disable hidden categories. |
| 211 |
add_filter( 'get_terms_args', array( __CLASS__, 'remove_hidden_categories_from_terms_query' ), 10, 2 ); |
| 212 |
|
| 213 |
$products = self::maybe_flatten_products_by_category( array_filter( $products ), $args ); |
| 214 |
|
| 215 |
return apply_filters( 'orderable_get_products_by_category', $products, $args ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Get Products. |
| 220 |
* |
| 221 |
* A wrapper around the WooCommerce `wc_get_products` and `wc_products_array_orderby` functions. |
| 222 |
* |
| 223 |
* @param array $args Arguments. |
| 224 |
* |
| 225 |
* @return array |
| 226 |
*/ |
| 227 |
public static function get_products( $args ) { |
| 228 |
$args['status'] = 'publish'; // Ensure only published products are returned. |
| 229 |
|
| 230 |
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 231 |
$args['stock_status'] = 'instock'; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Filter arguments used to retrieve products from database |
| 236 |
* |
| 237 |
* @param array $args WC_Product_Query arguments |
| 238 |
* |
| 239 |
* @return array New query arguments |
| 240 |
* @since 1.2.1 |
| 241 |
* @hook orderable_get_products_args |
| 242 |
*/ |
| 243 |
$products = wc_get_products( apply_filters( 'orderable_get_products_args', $args ) ); |
| 244 |
|
| 245 |
if ( ! empty( $products ) ) { |
| 246 |
$orderby = empty( $args['orderby'] ) ? 'menu_order' : $args['orderby']; |
| 247 |
|
| 248 |
$order = 'price-desc' === $orderby ? 'desc' : 'asc'; |
| 249 |
$orderby = 'price-desc' === $orderby ? 'price' : $orderby; |
| 250 |
|
| 251 |
$products = wc_products_array_orderby( $products, $orderby, $order ); |
| 252 |
} |
| 253 |
|
| 254 |
return apply_filters( 'orderable_get_products', $products, $args ); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Order Categories by menu order |
| 259 |
* |
| 260 |
* @param array $categories Categories. |
| 261 |
* |
| 262 |
* @return array |
| 263 |
*/ |
| 264 |
public static function order_categories_by_menu_order( $categories ) { |
| 265 |
$categories_ordered = array(); |
| 266 |
$category_terms = get_terms( |
| 267 |
array( |
| 268 |
'taxonomy' => 'product_cat', |
| 269 |
'orderby' => 'menu_order', |
| 270 |
'hide_empty' => true, |
| 271 |
) |
| 272 |
); |
| 273 |
|
| 274 |
foreach ( $category_terms as $term ) { |
| 275 |
if ( ! in_array( $term->term_id, $categories, true ) ) { |
| 276 |
continue; |
| 277 |
} |
| 278 |
|
| 279 |
$categories_ordered[] = $term->term_id; |
| 280 |
} |
| 281 |
|
| 282 |
return $categories_ordered; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Maybe flatten products by category. |
| 287 |
* |
| 288 |
* @param array $products |
| 289 |
* @param array $args |
| 290 |
* |
| 291 |
* @return array |
| 292 |
*/ |
| 293 |
public static function maybe_flatten_products_by_category( $products, $args = array() ) { |
| 294 |
// If we're already listing all products with no categories, escape. |
| 295 |
if ( empty( $products ) || isset( $products[0] ) ) { |
| 296 |
return $products; |
| 297 |
} |
| 298 |
|
| 299 |
$flatten_level = apply_filters( 'orderable_flatten_products_by_category_level', 'all', $args ); |
| 300 |
|
| 301 |
// Don't flatten if set to "none". |
| 302 |
if ( 'none' === $flatten_level ) { |
| 303 |
return $products; |
| 304 |
} |
| 305 |
|
| 306 |
$flattened_products = array(); |
| 307 |
|
| 308 |
if ( 'children' === $flatten_level ) { |
| 309 |
$orderby = empty( $args['sort'] ) ? 'menu_order' : $args['sort']; |
| 310 |
|
| 311 |
$order = 'price-desc' === $orderby ? 'desc' : 'asc'; |
| 312 |
$orderby = 'price-desc' === $orderby ? 'price' : $orderby; |
| 313 |
|
| 314 |
foreach ( $products as $category_id => $product_group ) { |
| 315 |
$product_group['products'] = wc_products_array_orderby( $product_group['products'], $orderby, $order ); |
| 316 |
|
| 317 |
$flattened_products[ $category_id ] = $product_group; |
| 318 |
} |
| 319 |
} else { |
| 320 |
$flattened_products[] = array( |
| 321 |
'category' => null, |
| 322 |
'products' => array(), |
| 323 |
); |
| 324 |
|
| 325 |
foreach ( $products as $product_group ) { |
| 326 |
if ( ! empty( $product_group['products'] ) ) { |
| 327 |
// Add category products to list. |
| 328 |
$flattened_products[0]['products'] = array_merge( $flattened_products[0]['products'], $product_group['products'] ); |
| 329 |
|
| 330 |
continue; |
| 331 |
} elseif ( ! empty( $product_group['category']['children'] ) ) { |
| 332 |
foreach ( $product_group['category']['children'] as $child_product_group ) { |
| 333 |
// Add category products to list. |
| 334 |
$flattened_products[0]['products'] = array_merge( $flattened_products[0]['products'], $child_product_group['products'] ); |
| 335 |
|
| 336 |
continue; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
return $flattened_products; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Get add to cart button. |
| 347 |
* |
| 348 |
* @param WC_Product $product Product. |
| 349 |
* @param string $classes Button classes. |
| 350 |
* |
| 351 |
* @return string |
| 352 |
*/ |
| 353 |
public static function get_add_to_cart_button( $product, $classes = '' ) { |
| 354 |
global $orderable_single_product; |
| 355 |
|
| 356 |
$args = array( |
| 357 |
'trigger' => self::get_add_to_cart_trigger( $product ), |
| 358 |
'product_id' => $product->get_id(), |
| 359 |
'product_type' => $product->get_type(), |
| 360 |
'variation_id' => null, |
| 361 |
'variation_attributes' => array(), |
| 362 |
'text' => __( 'Add', 'orderable' ), |
| 363 |
'classes' => $classes, |
| 364 |
); |
| 365 |
|
| 366 |
if ( 'variable' === $args['product_type'] ) { |
| 367 |
$args['text'] = empty( $orderable_single_product ) ? __( 'Select', 'orderable' ) : $args['text']; |
| 368 |
} elseif ( 'variation' === $args['product_type'] ) { |
| 369 |
$args['product_id'] = $product->get_parent_id(); |
| 370 |
$args['variation_id'] = $product->get_id(); |
| 371 |
} |
| 372 |
|
| 373 |
if ( ! $product->is_in_stock() ) { |
| 374 |
$args['classes'] .= ' orderable-button--out-of-stock'; |
| 375 |
$args['text'] = __( 'Out of Stock', 'orderable' ); |
| 376 |
} |
| 377 |
|
| 378 |
$args = apply_filters( 'orderable_add_to_cart_button_args', $args, $product ); |
| 379 |
|
| 380 |
return sprintf( |
| 381 |
'<button |
| 382 |
class="orderable-button %1$s" |
| 383 |
data-orderable-trigger="%2$s" |
| 384 |
data-orderable-product-id="%3$d" |
| 385 |
data-orderable-product-type="%4$s" |
| 386 |
data-orderable-variation-id="%5$d" |
| 387 |
data-orderable-variation-attributes="" |
| 388 |
data-quantity="1" |
| 389 |
data-product_id="%3$d" |
| 390 |
data-product_sku="%6$s" |
| 391 |
data-product_name="%7$s" |
| 392 |
data-price="%8$s" |
| 393 |
> |
| 394 |
%9$s |
| 395 |
</button>', |
| 396 |
esc_attr( $args['classes'] ), |
| 397 |
esc_attr( $args['trigger'] ), |
| 398 |
esc_attr( $args['product_id'] ), |
| 399 |
esc_attr( $args['product_type'] ), |
| 400 |
esc_attr( $args['variation_id'] ), |
| 401 |
esc_attr( $product->get_sku() ), |
| 402 |
esc_attr( $product->get_name() ), |
| 403 |
esc_attr( $product->get_price() ), |
| 404 |
wp_kses_post( $args['text'] ) |
| 405 |
); |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Get update cart item button. |
| 410 |
* |
| 411 |
* @param string $cart_item_key The cart item key. |
| 412 |
* @param WC_Product $product The product to be edited. |
| 413 |
* @param string $classes Button classes. |
| 414 |
* |
| 415 |
* @return string |
| 416 |
*/ |
| 417 |
public static function get_update_cart_item_button( $cart_item_key, $product, $classes = '' ) { |
| 418 |
$cart_item = WC()->cart->get_cart_item( $cart_item_key ); |
| 419 |
|
| 420 |
$args = array( |
| 421 |
'trigger' => 'update-cart-item', |
| 422 |
'product_id' => $cart_item['product_id'], |
| 423 |
'cart_item_key' => $cart_item_key, |
| 424 |
'variation_id' => $cart_item['variation_id'], |
| 425 |
'variation_attributes' => empty( array_filter( $cart_item['variation'] ) ) ? false : wp_json_encode( $cart_item['variation'] ), |
| 426 |
'product_type' => $product->get_type(), |
| 427 |
'text' => __( 'Update', 'orderable' ), |
| 428 |
'classes' => $classes, |
| 429 |
); |
| 430 |
|
| 431 |
/** |
| 432 |
* Filter arguments used to the update cart item button. |
| 433 |
* |
| 434 |
* @param array $args The arguments. |
| 435 |
* |
| 436 |
* @return array New arguments |
| 437 |
* @since 1.4.0 |
| 438 |
* @hook orderable_update_cart_item_button_args |
| 439 |
*/ |
| 440 |
$args = apply_filters( 'orderable_update_cart_item_button_args', $args, $cart_item_key ); |
| 441 |
|
| 442 |
ob_start(); |
| 443 |
?> |
| 444 |
<button |
| 445 |
class="orderable-button orderable-product__cancel-update" |
| 446 |
data-orderable-trigger="show-cart" |
| 447 |
> |
| 448 |
<?php echo esc_html__( 'Cancel', 'orderable' ); ?> |
| 449 |
</button> |
| 450 |
<button |
| 451 |
class="orderable-button <?php echo esc_attr( $args['classes'] ); ?>" |
| 452 |
data-orderable-trigger="<?php echo esc_attr( $args['trigger'] ); ?>" |
| 453 |
data-orderable-cart-item-key="<?php echo esc_attr( $args['cart_item_key'] ); ?>" |
| 454 |
data-orderable-product-id="<?php echo esc_attr( $args['product_id'] ); ?>" |
| 455 |
data-orderable-variation-id="<?php echo esc_attr( $args['variation_id'] ); ?>" |
| 456 |
data-orderable-variation-attributes="<?php echo esc_attr( $args['variation_attributes'] ); ?>" |
| 457 |
data-orderable-product-type="<?php echo esc_attr( $args['product_type'] ); ?>" |
| 458 |
> |
| 459 |
<?php echo esc_html( $args['text'] ); ?> |
| 460 |
</button> |
| 461 |
<?php |
| 462 |
|
| 463 |
/** |
| 464 |
* Filter the Update Cart Item button HTML. |
| 465 |
* |
| 466 |
* @param array $update_cart_item_button_html The Update Cart Item button HTML. |
| 467 |
* |
| 468 |
* @return array New HTML |
| 469 |
* @since 1.4.0 |
| 470 |
* @hook orderable_update_cart_item_button_html |
| 471 |
*/ |
| 472 |
$html = apply_filters( 'orderable_update_cart_item_button_html', ob_get_clean() ); |
| 473 |
|
| 474 |
return $html; |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* Get add to cart trigger value. |
| 479 |
* |
| 480 |
* @param WC_Product $product |
| 481 |
* |
| 482 |
* @return string |
| 483 |
*/ |
| 484 |
public static function get_add_to_cart_trigger( $product ) { |
| 485 |
$trigger = $product->is_type( 'variable' ) ? 'product-options' : 'add-to-cart'; |
| 486 |
|
| 487 |
return apply_filters( 'orderable_get_add_to_cart_trigger', $trigger, $product ); |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Get a list of attributes for available variations. |
| 492 |
* |
| 493 |
* @param WC_Product_Variable $product |
| 494 |
* |
| 495 |
* @return array |
| 496 |
*/ |
| 497 |
public static function get_available_variation_attributes( $product ) { |
| 498 |
$available_variation_attributes = array(); |
| 499 |
$available_variations = $product->get_available_variations(); |
| 500 |
$available_variations = wc_list_pluck( $available_variations, 'attributes' ); |
| 501 |
|
| 502 |
if ( empty( $available_variations ) ) { |
| 503 |
return $available_variation_attributes; |
| 504 |
} |
| 505 |
|
| 506 |
foreach ( $available_variations as $available_variation ) { |
| 507 |
foreach ( $available_variation as $attribute_slug => $attribute_value ) { |
| 508 |
if ( ! isset( $available_variation_attributes[ $attribute_slug ] ) ) { |
| 509 |
$available_variation_attributes[ $attribute_slug ] = array(); |
| 510 |
} |
| 511 |
|
| 512 |
$available_variation_attributes[ $attribute_slug ][] = $attribute_value; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
return $available_variation_attributes; |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* Get available attributes. |
| 521 |
* |
| 522 |
* This method remove an attributes which don't belong to |
| 523 |
* an active variation. |
| 524 |
* |
| 525 |
* @param WC_Product_Variable $product |
| 526 |
* |
| 527 |
* @return array |
| 528 |
*/ |
| 529 |
public static function get_available_attributes( $product ) { |
| 530 |
$available_attributes = array(); |
| 531 |
$attributes = $product->get_variation_attributes(); |
| 532 |
$available_variation_attributes = self::get_available_variation_attributes( $product ); |
| 533 |
|
| 534 |
if ( empty( $attributes ) ) { |
| 535 |
return $available_attributes; |
| 536 |
} |
| 537 |
|
| 538 |
foreach ( $attributes as $attribute_name => $attribute_terms ) { |
| 539 |
$attribute_name_sanitized = wc_variation_attribute_name( $attribute_name ); |
| 540 |
|
| 541 |
if ( empty( $available_variation_attributes[ $attribute_name_sanitized ] ) ) { |
| 542 |
continue; |
| 543 |
} |
| 544 |
|
| 545 |
if ( ! isset( $available_attributes[ $attribute_name ] ) ) { |
| 546 |
$available_attributes[ $attribute_name ] = array(); |
| 547 |
} |
| 548 |
|
| 549 |
foreach ( $attribute_terms as $attribute_term ) { |
| 550 |
if ( ! in_array( $attribute_term, $available_variation_attributes[ $attribute_name_sanitized ], true ) && ! in_array( '', $available_variation_attributes[ $attribute_name_sanitized ], true ) ) { |
| 551 |
continue; |
| 552 |
} |
| 553 |
|
| 554 |
$available_attributes[ $attribute_name ][] = $attribute_term; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
return $available_attributes; |
| 559 |
} |
| 560 |
|
| 561 |
/** |
| 562 |
* Is product single page hidden? |
| 563 |
* |
| 564 |
* @param int|WC_Product $product |
| 565 |
* |
| 566 |
* @return bool |
| 567 |
*/ |
| 568 |
public static function is_product_hidden( $product ) { |
| 569 |
if ( is_numeric( $product ) ) { |
| 570 |
$product = wc_get_product( $product ); |
| 571 |
} |
| 572 |
|
| 573 |
if ( empty( $product ) ) { |
| 574 |
return false; |
| 575 |
} |
| 576 |
|
| 577 |
$categories = $product->get_category_ids(); |
| 578 |
|
| 579 |
if ( empty( $categories ) ) { |
| 580 |
return false; |
| 581 |
} |
| 582 |
|
| 583 |
foreach ( $categories as $category_id ) { |
| 584 |
if ( self::is_category_hidden( $category_id ) ) { |
| 585 |
return true; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
return false; |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Is this category hidden? |
| 594 |
* |
| 595 |
* @param $category_id |
| 596 |
* |
| 597 |
* @return bool |
| 598 |
*/ |
| 599 |
public static function is_category_hidden( $category_id ) { |
| 600 |
$hidden_categories = Orderable_Settings::get_hidden_categories(); |
| 601 |
|
| 602 |
return in_array( $category_id, $hidden_categories, true ); |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* Set if product is visible, based on category settings. |
| 607 |
* |
| 608 |
* @param bool $visible |
| 609 |
* @param int $product_id |
| 610 |
* |
| 611 |
* @return bool |
| 612 |
*/ |
| 613 |
public static function set_product_visibility( $visible, $product_id ) { |
| 614 |
$hidden = self::is_product_hidden( $product_id ); |
| 615 |
|
| 616 |
return $hidden ? false : $visible; |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Disable cart permalink. |
| 621 |
* |
| 622 |
* @param string $permalink |
| 623 |
* |
| 624 |
* @return bool |
| 625 |
*/ |
| 626 |
public static function disable_cart_link( $permalink, $cart_item, $cart_item_key ) { |
| 627 |
if ( ! isset( $cart_item['data'] ) ) { |
| 628 |
return $permalink; |
| 629 |
} |
| 630 |
|
| 631 |
return self::is_product_hidden( $cart_item['data'] ) ? false : $permalink; |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
* Set product page to 404 if required. |
| 636 |
*/ |
| 637 |
public static function products_404() { |
| 638 |
if ( is_admin() || ! ( is_product() && is_single() ) ) { |
| 639 |
return; |
| 640 |
} |
| 641 |
|
| 642 |
global $post; |
| 643 |
|
| 644 |
if ( empty( $post ) ) { |
| 645 |
return; |
| 646 |
} |
| 647 |
|
| 648 |
$product = wc_get_product( $post->ID ); |
| 649 |
|
| 650 |
if ( empty( $product ) || ! self::is_product_hidden( $product ) ) { |
| 651 |
return; |
| 652 |
} |
| 653 |
|
| 654 |
global $wp_query; |
| 655 |
|
| 656 |
$wp_query->set_404(); |
| 657 |
status_header( 404 ); |
| 658 |
nocache_headers(); |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
* Exclude hidden categories from queries. |
| 663 |
* |
| 664 |
* @param $tax_query |
| 665 |
* @param $wc_query |
| 666 |
* |
| 667 |
* @return array |
| 668 |
*/ |
| 669 |
public static function remove_hidden_categories_from_products_query( $tax_query, $wc_query ) { |
| 670 |
$hidden_categories = Orderable_Settings::get_hidden_categories(); |
| 671 |
|
| 672 |
if ( empty( $hidden_categories ) ) { |
| 673 |
return $tax_query; |
| 674 |
} |
| 675 |
|
| 676 |
$tax_query[] = array( |
| 677 |
'taxonomy' => 'product_cat', |
| 678 |
'field' => 'term_id', |
| 679 |
'terms' => $hidden_categories, |
| 680 |
'operator' => 'NOT IN', |
| 681 |
); |
| 682 |
|
| 683 |
return $tax_query; |
| 684 |
} |
| 685 |
|
| 686 |
/** |
| 687 |
* Remove hidden categories from terms query. |
| 688 |
* |
| 689 |
* As well as hiding from terms lists (sidebar widgets, etc), |
| 690 |
* this also disables the archive page. |
| 691 |
* |
| 692 |
* @param array $args Args. |
| 693 |
* @param array $taxonomies Taxonomies. |
| 694 |
* |
| 695 |
* @return mixed |
| 696 |
*/ |
| 697 |
public static function remove_hidden_categories_from_terms_query( $args, $taxonomies ) { |
| 698 |
if ( is_admin() ) { |
| 699 |
return $args; |
| 700 |
} |
| 701 |
|
| 702 |
$taxonomy = ! empty( $args['taxonomy'] ) && isset( $args['taxonomy'][0] ) ? $args['taxonomy'][0] : false; |
| 703 |
|
| 704 |
if ( 'product_cat' !== $taxonomy || is_admin() ) { |
| 705 |
return $args; |
| 706 |
} |
| 707 |
|
| 708 |
// Exclude hidden categories. |
| 709 |
$hidden_categories = Orderable_Settings::get_hidden_categories(); |
| 710 |
$args['exclude'] = ! is_array( $args['exclude'] ) ? array() : $args['exclude']; |
| 711 |
$args['exclude'] = array_merge( $args['exclude'], $hidden_categories ); |
| 712 |
|
| 713 |
return $args; |
| 714 |
} |
| 715 |
|
| 716 |
/** |
| 717 |
* Remove hidden products from sitemap. |
| 718 |
* |
| 719 |
* @param array $query_args Query args. |
| 720 |
* @param string $post_type Post type. |
| 721 |
* |
| 722 |
* @return mixed |
| 723 |
*/ |
| 724 |
public static function remove_hidden_products_from_sitemap( $query_args, $post_type ) { |
| 725 |
if ( 'product' !== $post_type ) { |
| 726 |
return $query_args; |
| 727 |
} |
| 728 |
|
| 729 |
$hidden_categories = Orderable_Settings::get_hidden_categories(); |
| 730 |
|
| 731 |
if ( empty( $hidden_categories ) ) { |
| 732 |
return $query_args; |
| 733 |
} |
| 734 |
|
| 735 |
$tax_query = array( |
| 736 |
'taxonomy' => 'product_cat', |
| 737 |
'terms' => $hidden_categories, |
| 738 |
'field' => 'term_id', |
| 739 |
'include_children' => true, |
| 740 |
'operator' => 'NOT IN', |
| 741 |
); |
| 742 |
|
| 743 |
if ( ! isset( $query_args['tax_query'] ) ) { |
| 744 |
$query_args['tax_query'] = array(); |
| 745 |
} |
| 746 |
|
| 747 |
$query_args['tax_query'][] = $tax_query; |
| 748 |
|
| 749 |
return $query_args; |
| 750 |
} |
| 751 |
|
| 752 |
/** |
| 753 |
* Exclude hidden categories from sitemap. |
| 754 |
* |
| 755 |
* @param array $query_args Query args. |
| 756 |
* @param string $taxonomy Taxonomy. |
| 757 |
* |
| 758 |
* @return mixed |
| 759 |
*/ |
| 760 |
public static function remove_hidden_categories_from_sitemap( $query_args, $taxonomy ) { |
| 761 |
if ( 'product_cat' !== $taxonomy ) { |
| 762 |
return $query_args; |
| 763 |
} |
| 764 |
|
| 765 |
$hidden_categories = Orderable_Settings::get_hidden_categories(); |
| 766 |
|
| 767 |
if ( empty( $hidden_categories ) ) { |
| 768 |
return $query_args; |
| 769 |
} |
| 770 |
|
| 771 |
if ( ! isset( $query_args['exclude'] ) ) { |
| 772 |
$query_args['exclude'] = $hidden_categories; |
| 773 |
} else { |
| 774 |
if ( is_array( $hidden_categories ) ) { |
| 775 |
$query_args['exclude'] = array_merge( $query_args['exclude'], $hidden_categories ); |
| 776 |
} else { |
| 777 |
$query_args['exclude'] = $query_args['exclude'] . ',' . implode( ',', $hidden_categories ); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
return $query_args; |
| 782 |
} |
| 783 |
|
| 784 |
/** |
| 785 |
* Get product accordion data. |
| 786 |
* |
| 787 |
* @param WC_Product $product Product. |
| 788 |
* |
| 789 |
* @return array |
| 790 |
*/ |
| 791 |
public static function get_accordion_data( $product ) { |
| 792 |
$data = array(); |
| 793 |
|
| 794 |
$description = Orderable_Settings::get_setting( 'drawer_quickview_description' ); |
| 795 |
|
| 796 |
if ( 'none' !== $description ) { |
| 797 |
$description = 'short' === $description ? $product->get_short_description() : $product->get_description(); |
| 798 |
|
| 799 |
$data[] = array( |
| 800 |
'title' => __( 'Description', 'orderable' ), |
| 801 |
'content' => apply_filters( 'the_content', $description ), |
| 802 |
'id' => 'accordion-description', |
| 803 |
); |
| 804 |
} |
| 805 |
|
| 806 |
/** |
| 807 |
* Filter product accordion data. |
| 808 |
* |
| 809 |
* @var array $data |
| 810 |
* @var WC_Product $product |
| 811 |
*/ |
| 812 |
return apply_filters( 'orderable_get_accordion_data', $data, $product ); |
| 813 |
} |
| 814 |
} |
| 815 |
|