VariationSwatches
1 month ago
Wishlist
1 month ago
AdminMessage.php
1 month ago
AjaxSearch.php
1 month ago
Backorder.php
1 month ago
CatalogMode.php
2 years ago
Compare.php
1 month ago
DefaultTemplates.php
2 years ago
FlashSalesCountdown.php
1 year ago
MenuCart.php
5 months ago
MobilePanel.php
1 month ago
MultiStep.php
1 year ago
Notifications.php
1 month ago
QuickView.php
1 month ago
RecentlyViewedProducts.php
1 year ago
RenameLabel.php
5 months ago
Shopify.php
1 year ago
SingleAjaxAddToCart.php
1 month ago
SizeChart.php
1 month ago
StickyAddToCart.php
1 month ago
StickyAddToCart.php
140 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Sticky Add To Cart. |
| 4 | * |
| 5 | * @package ShopPress |
| 6 | */ |
| 7 | |
| 8 | namespace ShopPress\Modules; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use ShopPress\Modules\CatalogMode; |
| 13 | |
| 14 | class StickyAddToCart { |
| 15 | /** |
| 16 | * Init. |
| 17 | * |
| 18 | * @since 1.2.0 |
| 19 | */ |
| 20 | public static function init() { |
| 21 | |
| 22 | if ( CatalogMode::is_catalog_mode() ) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | add_action( 'wp_footer', array( __CLASS__, 'sticky_add_to_cart_template' ), 99 ); |
| 27 | add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue' ) ); |
| 28 | add_action( 'body_class', array( __CLASS__, 'body_class' ) ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Check if sticky add to cart is enabled. |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | * |
| 36 | * @return bool |
| 37 | */ |
| 38 | public static function is_sticky_add_to_cart() { |
| 39 | |
| 40 | if ( ! empty( sp_get_module_settings( 'sticky_add_to_cart', 'status' ) ) ) { |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * enqueue style. |
| 49 | * |
| 50 | * @since 1.0.0 |
| 51 | */ |
| 52 | public static function enqueue() { |
| 53 | |
| 54 | if ( is_product() && self::is_sticky_add_to_cart() ) { |
| 55 | wp_enqueue_style( 'sp-sticky-add-to-cart' ); |
| 56 | |
| 57 | if ( is_rtl() ) { |
| 58 | wp_enqueue_style( 'sp-sticky-add-to-cart-rtl' ); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Sticky add to cart Template. |
| 65 | * |
| 66 | * @since 1.0.0 |
| 67 | */ |
| 68 | public static function sticky_add_to_cart_template() { |
| 69 | |
| 70 | if ( ! sp_get_module_settings( 'sticky_add_to_cart', 'status' ) || ! is_product() ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $hide_on_mobile = sp_get_module_settings( 'sticky_add_to_cart', 'hide_on_mobile' ); |
| 75 | global $shoppress_sticky_add_to_cart; |
| 76 | $shoppress_sticky_add_to_cart = true; |
| 77 | global $product; |
| 78 | |
| 79 | ?> |
| 80 | <div class="sp-sticky-add-to-cart <?php echo $hide_on_mobile ? 'sp-hide-on-mobile' : ''; ?>"> |
| 81 | <div class="sp-left-content-wrapper"> |
| 82 | <div class="sticky-add-to-cart-image"> |
| 83 | <?php echo wp_kses_post( woocommerce_get_product_thumbnail() ); ?> |
| 84 | </div> |
| 85 | <div class="sp-title-price-wrapper"> |
| 86 | <div class="sticky-add-to-cart-title"> |
| 87 | <?php echo esc_html( $product->get_title() ); ?> |
| 88 | </div> |
| 89 | <div class="sticky-add-to-cart-price"> |
| 90 | <?php echo wp_kses_post( $product->get_price_html() ); ?> |
| 91 | </div> |
| 92 | </div> |
| 93 | </div> |
| 94 | <div class="sticky-add-to-cart-btn product"> |
| 95 | <?php |
| 96 | if ( $product->is_type( 'simple' ) ) { |
| 97 | woocommerce_simple_add_to_cart(); |
| 98 | } else { |
| 99 | echo '<script> |
| 100 | jQuery(document).ready(function($){ |
| 101 | |
| 102 | $(".sp-sticky-scroll-add-to-cart").on("click",function(e){ |
| 103 | e.preventDefault(); |
| 104 | jQuery([document.documentElement, document.body]).animate({ |
| 105 | scrollTop: $(".single_add_to_cart_button").closest("form").offset().top |
| 106 | }, 1000); |
| 107 | return false; |
| 108 | }); |
| 109 | }); |
| 110 | </script>'; |
| 111 | echo '<a href="' . esc_url( $product->add_to_cart_url() ) . '" class="single_add_to_cart_button sp-sticky-scroll-add-to-cart button alt">' . ( true == $product->is_type( 'variable' ) || true == $product->is_type( 'grouped' ) ? esc_html__( 'Select Options', 'shop-press' ) : esc_html( $product->single_add_to_cart_text() ) ) . '</a>'; |
| 112 | } |
| 113 | ?> |
| 114 | </div> |
| 115 | </div> |
| 116 | <?php |
| 117 | |
| 118 | $shoppress_sticky_add_to_cart = false; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Sticky add to cart Body class. |
| 123 | * |
| 124 | * @since 1.0.0 |
| 125 | */ |
| 126 | public static function body_class( $classes ) { |
| 127 | |
| 128 | $hide_on_mobile = sp_get_module_settings( 'sticky_add_to_cart', 'hide_on_mobile' ); |
| 129 | |
| 130 | if ( is_product() && $hide_on_mobile ) { |
| 131 | return array_merge( $classes, array( 'sp-active-sticky-add-to-cart', 'sp-hide-sticky-add-to-cart' ) ); |
| 132 | |
| 133 | } elseif ( is_product() && ! $hide_on_mobile ) { |
| 134 | return array_merge( $classes, array( 'sp-active-sticky-add-to-cart' ) ); |
| 135 | } |
| 136 | |
| 137 | return $classes; |
| 138 | } |
| 139 | } |
| 140 |