Sin descripción

xshop-woocommerce.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. *
  4. * Xshop woocommerce related functions
  5. *
  6. *
  7. */
  8. require get_template_directory() . '/inc/woo-items/shop-customizer.php';
  9. function xshop_woocommerce_setup() {
  10. add_theme_support( 'woocommerce' );
  11. add_theme_support( 'wc-product-gallery-zoom' );
  12. add_theme_support( 'wc-product-gallery-lightbox' );
  13. add_theme_support( 'wc-product-gallery-slider' );
  14. }
  15. add_action( 'after_setup_theme', 'xshop_woocommerce_setup' );
  16. function xshop_woocommerce_scripts() {
  17. wp_enqueue_style( 'xshop-woocommerce-style', get_template_directory_uri() . '/assets/css/xshop-woocommerce.css' , array(), XSHOP_VERSION );
  18. }
  19. add_action( 'wp_enqueue_scripts', 'xshop_woocommerce_scripts' );
  20. if ( ! function_exists( 'xshop_woocommerce_cart_link_fragment' ) ) {
  21. /**
  22. * Cart Fragments.
  23. *
  24. * Ensure cart contents update when products are added to the cart via AJAX.
  25. *
  26. * @param array $fragments Fragments to refresh via AJAX.
  27. * @return array Fragments to refresh via AJAX.
  28. */
  29. function xshop_woocommerce_cart_link_fragment( $fragments ) {
  30. ob_start();
  31. xshop_woocommerce_cart_link();
  32. $fragments['.xshoping-bag'] = ob_get_clean();
  33. return $fragments;
  34. }
  35. }
  36. add_filter( 'woocommerce_add_to_cart_fragments', 'xshop_woocommerce_cart_link_fragment' );
  37. if ( ! function_exists( 'xshop_woocommerce_cart_link' ) ) {
  38. /**
  39. * Cart Link.
  40. *
  41. * Displayed a link to the cart including the number of items present and the cart total.
  42. *
  43. * @return void
  44. */
  45. function xshop_woocommerce_cart_link() {
  46. $item_count_text = sprintf(
  47. /* translators: number of items in the mini cart. */
  48. _n( '+%d', '+%d', WC()->cart->get_cart_contents_count(), 'xshop' ),
  49. WC()->cart->get_cart_contents_count()
  50. );
  51. ?>
  52. <div class="xshoping-bag" data-bs-toggle="modal" data-bs-target="#cartModal">
  53. <div class="xshoping-inner-bag">
  54. <i class="fa fa-shopping-basket"></i>
  55. <span class="count cart-contents"><?php echo esc_html( $item_count_text ); ?></span>
  56. </div>
  57. </div>
  58. <?php
  59. }
  60. }
  61. if ( ! function_exists( 'xshop_woocommerce_header_cart' ) ) {
  62. /**
  63. * Display Header Cart.
  64. *
  65. * @return void
  66. */
  67. function xshop_woocommerce_header_cart() {
  68. if ( is_cart() || is_checkout() ) {
  69. $xshop_class = 'current-menu-item xcart-page d-none';
  70. } else {
  71. $xshop_class = 'not-cart-page';
  72. }
  73. ?>
  74. <div class="xshoping-cart <?php echo esc_attr($xshop_class); ?>">
  75. <?php xshop_woocommerce_cart_link(); ?>
  76. <!-- Modal -->
  77. <!-- <div class="modal fade" id="cartModal" tabindex="-1" role="dialog" aria-labelledby="xcartTitle" aria-hidden="true"> -->
  78. <div class="modal fade" id="cartModal" tabindex="-1" aria-labelledby="cartModalLabel" aria-hidden="true">
  79. <div class="modal-dialog" role="document">
  80. <div class="modal-content">
  81. <div class="modal-header">
  82. <h5 class="modal-title" id="xcartTitle"><?php echo esc_html__( 'Shopping Cart ','xshop' ); ?></h5>
  83. </div>
  84. <div class="modal-body">
  85. <?php
  86. $instance = array(
  87. 'title' => '',
  88. );
  89. the_widget( 'WC_Widget_Cart', $instance );
  90. ?>
  91. </div>
  92. <div class="modal-footer">
  93. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php esc_html_e( 'Close', 'xshop' ); ?>
  94. </button>
  95. </div>
  96. </div>
  97. </div>
  98. </div>
  99. </div>
  100. <?php
  101. }
  102. }
  103. function xshop_shop_widgets_init() {
  104. register_sidebar( array(
  105. 'name' => esc_html__( 'Shop Sidebar', 'xshop' ),
  106. 'id' => 'shop-sidebar',
  107. 'description' => esc_html__( 'Add shop widgets here.', 'xshop' ),
  108. 'before_widget' => '<section id="%1$s" class="widget shadow mb-4 p-3 %2$s">',
  109. 'after_widget' => '</section>',
  110. 'before_title' => '<h2 class="widget-title">',
  111. 'after_title' => '</h2>',
  112. ) );
  113. }
  114. add_action( 'widgets_init', 'xshop_shop_widgets_init' );
  115. function xshop_before_single_product_div(){
  116. echo '<div class="xshop-single-product" ?>';
  117. }
  118. add_action('woocommerce_before_single_product_summary','xshop_before_single_product_div',5);
  119. function xshop_after_single_product_div(){
  120. echo '</div>';
  121. }
  122. add_action('woocommerce_after_single_product_summary','xshop_after_single_product_div',5);
  123. function xshop_woo_body_classes( $classes ) {
  124. if ( ! is_active_sidebar( 'shop-sidebar' ) && is_shop() ) {
  125. $classes[] = 'no-shop-widget';
  126. }
  127. if ( is_front_page() && is_shop() ) {
  128. $classes[] = 'xfront-shop';
  129. }
  130. return $classes;
  131. }
  132. add_filter( 'body_class', 'xshop_woo_body_classes' );
  133. /**
  134. * Change number or products per row
  135. */
  136. add_filter('loop_shop_columns', 'xshop_loop_columns', 999);
  137. if (!function_exists('xshop_loop_columns')) {
  138. function xshop_loop_columns() {
  139. if( is_active_sidebar( 'shop-sidebar' ) ){
  140. return 3; // 3 products per row
  141. }else{
  142. return 4; // 3 products per row
  143. }
  144. }
  145. }
  146. add_filter( 'woocommerce_output_related_products_args', 'xshop_related_products_args', 20 );
  147. function xshop_related_products_args( $args ) {
  148. if( is_active_sidebar( 'shop-sidebar' ) ){
  149. $args['posts_per_page'] = 3; // 4 related products
  150. $args['columns'] = 3; // arranged in 2 columns
  151. }else{
  152. $args['posts_per_page'] = 4; // 4 related products
  153. }
  154. return $args;
  155. }