| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module: Drawer. |
| 4 |
* |
| 5 |
* @package Orderable/Classes |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Drawer module class. |
| 12 |
*/ |
| 13 |
class Orderable_Drawer { |
| 14 |
/** |
| 15 |
* Init. |
| 16 |
*/ |
| 17 |
public static function run() { |
| 18 |
self::load_classes(); |
| 19 |
|
| 20 |
add_action( 'wp_footer', array( __CLASS__, 'add' ) ); |
| 21 |
add_filter( 'woocommerce_add_to_cart_fragments', array( __CLASS__, 'cart_count_fragments' ), 10, 1 ); |
| 22 |
add_filter( 'woocommerce_add_to_cart_fragments', array( __CLASS__, 'cart_content_fragments' ), 10, 1 ); |
| 23 |
add_filter( 'woocommerce_add_to_cart_fragments', array( __CLASS__, 'quantity_roller_fragments_on_updating_product' ), 10, 1 ); |
| 24 |
add_filter( 'woocommerce_cart_item_permalink', '__return_false' ); |
| 25 |
add_filter( 'wc_get_template', array( __CLASS__, 'mini_cart_template' ), 100, 5 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Load classes for this module. |
| 30 |
*/ |
| 31 |
public static function load_classes() { |
| 32 |
$classes = array( |
| 33 |
'drawer-settings' => 'Orderable_Drawer_Settings', |
| 34 |
'drawer-ajax' => 'Orderable_Drawer_Ajax', |
| 35 |
); |
| 36 |
|
| 37 |
foreach ( $classes as $file_name => $class_name ) { |
| 38 |
require_once ORDERABLE_MODULES_PATH . 'drawer/class-' . $file_name . '.php'; |
| 39 |
|
| 40 |
$class_name::run(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Add drawer and overlay. |
| 46 |
*/ |
| 47 |
public static function add() { |
| 48 |
if ( is_admin() || is_cart() || is_checkout() ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
include Orderable_Helpers::get_template_path( 'overlay.php', 'drawer' ); |
| 53 |
include Orderable_Helpers::get_template_path( 'drawer.php', 'drawer' ); |
| 54 |
include Orderable_Helpers::get_template_path( 'floating-cart.php', 'drawer' ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Update cart count after adding to cart. |
| 59 |
* |
| 60 |
* @param array $fragments Array of HTML fragments. |
| 61 |
* |
| 62 |
* @return mixed |
| 63 |
*/ |
| 64 |
public static function cart_count_fragments( $fragments ) { |
| 65 |
ob_start(); |
| 66 |
|
| 67 |
include Orderable_Helpers::get_template_path( 'floating-cart.php', 'drawer' ); |
| 68 |
|
| 69 |
$fragments['.orderable-floating-cart'] = ob_get_clean(); |
| 70 |
|
| 71 |
return $fragments; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Update cart content after adding to cart. |
| 76 |
* |
| 77 |
* @param array $fragments Array of HTML fragments. |
| 78 |
* |
| 79 |
* @return mixed |
| 80 |
*/ |
| 81 |
public static function cart_content_fragments( $fragments ) { |
| 82 |
ob_start(); |
| 83 |
|
| 84 |
self::mini_cart(); |
| 85 |
|
| 86 |
$fragments['.orderable-mini-cart-wrapper'] = ob_get_clean(); |
| 87 |
|
| 88 |
if ( Orderable_Helpers::has_notices() ) { |
| 89 |
ob_start(); |
| 90 |
?> |
| 91 |
<div class="orderable-mini-cart__notices orderable-mini-cart__notices--border-top"> |
| 92 |
<?php wc_print_notices(); ?> |
| 93 |
</div> |
| 94 |
<?php |
| 95 |
|
| 96 |
$fragments['.orderable-mini-cart__notices'] = ob_get_clean(); |
| 97 |
} |
| 98 |
|
| 99 |
return $fragments; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Replace mini cart template. |
| 104 |
* |
| 105 |
* @param string $template |
| 106 |
* @param string $template_name |
| 107 |
* @param array $args |
| 108 |
* @param string $template_path |
| 109 |
* @param string $default_path |
| 110 |
* |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public static function mini_cart_template( $template, $template_name, $args, $template_path, $default_path ) { |
| 114 |
if ( 'cart/mini-cart.php' !== $template_name ) { |
| 115 |
return $template; |
| 116 |
} |
| 117 |
|
| 118 |
if ( empty( $args['orderable'] ) ) { |
| 119 |
return $template; |
| 120 |
} |
| 121 |
|
| 122 |
// if file exists in theme, use that. |
| 123 |
$theme_file = get_stylesheet_directory() . '/orderable/cart/mini-cart.php'; |
| 124 |
if ( file_exists( $theme_file ) ) { |
| 125 |
return $theme_file; |
| 126 |
} |
| 127 |
|
| 128 |
return ORDERABLE_PATH . 'woocommerce/cart/mini-cart.php'; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Output mini cart with Orderable param set. |
| 133 |
*/ |
| 134 |
public static function mini_cart() { |
| 135 |
?> |
| 136 |
<div class="orderable-mini-cart-wrapper"> |
| 137 |
<?php woocommerce_mini_cart( array( 'orderable' => true ) ); ?> |
| 138 |
</div> |
| 139 |
<?php |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Update the quantity roller outside the side drawer. |
| 144 |
* |
| 145 |
* @param array $fragments The WooCommerce fragments. |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
public static function quantity_roller_fragments_on_updating_product( $fragments ) { |
| 149 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 150 |
if ( empty( $_POST['action'] ) || empty( $_POST['product_id'] ) ) { |
| 151 |
return $fragments; |
| 152 |
} |
| 153 |
|
| 154 |
$action = sanitize_text_field( wp_unslash( $_POST['action'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 155 |
$product_id = absint( sanitize_text_field( wp_unslash( $_POST['product_id'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 156 |
|
| 157 |
// phpcs:ignore WordPress.Security.NonceVerification |
| 158 |
if ( 'orderable_update_cart_item_options' !== $action || empty( $product_id ) ) { |
| 159 |
return $fragments; |
| 160 |
} |
| 161 |
|
| 162 |
$cart_item = Orderable_Helpers::is_product_in_the_cart( $product_id ); |
| 163 |
|
| 164 |
if ( empty( $cart_item ) ) { |
| 165 |
return $fragments; |
| 166 |
} |
| 167 |
|
| 168 |
ob_start(); |
| 169 |
Orderable_Products::get_quantity_roller( $cart_item ); |
| 170 |
$fragments[ ".orderable-product[data-orderable-product-id='{$product_id}'] .orderable-product__actions-button .orderable-quantity-roller" ] = ob_get_clean(); |
| 171 |
|
| 172 |
return $fragments; |
| 173 |
} |
| 174 |
} |
| 175 |
|