Advertisement
cyberdev

Untitled

Aug 17th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.01 KB | None | 0 0
  1. <?php
  2. /**
  3. * WooCommerce plugin functions
  4. *
  5. * @license For the full license information, please view the Licensing folder
  6. * that was distributed with this source code.
  7. *
  8. * @package Bimber_Theme
  9. */
  10.  
  11. // Prevent direct script access.
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. die( 'No direct script access allowed' );
  14. }
  15.  
  16.  
  17. add_action( 'after_setup_theme', 'bimber_woocommerce_support' );
  18.  
  19. // Disable all stylesheets.
  20. add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
  21.  
  22. add_filter( 'bimber_setup_sidebars', 'bimber_woocommerce_setup_sidebars' );
  23. add_filter( 'bimber_sidebar', 'bimber_woocommerce_sidebar' );
  24. add_filter( 'bimber_show_mini_cart', 'bimber_woocommerce_show_mini_cart' );
  25.  
  26. // Before and after the loop.
  27. add_action( 'woocommerce_before_shop_loop', 'bimber_woocommerce_before_shop_loop', 999 );
  28. add_action( 'woocommerce_after_shop_loop', 'bimber_woocommerce_after_shop_loop', 999 );
  29.  
  30. // Adjust span.count markup.
  31. add_filter( 'woocommerce_layered_nav_count', 'bimber_woocommerce_layered_nav_count', 10, 2 );
  32. add_filter( 'woocommerce_product_reviews_tab_title', 'bimber_woocommerce_product_reviews_tab_title' );
  33.  
  34. // Display 12 products per page.
  35. add_filter( 'loop_shop_per_page', 'bimber_wc_loop_shop_per_page', 20 );
  36.  
  37. // Override theme default specification for product # per row.
  38. add_filter('loop_shop_columns', 'bimber_wc_loop_shop_columns', 999);
  39.  
  40. // Adjust the HTML markup of the add_to_cart button.
  41. add_filter( 'woocommerce_loop_add_to_cart_args', 'bimber_woocommerce_loop_add_to_cart_args', 10, 2 );
  42. // Adjust the HTML markup of the order button.
  43. add_filter( 'woocommerce_order_button_html', 'bimber_woocommerce_order_button_html' );
  44.  
  45. // Breadcrumbs.
  46. remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
  47. add_action( 'woocommerce_before_single_product', 'bimber_wc_breadcrumbs', 9 );
  48.  
  49. add_filter( 'wp', 'bimber_wc_comments_template', 99 );
  50.  
  51. // Reconfigure related products.
  52. add_filter( 'woocommerce_output_related_products_args', 'bimber_woocommerce_output_related_products_args' );
  53. // Reconfigure upsell products.
  54. add_filter( 'woocommerce_upsell_display_args', 'bimber_woocommerce_upsell_display_args' );
  55.  
  56.  
  57. add_filter( 'bimber_skip_quizzes_for_query', 'bimber_wp_skip_quizzes', 10, 2 );
  58.  
  59. add_action( 'wp_enqueue_scripts', 'bimber_woocommerce_maybe_enqueue_page_styles', 99 );
  60.  
  61. remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );
  62.  
  63. add_filter( 'manage_product_posts_columns', 'bimber_wc_product_list_custom_columns' );
  64.  
  65. add_filter( 'theme_page_templates', 'store_shop_page_templates', 9, 3 );
  66. add_filter( 'theme_page_templates', 'shop_shop_page_templates', 11, 3 );
  67.  
  68. remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
  69. add_action( 'woocommerce_cart_is_empty', 'bimber_wc_empty_cart_message', 10 );
  70.  
  71. /**
  72. * Declare WooCommerce support
  73. */
  74. function bimber_woocommerce_support() {
  75. add_theme_support( 'woocommerce' );
  76.  
  77. add_theme_support( 'wc-product-gallery-zoom' );
  78. add_theme_support( 'wc-product-gallery-lightbox' );
  79. add_theme_support( 'wc-product-gallery-slider' );
  80. }
  81.  
  82. /**
  83. * Render the opening markup of the content theme area
  84. */
  85. function bimber_woocommerce_content_wrapper_start() {
  86. ?>
  87. <div class="g1-row g1-row-layout-page g1-row-padding-m">
  88. <div class="g1-row-inner">
  89. <div class="g1-column g1-column-2of3">
  90.  
  91. <?php
  92. }
  93.  
  94. /**
  95. * Render the closing markup of the content theme area.
  96. */
  97. function bimber_woocommerce_content_wrapper_end() {
  98. ?>
  99. </div><!-- .g1-column -->
  100. <?php
  101. }
  102.  
  103. /**
  104. * Render the closing markup of the content theme area.
  105. */
  106. function bimber_woocommerce_sidebar_wrapper_end() {
  107. ?>
  108. </div>
  109. </div><!-- .g1-row -->
  110. <?php
  111. }
  112.  
  113. /**
  114. * Reigster WooCommerce specific sidebars
  115. *
  116. * @param array $sidebars Registered sidebars.
  117. *
  118. * @return array
  119. */
  120. function bimber_woocommerce_setup_sidebars( $sidebars ) {
  121. $sidebars['bimber_woocommerce'] = array(
  122. 'label' => esc_html__( 'WooCommerce', 'bimber' ),
  123. 'description' => esc_html__( 'Leave empty to use the Primary sidebar', 'bimber' ),
  124. );
  125.  
  126. $sidebars['bimber_woocommerce_single_product'] = array(
  127. 'label' => esc_html__( 'WooCommerce Single Product', 'bimber' ),
  128. 'description' => esc_html__( 'Leave empty to use the WooCommerce sidebar', 'bimber' ),
  129. );
  130.  
  131. return $sidebars;
  132. }
  133.  
  134. /**
  135. * Load WooCommerce specific sidebar
  136. *
  137. * @param string $sidebar Sidebar set.
  138. *
  139. * @return string
  140. */
  141. function bimber_woocommerce_sidebar( $sidebar ) {
  142. if ( is_woocommerce() || is_checkout() || is_cart() ) {
  143. $sidebar = 'bimber_woocommerce';
  144. }
  145.  
  146. // If empty, use WooCommerce sidebar as a fallback.
  147. if ( is_product() && is_active_sidebar( 'bimber_woocommerce_single_product' ) ) {
  148. $sidebar = 'bimber_woocommerce_single_product';
  149. }
  150.  
  151. return $sidebar;
  152. }
  153.  
  154. /**
  155. * Check whether to show mini card icon in the navbar
  156. *
  157. * @param bool $show
  158. *
  159. * @return bool
  160. */
  161. function bimber_woocommerce_show_mini_cart( $show ) {
  162. $visibility = bimber_get_theme_option( 'woocommerce', 'cart_visibility' );
  163.  
  164. if ( 'none' === $visibility ) {
  165. return false;
  166. }
  167.  
  168. if ( 'on_woocommerce' === $visibility ) {
  169. $show = is_woocommerce();
  170. }
  171.  
  172. if ( is_cart() || is_checkout() ) {
  173. $show = false;
  174. }
  175.  
  176. return $show;
  177. }
  178.  
  179.  
  180.  
  181. function bimber_woocommerce_maybe_enqueue_page_styles() {
  182. if ( is_shop() ) {
  183. bimber_enqueue_page_styles( wc_get_page_id('shop') );
  184. }
  185. }
  186.  
  187.  
  188.  
  189. /**
  190. * Show the product title in the product loop. By default this is an H3.
  191. */
  192. function woocommerce_template_loop_product_title() {
  193. echo '<h3 class="g1-delta g1-delta-1st">' . get_the_title() . '</h3>';
  194. }
  195.  
  196.  
  197.  
  198. function bimber_wc_loop_shop_per_page( $v ) {
  199. $v = 12;
  200.  
  201. return $v;
  202. }
  203.  
  204. function bimber_wc_loop_shop_columns( $columns ) {
  205. $columns = bimber_woocommerce_page_has_sidebar() ? 3 : 4;
  206.  
  207. return $columns;
  208. }
  209.  
  210.  
  211. function bimber_woocommerce_layered_nav_count( $count, $term ) {
  212. $count = str_replace(
  213. array( '<span class="count">(', ')</span>'),
  214. array( '<span class="count">', '</span>'),
  215. $count
  216. );
  217.  
  218. return $count;
  219. }
  220.  
  221. function bimber_woocommerce_product_reviews_tab_title( $title ) {
  222. $title = str_replace(
  223. array( '(', ')' ),
  224. array( '<span class="count">', '</span>' ),
  225. $title
  226. );
  227.  
  228. return $title;
  229. }
  230.  
  231. /**
  232. * Replace CSS classes of the add_to_cart button.
  233. *
  234. * @param array $args Arguments.
  235. * @param $product
  236. *
  237. * @return array
  238. */
  239. function bimber_woocommerce_loop_add_to_cart_args( $args, $product ) {
  240. $args['class'] = preg_replace( '/^button /', 'g1-button g1-button-s g1-button-simple ' , $args['class'] );
  241.  
  242. return $args;
  243. }
  244.  
  245.  
  246. function bimber_woocommerce_breadcrumb_defaults( $args ) {
  247. $new_args = array(
  248. 'delimiter' => '',
  249. 'wrap_before' => '<nav class="g1-breadcrumbs g1-meta" itemprop="breadcrumb"><ol>',
  250. 'before' => '<li class="g1-breadcrumbs-item">',
  251. 'after' => '</li>',
  252. 'wrap_after' => '</ol></nav>',
  253. );
  254.  
  255. $args = array_merge( $args, $new_args );
  256.  
  257. return $args;
  258. }
  259.  
  260. function bimber_wc_breadcrumbs() {
  261. if ( bimber_show_breadcrumbs() ) {
  262. bimber_render_breadcrumbs();
  263. }
  264. }
  265.  
  266. /**
  267. * Disable all hooks that overrides native WP comments
  268. */
  269. function bimber_wc_comments_template() {
  270. if ( is_woocommerce() ) {
  271. // Disable Disqus.
  272. remove_filter( 'comments_template', 'dsq_comments_template' );
  273. }
  274. }
  275.  
  276. /**
  277. * Prevent quizzes injection on WC pages
  278. *
  279. * @param bool $skip True if quizzes shouldn't be included for the $query.
  280. * @param WP_Query $query Current query object.
  281. *
  282. * @return bool
  283. */
  284. function bimber_wp_skip_quizzes( $skip, $query ) {
  285. if ( $query->is_main_query() && ( is_shop() || is_product_taxonomy() ) ) {
  286. $skip = true;
  287. }
  288.  
  289. return $skip;
  290. }
  291.  
  292. /**
  293. * Change the number of maximum related products to display.
  294. *
  295. * @param array $args Arguments.
  296. *
  297. * @return array
  298. */
  299. function bimber_woocommerce_output_related_products_args( $args ) {
  300. $args['posts_per_page'] = 4;
  301. $args['columns'] = 4;
  302.  
  303. return $args;
  304. }
  305.  
  306.  
  307. /**
  308. * Change the number of maximum upsell products to display.
  309. *
  310. * @param array $args Arguments.
  311. *
  312. * @return array
  313. */
  314. function bimber_woocommerce_upsell_display_args( $args ) {
  315. $args['posts_per_page'] = 4;
  316. $args['columns'] = 4;
  317.  
  318. return $args;
  319. }
  320.  
  321.  
  322. /**
  323. * Adjust the HTML markup of the order button.
  324. *
  325. * @param string $html HTML markup.
  326. *
  327. * @return string
  328. */
  329. function bimber_woocommerce_order_button_html( $html ) {
  330. $html = str_replace( 'class="button', 'class="g1-button g1-button-xl g1-button-solid button', $html );
  331.  
  332. return $html;
  333. }
  334.  
  335.  
  336. if ( ! function_exists( 'woocommerce_template_loop_category_title' ) ) {
  337.  
  338. /**
  339. * Show the subcategory title in the product loop.
  340. */
  341. function woocommerce_template_loop_category_title( $category ) {
  342. ?>
  343. <h3 class="g1-delta g1-delta-2nd">
  344. <?php
  345. echo $category->name;
  346.  
  347. if ( $category->count > 0 )
  348. echo apply_filters( 'woocommerce_subcategory_count_html', ' <div class="product-category-count"><mark class="count">' . sprintf( _n( '%d product', '%d products', $category->count ), $category->count ) . '</mark></div>', $category );
  349. ?>
  350. </h3>
  351. <?php
  352. }
  353. }
  354.  
  355. if ( ! function_exists( 'woocommerce_product_archive_description' ) ) {
  356.  
  357. /**
  358. * Show a shop page description on product archives.
  359. *
  360. * @subpackage Archives
  361. */
  362. function woocommerce_product_archive_description() {
  363. if ( is_post_type_archive( 'product' ) && 0 === absint( get_query_var( 'paged' ) ) ) {
  364. $shop_page = get_post( wc_get_page_id( 'shop' ) );
  365.  
  366. if ( $shop_page ) {
  367. $description = wc_format_content( $shop_page->post_content );
  368. if ( $description ) {
  369. echo '<div class="g1-row g1-row-layout-page">';
  370. echo '<div class="g1-row-inner">';
  371. echo '<div class="g1-column">';
  372. echo $description;
  373. echo '</div>';
  374. echo '</div>';
  375. echo '</div>';
  376. }
  377. }
  378. }
  379. }
  380. }
  381.  
  382. function bimber_woocommerce_page_has_sidebar() {
  383. $result = true;
  384.  
  385. $shop_page_id = wc_get_page_id( 'shop' );
  386. $template = get_post_meta( $shop_page_id, '_wp_page_template', true );
  387.  
  388. if ( 'g1-template-page-full.php' === $template ) {
  389. $result = false;
  390. }
  391.  
  392. return $result;
  393. }
  394.  
  395.  
  396. function bimber_woocommerce_before_shop_loop() {
  397. if ( is_shop() ) {
  398. if ( bimber_woocommerce_page_has_sidebar() ) {
  399. echo '<div class="woocommerce columns-3">';
  400. } else {
  401. echo '<div class="woocommerce columns-4">';
  402. }
  403. }
  404. }
  405.  
  406.  
  407. function bimber_woocommerce_after_shop_loop() {
  408. if ( is_shop() ) {
  409. echo '</div>';
  410. }
  411. }
  412.  
  413. /**
  414. * Render WC categories
  415. *
  416. * @param array $args
  417. * @return void
  418. */
  419. function bimber_render_product_categories( $args = array() ) {
  420. $out_escaped = '';
  421.  
  422. $defaults = array(
  423. 'before' => '<p class="entry-categories %s"><span class="entry-categories-inner">',
  424. 'after' => '</span></p>',
  425. 'class' => '',
  426. 'use_microdata' => false,
  427. );
  428.  
  429. $args = wp_parse_args( $args, $defaults );
  430.  
  431. // Sanitize HTML classes.
  432. $args['class'] = explode( ' ', $args['class'] );
  433. $args['class'] = implode( ' ', array_map( 'sanitize_html_class', $args['class'] ) );
  434.  
  435. $args['before'] = sprintf( $args['before'], $args['class'] );
  436.  
  437. $term_list = get_the_terms( get_the_ID(), 'product_cat' );
  438.  
  439. if ( is_array( $term_list ) ) {
  440. $out_escaped .= $args['before'];
  441.  
  442. foreach ( $term_list as $term ) {
  443. if ( $args['use_microdata'] ) {
  444. $out_escaped .= sprintf(
  445. '<a href="%s" class="entry-category %s"><span itemprop="articleSection">%s</span></a>, ',
  446. esc_url( get_term_link( $term->slug, 'product_cat' ) ),
  447. sanitize_html_class( 'entry-category-item-' . $term->term_taxonomy_id ),
  448. wp_kses_post( $term->name )
  449. );
  450. } else {
  451. $out_escaped .= sprintf(
  452. '<a href="%s" class="entry-category %s">%s</a>, ',
  453. esc_url( get_term_link( $term->slug, 'product_cat' ) ),
  454. sanitize_html_class( 'entry-category-item-' . $term->term_taxonomy_id ),
  455. wp_kses_post( $term->name )
  456. );
  457. }
  458. }
  459.  
  460. $out_escaped .= $args['after'];
  461. }
  462.  
  463. echo $out_escaped;
  464. }
  465.  
  466. /**
  467. * Add product post type to search results archive page
  468. *
  469. * @param WP_Query $query WP Query object.
  470. */
  471. function bimber_woocommerce_add_products_to_search_results( $query ) {
  472. $is_bbpress = false;
  473. if ( function_exists( 'is_bbpress' ) ) {
  474. $is_bbpress = is_bbpress();
  475. }
  476. if ( $query->is_search() && ! $is_bbpress ) {
  477. $post_type = $query->get( 'post_type' );
  478. $post_type = ( '' === $post_type ) ? array( 'post' ) : (array) $post_type;
  479. $post_type[] = 'product';
  480. $query->set( 'post_type', $post_type );
  481. }
  482. }
  483.  
  484. function bimber_woocommerce_set_up_shop_page() {
  485. $page = get_page_by_title( 'Shop' );
  486.  
  487. if ( $page ) {
  488. update_option( 'woocommerce_shop_page_id', $page->ID );
  489. }
  490. }
  491.  
  492. /**
  493. * Return injected product query.
  494. *
  495. * @return WP_Query
  496. */
  497. function bimber_wc_get_injected_product_query() {
  498. $product_category = is_home() ? bimber_get_theme_option( 'home', 'product_category' ) : bimber_get_theme_option( 'archive', 'product_category' );
  499.  
  500. if ( is_array( $product_category ) ) {
  501. $product_category = implode( ',', $product_category );
  502. }
  503.  
  504. // Query: count products.
  505. // ---------------------
  506.  
  507. $args = array(
  508. 'post_type' => 'product',
  509. 'posts_per_page' => -1,
  510. );
  511.  
  512. if ( ! empty( $product_category ) ) {
  513. $args['product_cat'] = $product_category;
  514. }
  515.  
  516. $count_query = new WP_Query( $args );
  517.  
  518. $products_count = $count_query->found_posts;
  519.  
  520. // Query: Get single product.
  521. // -------------------------
  522.  
  523. $args['posts_per_page'] = 1;
  524.  
  525. $ordering = WC()->query->get_catalog_ordering_args();
  526.  
  527. $args['orderby'] = $ordering['orderby'];
  528. $args['order'] = $ordering['order'];
  529.  
  530. if ( isset( $ordering['meta_key'] ) ) {
  531. $args['meta_key'] = $ordering['meta_key'];
  532. }
  533.  
  534. if ( $products_count > 1 ) {
  535. global $bimber_product_offset;
  536.  
  537. $offset = absint( $bimber_product_offset );
  538.  
  539. $args['offset'] = $offset % $products_count;
  540. }
  541.  
  542. return new WP_Query( $args );
  543. }
  544.  
  545. /**
  546. * Manage product list columns
  547. *
  548. * @param array $columns List of columns.
  549. *
  550. * @return array
  551. */
  552. function bimber_wc_product_list_custom_columns( $columns ) {
  553. if ( isset( $columns['featured_image'] ) ) {
  554. unset( $columns['featured_image'] );
  555. }
  556.  
  557. return $columns;
  558. }
  559.  
  560. function store_shop_page_templates( $page_templates, $class, $post ) {
  561. $shop_page_id = wc_get_page_id( 'shop' );
  562.  
  563. if ( $post && absint( $shop_page_id ) === absint( $post->ID ) ) {
  564. global $bimber_shop_page_templates;
  565.  
  566. $bimber_shop_page_templates = $page_templates;
  567. }
  568.  
  569. return $page_templates;
  570. }
  571.  
  572. function shop_shop_page_templates( $page_templates, $class, $post ) {
  573. $shop_page_id = wc_get_page_id( 'shop' );
  574.  
  575. if ( $post && absint( $shop_page_id ) === absint( $post->ID ) ) {
  576. global $bimber_shop_page_templates;
  577.  
  578. if ( empty( $page_templates ) && ! empty( $bimber_shop_page_templates ) ) {
  579. $page_templates = $bimber_shop_page_templates;
  580. }
  581. }
  582.  
  583. return $page_templates;
  584. }
  585.  
  586. function bimber_wc_empty_cart_message() { ?>
  587. <p class="g1-alpha g1-alpha-1st cart-empty">
  588. <?php _e( 'Your cart is currently empty.', 'woocommerce' ) ?>
  589. </p>
  590. <?php
  591. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement