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
MenuCart.php
211 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Menu Cart. |
| 4 | * |
| 5 | * @package ShopPress |
| 6 | */ |
| 7 | |
| 8 | namespace ShopPress\Modules; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | class MenuCart { |
| 13 | /** |
| 14 | * Init. |
| 15 | * |
| 16 | * @since 1.2.0 |
| 17 | */ |
| 18 | public static function init() { |
| 19 | $menu_cart_id = sp_get_module_settings( 'menu_cart', 'cart_menu_id' )['value'] ?? false; |
| 20 | if ( ! $menu_cart_id ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | $display_as = sp_get_module_settings( 'menu_cart', 'display_cart_as' )['value'] ?? 'dropdown'; |
| 25 | $display_as_drawer = 'drawer' === $display_as ? true : false; |
| 26 | |
| 27 | add_filter( 'wp_nav_menu_items', array( __CLASS__, 'add_to_menu' ), 999, 2 ); |
| 28 | add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue' ), 99 ); |
| 29 | add_filter( 'woocommerce_add_to_cart_fragments', array( __CLASS__, 'fragment' ), 99, 1 ); |
| 30 | |
| 31 | if ( $display_as_drawer ) { |
| 32 | add_action( 'wp_footer', array( __CLASS__, 'drawer' ) ); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Enqueue scripts. |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | public static function enqueue() { |
| 42 | wp_enqueue_style( 'sp-mini-cart' ); |
| 43 | |
| 44 | if ( is_rtl() ) { |
| 45 | wp_enqueue_style( 'sp-mini-cart-rtl' ); |
| 46 | } |
| 47 | |
| 48 | wp_enqueue_script( 'sp-menu-cart' ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Return icon. |
| 53 | * |
| 54 | * @since 1.2.0 |
| 55 | * |
| 56 | * @return string|int |
| 57 | */ |
| 58 | public static function get_icon() { |
| 59 | $icon_type = sp_get_module_settings( 'menu_cart', 'icon_type', 'icons_pack' ); |
| 60 | |
| 61 | if ( 'icons_pack' === $icon_type ) { |
| 62 | $icon = sp_get_module_settings( 'menu_cart', 'icons_pack' ); |
| 63 | |
| 64 | return sp_render_icon_pack( $icon ); |
| 65 | } elseif ( 'custom_icon' === $icon_type ) { |
| 66 | return sp_get_module_settings( 'menu_cart', 'custom_icon' )['id'] ?? false; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Fragment. |
| 72 | * |
| 73 | * @since 1.0.0 |
| 74 | */ |
| 75 | public static function fragment( $fragments ) { |
| 76 | $cart_items_count = ! empty( WC()->cart ) ? WC()->cart->get_cart_contents_count() : 0; |
| 77 | $button_icon = self::get_icon(); |
| 78 | $display_as = sp_get_module_settings( 'menu_cart', 'display_cart_as' )['value'] ?? 'dropdown'; |
| 79 | $display_as_drawer = 'drawer' === $display_as ? true : false; |
| 80 | ob_start(); |
| 81 | ?> |
| 82 | <a href="#"> |
| 83 | <div class="sp-header-cart <?php echo $display_as_drawer ? 'sp-drawer-click' : ''; ?>" <?php echo $display_as_drawer ? 'data-drawer-target="sp-mc-drawer-menu_cart"' : ''; ?>> |
| 84 | <?php echo ! is_numeric( $button_icon ) ? wp_kses( $button_icon, sp_allowd_svg_tags() ) : wp_get_attachment_image( $button_icon, array( 20, 20, true ) ); ?> |
| 85 | <span class="sp-cart-items-count"><?php echo esc_html( (string) $cart_items_count ); ?></span> |
| 86 | </div> |
| 87 | </a> |
| 88 | |
| 89 | <?php |
| 90 | $count = ob_get_clean(); |
| 91 | |
| 92 | ob_start(); |
| 93 | if ( 'dropdown' === $display_as ) { |
| 94 | self::dropdown(); |
| 95 | } |
| 96 | $dropdown = ob_get_clean(); |
| 97 | |
| 98 | ob_start(); |
| 99 | woocommerce_mini_cart(); |
| 100 | $mini_cart = ob_get_clean(); |
| 101 | |
| 102 | $fragments['.sp-header-cart'] = $count; |
| 103 | $fragments['.sp-mc-dd'] = $dropdown; |
| 104 | $fragments['cart_hash'] = WC()->cart->get_cart_hash(); |
| 105 | if ( $display_as_drawer ) { |
| 106 | |
| 107 | ob_start(); |
| 108 | if ( $display_as_drawer ) { |
| 109 | self::drawer(); |
| 110 | } |
| 111 | $fragments['#sp-mc-drawer-menu_cart'] = ob_get_clean(); |
| 112 | } |
| 113 | $fragments['div.widget_shopping_cart_content'] = $mini_cart; |
| 114 | return $fragments; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Fragment. |
| 119 | * |
| 120 | * @since 1.0.0 |
| 121 | */ |
| 122 | public static function cart_content() { |
| 123 | include sp_get_template_path( 'mini-cart/style-1' ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * display cart header. |
| 128 | * |
| 129 | * @since 1.2.0 |
| 130 | */ |
| 131 | public static function display_header_cart() { |
| 132 | $cart_items_count = ! empty( WC()->cart ) ? WC()->cart->get_cart_contents_count() : 0; |
| 133 | $display_as = sp_get_module_settings( 'menu_cart', 'display_cart_as' )['value'] ?? 'dropdown'; |
| 134 | $display_as_drawer = 'drawer' === $display_as ? true : false; |
| 135 | $button_icon = self::get_icon(); |
| 136 | ?> |
| 137 | <a href="#"> |
| 138 | <div class="sp-header-cart <?php echo $display_as_drawer ? 'sp-drawer-click' : ''; ?>" <?php echo $display_as_drawer ? 'data-drawer-target="sp-mc-drawer-menu_cart"' : ''; ?>> |
| 139 | <?php echo ! is_numeric( $button_icon ) ? wp_kses( $button_icon, sp_allowd_svg_tags() ) : wp_get_attachment_image( $button_icon, array( 20, 20, true ) ); ?> |
| 140 | <span class="sp-cart-items-count"><?php echo esc_html( (string) $cart_items_count ); ?></span> |
| 141 | </div> |
| 142 | </a> |
| 143 | <?php |
| 144 | if ( 'dropdown' === $display_as ) { |
| 145 | self::dropdown(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Dropdown. |
| 151 | * |
| 152 | * @since 1.2.0 |
| 153 | */ |
| 154 | public static function drawer() { |
| 155 | |
| 156 | $drawer_position = sp_get_module_settings( 'menu_cart', 'drawer_position' )['value'] ?? 'left'; |
| 157 | $close_icon = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="17" height="17" viewBox="0 0 17 17"> <g> </g> <path d="M9.207 8.5l6.646 6.646-0.707 0.707-6.646-6.646-6.646 6.646-0.707-0.707 6.646-6.646-6.647-6.646 0.707-0.707 6.647 6.646 6.646-6.646 0.707 0.707-6.646 6.646z" fill="#000000" /> </svg>'; |
| 158 | ?> |
| 159 | <div id="sp-mc-drawer-menu_cart" class="sp-drawer sp-mini-cart-drawer <?php echo esc_attr( $drawer_position ? 'float-' . $drawer_position : 'float-left' ); ?>"> |
| 160 | <div class="sp-drawer-content-wrap"> |
| 161 | <div class="sp-drawer-close"> |
| 162 | <?php echo wp_kses( $close_icon, sp_allowd_svg_tags() ); ?> |
| 163 | </div> |
| 164 | |
| 165 | <?php self::cart_content(); ?> |
| 166 | </div> |
| 167 | </div> |
| 168 | <?php |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Dropdown. |
| 173 | * |
| 174 | * @since 1.0.0 |
| 175 | */ |
| 176 | public static function dropdown() { |
| 177 | ?> |
| 178 | <div class="sp-mc-dd" id="sp-mc-dd-menu_cart"> |
| 179 | <?php self::cart_content(); ?> |
| 180 | </div> |
| 181 | <?php |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Add to menu |
| 186 | * |
| 187 | * @param array $items |
| 188 | * @param object $args |
| 189 | * |
| 190 | * @since 1.0.0 |
| 191 | * |
| 192 | * @return string |
| 193 | */ |
| 194 | public static function add_to_menu( $items, $args ) { |
| 195 | $menu_cart_id = sp_get_module_settings( 'menu_cart', 'cart_menu_id' )['value'] ?? false; |
| 196 | if ( isset( $args->menu ) ) { |
| 197 | $menu_obj = wp_get_nav_menu_object ($args->menu); |
| 198 | $menu_id = is_object( $args->menu ) ? $args->menu->term_id : $args->menu; |
| 199 | if ( $menu_id == $menu_cart_id || $menu_obj->slug == $menu_id) { |
| 200 | ob_start(); |
| 201 | echo '<li class="sp-mc-item menu-item sp-open-dropdown">'; |
| 202 | self::display_header_cart(); |
| 203 | echo '</li>'; |
| 204 | $items .= ob_get_clean(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return $items; |
| 209 | } |
| 210 | } |
| 211 |