VariationSwatches
1 week ago
Wishlist
1 week ago
AdminMessage.php
1 week ago
AjaxSearch.php
1 week ago
Backorder.php
1 week ago
CatalogMode.php
1 year ago
Compare.php
1 week ago
DefaultTemplates.php
2 years ago
FlashSalesCountdown.php
1 year ago
MenuCart.php
4 months ago
MobilePanel.php
1 week ago
MultiStep.php
1 year ago
Notifications.php
1 week ago
QuickView.php
1 week ago
RecentlyViewedProducts.php
1 year ago
RenameLabel.php
4 months ago
Shopify.php
1 year ago
SingleAjaxAddToCart.php
1 week ago
SizeChart.php
1 week ago
StickyAddToCart.php
1 week ago
CatalogMode.php
307 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Catalog Mode. |
| 4 | * |
| 5 | * @package ShopPress |
| 6 | */ |
| 7 | |
| 8 | namespace ShopPress\Modules; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | class CatalogMode { |
| 13 | /** |
| 14 | * Init. |
| 15 | * |
| 16 | * @since 1.2.0 |
| 17 | */ |
| 18 | public static function init() { |
| 19 | |
| 20 | if ( ! self::is_catalog_mode() ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | $hide_add_to_cart = sp_get_module_settings( 'catalog_mode', 'hide_add_to_cart', false ); |
| 25 | $change_text_and_link = sp_get_module_settings( 'catalog_mode', 'change_text_and_link', false ); |
| 26 | |
| 27 | if ( $hide_add_to_cart ) { |
| 28 | add_action( 'woocommerce_init', array( __CLASS__, 'add_to_cart_button' ), 99 ); |
| 29 | } |
| 30 | |
| 31 | if ( $change_text_and_link ) { |
| 32 | add_filter( 'woocommerce_product_add_to_cart_text', array( __CLASS__, 'custom_add_to_cart_text' ), 10, 2 ); |
| 33 | add_filter( 'woocommerce_product_single_add_to_cart_text', array( __CLASS__, 'custom_add_to_cart_text' ), 10, 2 ); |
| 34 | add_filter( 'woocommerce_product_add_to_cart_url', array( __CLASS__, 'custom_add_to_cart_link' ), 10, 2 ); |
| 35 | add_filter( 'woocommerce_add_to_cart_redirect', array( __CLASS__, 'custom_add_to_cart_link' ), 10, 2 ); |
| 36 | add_filter( 'woocommerce_is_sold_individually', array( __CLASS__, 'remove_quantity_fields' ), 10, 2 ); |
| 37 | } |
| 38 | |
| 39 | add_filter( 'woocommerce_get_price_html', array( __CLASS__, 'custom_price_html' ), 100, 2 ); |
| 40 | add_filter( 'woocommerce_product_tabs', array( __CLASS__, 'remove_product_tabs' ), 98 ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Check if catalog mode is enabled. |
| 45 | * |
| 46 | * @since 1.0.0 |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | public static function is_catalog_mode() { |
| 51 | |
| 52 | if ( ! empty( sp_get_module_settings( 'catalog_mode', 'status' ) ) ) { |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Add to cart button catalog mode. |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | */ |
| 64 | public static function add_to_cart_button() { |
| 65 | |
| 66 | if ( self::is_available_catalog_mode() === false ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); |
| 71 | remove_action( 'woocommerce_loop_add_to_cart_link', 'woocommerce_template_loop_add_to_cart', 10 ); |
| 72 | remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); |
| 73 | remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 ); |
| 74 | remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 ); |
| 75 | remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 ); |
| 76 | remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 ); |
| 77 | add_filter( 'woocommerce_loop_add_to_cart_link', '__return_empty_string', 10 ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Remove product tabs catalog mode. |
| 82 | * |
| 83 | * @param array $tabs |
| 84 | * |
| 85 | * @since 1.4.0 |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | public static function remove_product_tabs( $tabs ) { |
| 90 | $hide_review_tab = sp_get_module_settings( 'catalog_mode', 'hide_review_tab', false ); |
| 91 | |
| 92 | if ( $hide_review_tab && self::is_available_catalog_mode() === true ) { |
| 93 | unset( $tabs['reviews'] ); |
| 94 | } |
| 95 | |
| 96 | return $tabs; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add to cart button catalog mode. |
| 101 | * |
| 102 | * @param string $price |
| 103 | * |
| 104 | * @since 1.4.0 |
| 105 | * |
| 106 | * @return string |
| 107 | */ |
| 108 | public static function custom_price_html( $price ) { |
| 109 | $hide_price = sp_get_module_settings( 'catalog_mode', 'hide_price', false ); |
| 110 | $price_placeholder = sp_get_module_settings( 'catalog_mode', 'price_placeholder', '' ); |
| 111 | if ( $hide_price && self::is_available_catalog_mode() === true ) { |
| 112 | return $price_placeholder; |
| 113 | } |
| 114 | |
| 115 | return $price; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Custom add to cart text catalog mode. |
| 120 | * |
| 121 | * @param string $add_to_cart_text |
| 122 | * |
| 123 | * @since 1.4.0 |
| 124 | * |
| 125 | * @return string |
| 126 | */ |
| 127 | public static function custom_add_to_cart_text( $add_to_cart_text ) { |
| 128 | |
| 129 | if ( self::is_available_catalog_mode() === false ) { |
| 130 | return $add_to_cart_text; |
| 131 | } |
| 132 | |
| 133 | $custom_text = sp_get_module_settings( 'catalog_mode', 'custom_text' ); |
| 134 | |
| 135 | return empty( ! $custom_text ) ? $custom_text : $add_to_cart_text; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Custom add to cart link catalog mode. |
| 140 | * |
| 141 | * @param string $url |
| 142 | * |
| 143 | * @since 1.4.0 |
| 144 | * |
| 145 | * @return string |
| 146 | */ |
| 147 | public static function custom_add_to_cart_link( $url ) { |
| 148 | |
| 149 | if ( self::is_available_catalog_mode() === false ) { |
| 150 | return $url; |
| 151 | } |
| 152 | |
| 153 | $custom_link = sp_get_module_settings( 'catalog_mode', 'custom_link' ) ?? $url; |
| 154 | |
| 155 | return $custom_link; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Remove quantity fields catalog mode. |
| 160 | * |
| 161 | * @since 1.4.0 |
| 162 | * |
| 163 | * @return bool |
| 164 | */ |
| 165 | public static function remove_quantity_fields() { |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Return prepared rules |
| 171 | * |
| 172 | * @param array $conditions |
| 173 | * |
| 174 | * @since 1.4.0 |
| 175 | * |
| 176 | * @return array |
| 177 | */ |
| 178 | public static function prepare_rules( $conditions ) { |
| 179 | |
| 180 | foreach ( $conditions as $c_key => $rules ) { |
| 181 | |
| 182 | foreach ( $rules as $k => $rule ) { |
| 183 | |
| 184 | $rule['condition'] = $rule['condition']['value'] ?? false; |
| 185 | $rule['operator'] = $rule['operator']['value'] ?? false; |
| 186 | |
| 187 | if ( isset( $rule['value']['min'] ) ) { |
| 188 | |
| 189 | $rule['value'] = $rule['value'] ?? array( |
| 190 | 'min' => 0, |
| 191 | 'max' => 5, |
| 192 | ); |
| 193 | } elseif ( is_array( $rule['value'] ) && isset( current( $rule['value'] )['value'] ) ) { |
| 194 | |
| 195 | $rule['value'] = array_column( (array) ( $rule['value'] ?? array() ), 'value' ); |
| 196 | } |
| 197 | |
| 198 | $rules[ $k ] = $rule; |
| 199 | } |
| 200 | |
| 201 | $conditions[ $c_key ] = $rules; |
| 202 | } |
| 203 | |
| 204 | return $conditions; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Is available catalog mode. |
| 209 | * |
| 210 | * @since 1.4.0 |
| 211 | * |
| 212 | * @return bool |
| 213 | */ |
| 214 | public static function is_available_catalog_mode() { |
| 215 | // TODO: add rules option later! |
| 216 | return true; |
| 217 | |
| 218 | $rules = sp_get_module_settings( 'catalog_mode', 'rules', array() ); |
| 219 | if ( empty( $rules ) ) { |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | $rules = self::prepare_rules( $rules ); |
| 224 | $user_id = get_current_user_id(); |
| 225 | $user_roles = $user_id ? (array) wp_get_current_user()->roles : array(); |
| 226 | |
| 227 | global $product; |
| 228 | |
| 229 | if ( ! is_a( $product, '\WC_Product' ) ) { |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | $product_id = $product->get_id(); |
| 234 | $product_category_ids = $product->get_category_ids(); |
| 235 | |
| 236 | $is_available = false; |
| 237 | |
| 238 | foreach ( $rules as $group_id => $group_data ) { |
| 239 | |
| 240 | $rule_group_validation = true; |
| 241 | foreach ( $group_data as $rule_id => $rule ) { |
| 242 | $type = $rule['condition'] ?? false; |
| 243 | $operator = $rule['operator'] ?? false; |
| 244 | $value = $rule['value'] ?? false; |
| 245 | if ( ! $type || ! $operator || empty( $value ) ) { |
| 246 | continue; |
| 247 | } |
| 248 | |
| 249 | $validation = false; |
| 250 | switch ( $type ) { |
| 251 | case 'product_categories': |
| 252 | case 'products': |
| 253 | if ( 'product_categories' == $type ) { |
| 254 | $values = $product_category_ids; |
| 255 | } elseif ( 'products' == $type ) { |
| 256 | $values = array( $product_id ); |
| 257 | } |
| 258 | |
| 259 | $intersect = array_intersect( (array) $values, (array) $value ); |
| 260 | if ( 'all' === $operator ) { |
| 261 | $validation = true; |
| 262 | } elseif ( 'include' == $operator ) { |
| 263 | $validation = ! empty( $intersect ); |
| 264 | } else { |
| 265 | $validation = empty( $intersect ); |
| 266 | } |
| 267 | break; |
| 268 | case 'user_role': |
| 269 | $intersect = array_intersect( (array) $user_roles, (array) $value ); |
| 270 | if ( 'all' === $operator ) { |
| 271 | $validation = true; |
| 272 | } elseif ( 'include' == $operator ) { |
| 273 | $validation = ! empty( $intersect ); |
| 274 | } else { |
| 275 | $validation = empty( $intersect ); |
| 276 | } |
| 277 | break; |
| 278 | case 'login_status': |
| 279 | if ( |
| 280 | ( 'logged_in' == $value['value'] && $user_id ) |
| 281 | || |
| 282 | ( 'not_logged_in' == $value['value'] && ! $user_id ) |
| 283 | ) { |
| 284 | $validation = true; |
| 285 | } else { |
| 286 | $validation = false; |
| 287 | } |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | if ( ! $validation ) { |
| 292 | |
| 293 | $rule_group_validation = false; |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if ( $rule_group_validation ) { |
| 299 | $is_available = true; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return $is_available; |
| 305 | } |
| 306 | } |
| 307 |