PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Project/faci-qsoft/wp-content/themes/noo-jobmonster/framework/functions/woocommerce.php

https://gitlab.com/hop23typhu/list-theme
PHP | 403 lines | 311 code | 64 blank | 28 comment | 55 complexity | 63223c99f9e535de788eacee20047065 MD5 | raw file
  1. <?php
  2. if ( class_exists( 'woocommerce' ) ) {
  3. if ( version_compare( WOOCOMMERCE_VERSION, "2.1" ) >= 0 ) {
  4. // WooCommerce 2.1 or above is active
  5. add_filter( 'woocommerce_enqueue_styles', '__return_false' );
  6. } else {
  7. // WooCommerce is less than 2.1
  8. define( 'WOOCOMMERCE_USE_CSS', false );
  9. }
  10. function noo_woocommerce_show_page_title() {
  11. return false;
  12. }
  13. add_filter( 'woocommerce_show_page_title', 'noo_woocommerce_show_page_title' );
  14. // Number of products per page
  15. function noo_woocommerce_loop_shop_per_page() {
  16. return noo_get_option( 'noo_shop_num', 12 );
  17. }
  18. add_filter( 'loop_shop_per_page', 'noo_woocommerce_loop_shop_per_page' );
  19. function noo_add_to_cart_fragments( $fragments ) {
  20. $output = noo_minicart();
  21. $fragments['.minicart'] = $output;
  22. $fragments['.mobile-minicart-icon'] = noo_minicart_mobile();
  23. return $fragments;
  24. }
  25. //add_filter( 'add_to_cart_fragments', 'noo_add_to_cart_fragments' );
  26. function noo_woocommerce_remove_cart_item() {
  27. global $woocommerce;
  28. $response = array();
  29. if ( ! isset( $_GET['item'] ) && ! isset( $_GET['_wpnonce'] ) ) {
  30. exit();
  31. }
  32. $woocommerce->cart->set_quantity( $_GET['item'], 0 );
  33. $cart_count = $woocommerce->cart->cart_contents_count;
  34. $response['count'] = $cart_count != 0 ? $cart_count : "";
  35. $response['minicart'] = noo_minicart( true );
  36. // widget cart update
  37. ob_start();
  38. woocommerce_mini_cart();
  39. $mini_cart = ob_get_clean();
  40. $response['widget'] = '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>';
  41. echo json_encode( $response );
  42. exit();
  43. }
  44. //add_action( 'wp_ajax_noo_woocommerce_remove_cart_item', 'noo_woocommerce_remove_cart_item' );
  45. //add_action( 'wp_ajax_nopriv_noo_woocommerce_remove_cart_item', 'noo_woocommerce_remove_cart_item' );
  46. function noo_product_items_text( $count ) {
  47. $product_item_text = "";
  48. if ( $count > 1 ) {
  49. $product_item_text = str_replace( '%', number_format_i18n( $count ), __( '% items', 'noo' ) );
  50. } elseif ( $count == 0 ) {
  51. $product_item_text = __( '0 items', 'noo' );
  52. } else {
  53. $product_item_text = __( '1 item', 'noo' );
  54. }
  55. return $product_item_text;
  56. }
  57. // Mobile icon
  58. function noo_minicart_mobile() {
  59. if( ! noo_get_option('noo_header_nav_icon_cart', true ) ) {
  60. return '';
  61. }
  62. global $woocommerce;
  63. $cart_output = "";
  64. $cart_total = $woocommerce->cart->get_cart_total();
  65. $cart_count = $woocommerce->cart->cart_contents_count;
  66. $cart_output = '<a href="' . $woocommerce->cart->get_cart_url() . '" title="' . __( 'View Cart', 'woocommerce' ) .
  67. '" class="mobile-minicart-icon"><i class="fa fa-shopping-cart"></i><span>' . $cart_count . '</span></a>';
  68. return $cart_output;
  69. }
  70. // Menu cart
  71. function noo_minicart( $content = false ) {
  72. if( ! noo_get_option('noo_header_nav_icon_cart', true ) ) {
  73. return '';
  74. }
  75. global $woocommerce;
  76. $cart_output = "";
  77. $cart_total = $woocommerce->cart->get_cart_total();
  78. $cart_count = $woocommerce->cart->cart_contents_count;
  79. $cart_count_text = noo_product_items_text( $cart_count );
  80. $cart_has_items = '';
  81. if ( $cart_count != "0" ) {
  82. $cart_has_items = ' has-items';
  83. }
  84. $output = '';
  85. if ( ! $content ) {
  86. $output .= '<li id="nav-menu-item-cart" class="menu-item noo-menu-item-cart minicart"><a title="' .
  87. __( 'View cart', 'noo' ) . '" class="cart-button" href="' . $woocommerce->cart->get_cart_url() .
  88. '">' . '<span class="cart-item' . $cart_has_items . '"><i class="fa fa-shopping-cart"></i>';
  89. if ( $cart_count != "0" ) {
  90. $output .= "<span>" . $cart_count . "</span>";
  91. }
  92. $output .= '</span>';
  93. $output .= '</a>';
  94. $output .= '<div class="noo-minicart">';
  95. }
  96. if ( $cart_count != "0" ) {
  97. $output .= '<div class="minicart-header">' . $cart_count_text . ' ' .
  98. __( 'in the shopping cart', 'noo' ) . '</div>';
  99. $output .= '<div class="minicart-body">';
  100. foreach ( $woocommerce->cart->cart_contents as $cart_item_key => $cart_item ) {
  101. $cart_product = $cart_item['data'];
  102. $product_title = $cart_product->get_title();
  103. $product_short_title = ( strlen( $product_title ) > 25 ) ? substr( $product_title, 0, 22 ) . '...' : $product_title;
  104. if ( $cart_product->exists() && $cart_item['quantity'] > 0 ) {
  105. $output .= '<div class="cart-product clearfix">';
  106. $output .= '<div class="cart-product-image"><a class="cart-product-img" href="' .
  107. get_permalink( $cart_item['product_id'] ) . '">' . $cart_product->get_image() . '</a></div>';
  108. $output .= '<div class="cart-product-details">';
  109. $output .= '<div class="cart-product-title"><a href="' . get_permalink( $cart_item['product_id'] ) .
  110. '">' .
  111. apply_filters( 'woocommerce_cart_widget_product_title', $product_short_title, $cart_product ) .
  112. '</a></div>';
  113. $output .= '<div class="cart-product-price">' . __( "Price", "woocommerce" ) . ' ' .
  114. woocommerce_price( $cart_product->get_price() ) . '</div>';
  115. $output .= '<div class="cart-product-quantity">' . __( 'Quantity', 'woocommerce' ) . ' ' .
  116. $cart_item['quantity'] . '</div>';
  117. $output .= '</div>';
  118. $output .= apply_filters(
  119. 'woocommerce_cart_item_remove_link',
  120. sprintf(
  121. '<a href="%s" class="remove" title="%s">&times;</a>',
  122. esc_url( $woocommerce->cart->get_remove_url( $cart_item_key ) ),
  123. __( 'Remove this item', 'woocommerce' ) ),
  124. $cart_item_key );
  125. $output .= '</div>';
  126. }
  127. }
  128. $output .= '</div>';
  129. $output .= '<div class="minicart-footer">';
  130. $output .= '<div class="minicart-total">' . __( 'Cart Subtotal', 'woocommerce' ) . ' ' . $cart_total .
  131. '</div>';
  132. $output .= '<div class="minicart-actions clearfix">';
  133. if ( version_compare( WOOCOMMERCE_VERSION, "2.1.0" ) >= 0 ) {
  134. $cart_url = apply_filters( 'woocommerce_get_checkout_url', WC()->cart->get_cart_url() );
  135. $checkout_url = apply_filters( 'woocommerce_get_checkout_url', WC()->cart->get_checkout_url() );
  136. $output .= '<a class="button" href="' . esc_url( $cart_url ) . '"><span class="text">' .
  137. __( 'View Cart', 'woocommerce' ) . '</span></a>';
  138. $output .= '<a class="checkout-button button" href="' . esc_url( $checkout_url ) .
  139. '"><span class="text">' . __( 'Proceed to Checkout', 'woocommerce' ) . '</span></a>';
  140. } else {
  141. $output .= '<a class="button" href="' . esc_url( $woocommerce->cart->get_cart_url() ) .
  142. '"><span class="text">' . __( 'View Cart', 'woocommerce' ) . '</span></a>';
  143. $output .= '<a class="checkout-button button" href="' . esc_url(
  144. $woocommerce->cart->get_checkout_url() ) . '"><span class="text">' .
  145. __( 'Proceed to Checkout', 'woocommerce' ) . '</span></a>';
  146. }
  147. $output .= '</div>';
  148. $output .= '</div>';
  149. } else {
  150. $output .= '<div class="minicart-header">' . __( 'Your shopping bag is empty.', 'noo' ) . '</div>';
  151. $shop_page_url = "";
  152. if ( version_compare( WOOCOMMERCE_VERSION, "2.1.0" ) >= 0 ) {
  153. $shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
  154. } else {
  155. $shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
  156. }
  157. $output .= '<div class="minicart-footer">';
  158. $output .= '<div class="minicart-actions clearfix">';
  159. $output .= '<a class="button pull-left" href="' . esc_url( $shop_page_url ) . '"><span class="text">' .
  160. __( 'Go to the shop', 'noo' ) . '</span></a>';
  161. $output .= '</div>';
  162. $output .= '</div>';
  163. }
  164. if ( ! $content ) {
  165. $output .= '</div>';
  166. $output .= '</li>';
  167. }
  168. return $output;
  169. }
  170. function noo_navbar_minicart( $items, $args ) {
  171. if ( $args->theme_location == 'primary' ) {
  172. $minicart = noo_minicart();
  173. $items .= $minicart;
  174. }
  175. return $items;
  176. }
  177. //add_filter( 'wp_nav_menu_items', 'noo_navbar_minicart', 10, 2 );
  178. function noo_woocommerce_update_product_image_size() {
  179. $catalog = array( 'width' => '500', 'height' => '700', 'crop' => 1 );
  180. $single = array( 'width' => '500', 'height' => '700', 'crop' => 1 );
  181. $thumbnail = array( 'width' => '100', 'height' => '100', 'crop' => 1 );
  182. update_option( 'shop_catalog_image_size', $catalog );
  183. update_option( 'shop_single_image_size', $single );
  184. update_option( 'shop_thumbnail_image_size', $thumbnail );
  185. }
  186. if ( is_admin() && isset( $_GET['activated'] ) && $pagenow == 'themes.php' ) {
  187. add_action( 'init', 'noo_woocommerce_update_product_image_size', 1 );
  188. }
  189. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  190. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  191. function noo_woocommerce_shop_columns() {
  192. if ( noo_get_option( 'noo_shop_layout', 'fullwidth' ) === 'fullwidth' ) {
  193. return 4;
  194. }
  195. return 3;
  196. }
  197. add_filter( 'loop_shop_columns', 'noo_woocommerce_shop_columns' );
  198. function noo_woocommerce_shop_posts_per_page() {
  199. return noo_get_option( 'noo_woocommerce_shop_count', 12 );
  200. }
  201. add_filter( 'loop_shop_per_page', 'noo_woocommerce_shop_posts_per_page' );
  202. // Loop thumbnail
  203. remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
  204. add_action( 'woocommerce_before_shop_loop_item_title', 'noo_template_loop_product_thumbnail', 10 );
  205. //add_action( 'woocommerce_before_shop_loop_item_title', 'noo_template_loop_product_frist_thumbnail', 11 );
  206. function noo_template_loop_product_thumbnail() {
  207. $first_image = noo_template_loop_product_get_frist_thumbnail();
  208. echo '<div class="noo-product-thumbnail'.(!empty($first_image) ? ' noo-product-front-thumbnail':'').'">' . woocommerce_get_product_thumbnail() .'</div>';
  209. if ( $first_image != '' ) {
  210. echo '<div class="noo-product-thumbnail noo-product-back-thumbnail">' . $first_image . '</div>';
  211. }
  212. }
  213. function noo_template_loop_product_get_frist_thumbnail() {
  214. global $product, $post;
  215. $image = '';
  216. if ( version_compare( WOOCOMMERCE_VERSION, "2.0.0" ) >= 0 ) {
  217. $attachment_ids = $product->get_gallery_attachment_ids();
  218. $image_count = 0;
  219. if ( $attachment_ids ) {
  220. foreach ( $attachment_ids as $attachment_id ) {
  221. if ( noo_get_post_meta( $attachment_id, '_woocommerce_exclude_image' ) )
  222. continue;
  223. $image = wp_get_attachment_image( $attachment_id, 'shop_catalog' );
  224. $image_count++;
  225. if ( $image_count == 1 )
  226. break;
  227. }
  228. }
  229. } else {
  230. $attachments = get_posts(
  231. array(
  232. 'post_type' => 'attachment',
  233. 'numberposts' => - 1,
  234. 'post_status' => null,
  235. 'post_parent' => $post->ID,
  236. 'post__not_in' => array( get_post_thumbnail_id() ),
  237. 'post_mime_type' => 'image',
  238. 'orderby' => 'menu_order',
  239. 'order' => 'ASC',
  240. 'suppress_filters' => false ) );
  241. $image_count = 0;
  242. if ( $attachments ) {
  243. foreach ( $attachments as $attachment ) {
  244. if ( noo_get_post_meta( $attachment->ID, '_woocommerce_exclude_image' ) == 1 )
  245. continue;
  246. $image = wp_get_attachment_image( $attachment->ID, 'shop_catalog' );
  247. $image_count++;
  248. if ( $image_count == 1 )
  249. break;
  250. }
  251. }
  252. }
  253. return $image;
  254. }
  255. // Loop actions
  256. add_action( 'woocommerce_after_shop_loop_item', 'noo_template_loop_quickview', 11 );
  257. function noo_template_loop_quickview() {
  258. global $product;
  259. echo '<a class="button shop-loop-quickview" data-product_id ="' . $product->id . '" href="' . $product->get_permalink() .
  260. '">' . esc_html__( 'Quick view', 'noo' ) . '</a>';
  261. }
  262. add_action( 'woocommerce_after_shop_loop_item', 'noo_template_loop_wishlist', 12 );
  263. function noo_template_loop_wishlist() {
  264. if ( noo_woocommerce_wishlist_is_active() ) {
  265. echo do_shortcode( '[yith_wcwl_add_to_wishlist]' );
  266. }
  267. }
  268. // Quick view
  269. add_action( 'wp_ajax_woocommerce_quickview', 'noo_woocommerce_quickview' );
  270. add_action( 'wp_ajax_nopriv_woocommerce_quickview', 'noo_woocommerce_quickview' );
  271. function noo_woocommerce_quickview() {
  272. global $woocommerce, $post, $product;
  273. $product_id = $_POST['product_id'];
  274. $product = get_product( $product_id );
  275. $post = get_post( $product_id );
  276. $output = '';
  277. ob_start();
  278. woocommerce_get_template( 'quickview.php' );
  279. $output = ob_get_contents();
  280. ob_end_clean();
  281. echo $output;
  282. die();
  283. }
  284. // Wishlist
  285. if ( ! function_exists( 'noo_woocommerce_wishlist_is_active' ) ) {
  286. /**
  287. * Check yith-woocommerce-wishlist plugin is active
  288. *
  289. * @return boolean .TRUE is active
  290. */
  291. function noo_woocommerce_wishlist_is_active() {
  292. $active_plugins = (array) get_option( 'active_plugins', array() );
  293. if ( is_multisite() )
  294. $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
  295. return in_array( 'yith-woocommerce-wishlist/init.php', $active_plugins ) ||
  296. array_key_exists( 'yith-woocommerce-wishlist/init.php', $active_plugins );
  297. }
  298. }
  299. if ( ! function_exists( 'noo_woocommerce_compare_is_active' ) ) {
  300. /**
  301. * Check yith-woocommerce-compare plugin is active
  302. *
  303. * @return boolean .TRUE is active
  304. */
  305. function noo_woocommerce_compare_is_active() {
  306. $active_plugins = (array) get_option( 'active_plugins', array() );
  307. if ( is_multisite() )
  308. $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
  309. return in_array( 'yith-woocommerce-compare/init.php', $active_plugins ) ||
  310. array_key_exists( 'yith-woocommerce-compare/init.php', $active_plugins );
  311. }
  312. }
  313. remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
  314. add_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_sale_flash' );
  315. // Single share
  316. add_action( 'woocommerce_single_product_summary', 'noo_social_share', 50 );
  317. // Related products
  318. add_filter( 'woocommerce_output_related_products_args', 'noo_woocommerce_output_related_products_args' );
  319. function noo_woocommerce_output_related_products_args() {
  320. if ( noo_get_option( 'noo_shop_layout', 'fullwidth' ) === 'fullwidth' ) {
  321. $args = array( 'posts_per_page' => 4, 'columns' => 4 );
  322. return $args;
  323. }
  324. $args = array( 'posts_per_page' => 3, 'columns' => 3 );
  325. return $args;
  326. }
  327. // Upsell products
  328. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
  329. add_action( 'woocommerce_after_single_product_summary', 'noo_woocommerce_upsell_display', 15 );
  330. if ( ! function_exists( 'noo_woocommerce_upsell_display' ) ) {
  331. function noo_woocommerce_upsell_display() {
  332. if ( noo_get_option( 'noo_shop_layout', 'fullwidth' ) === 'fullwidth' ) {
  333. woocommerce_upsell_display( - 1, 4 );
  334. } else {
  335. woocommerce_upsell_display( - 1, 3 );
  336. }
  337. }
  338. }
  339. }