Advertisement
Guest User

Woocommerce FIX

a guest
May 8th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.69 KB | None | 0 0
  1. <?php
  2.  
  3. add_filter('woocommerce_enqueue_styles', '__return_false');
  4.  
  5. if (!function_exists('woocommerce_enabled')) {
  6.  
  7. function woocommerce_enabled() {
  8. if (class_exists('woocommerce'))
  9. return true;
  10. return false;
  11. }
  12.  
  13. }
  14.  
  15. /* ====== Remove Actions ====== */
  16.  
  17. remove_action('woocommerce_before_shop_loop', 'woocommerce_result_count', 20);
  18. remove_action('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30);
  19.  
  20.  
  21.  
  22.  
  23. /* ====== Remove Page Title ====== */
  24.  
  25. add_filter('woocommerce_show_page_title', 'title_none');
  26.  
  27. if (!function_exists('title_none')) {
  28.  
  29. function title_none() {
  30. return false;
  31. }
  32.  
  33. }
  34. /* ====== Close Div Tag ====== */
  35. if (!function_exists('tw_close_div')) {
  36.  
  37. function tw_close_div() {
  38. echo "</div>";
  39. }
  40.  
  41. }
  42. /* ====== Change Wrapper Begin ====== */
  43.  
  44. remove_action('woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
  45. add_action('woocommerce_before_main_content', 'tw_woocommerce_output_content_wrapper', 10);
  46.  
  47. if (!function_exists('tw_woocommerce_output_content_wrapper')) {
  48.  
  49. function tw_woocommerce_output_content_wrapper() {
  50. echo '<div class="container">';
  51. echo "<div class='row'>";
  52. if (is_singular('product')) {
  53. if (tw_option('woo_singlesidebar'))
  54. echo "<div class='span9'>";
  55. else
  56. echo "<div class='span12'>";
  57. } else if (tw_option('woo_shopsidebar')) {
  58. echo "<div class='span9'>";
  59. } else {
  60. echo "<div class='span12'>";
  61. }
  62. }
  63.  
  64. }
  65. /* ====== Change Wrapper End ====== */
  66.  
  67. remove_action('woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
  68. add_action('woocommerce_after_main_content', 'tw_close_div', 10);
  69.  
  70.  
  71.  
  72. /* ====== Added div loop product before ====== */
  73.  
  74. add_action('woocommerce_before_shop_loop_item', 'tw_loop_product_before', 5);
  75.  
  76. if (!function_exists('tw_loop_product_before')) {
  77.  
  78. function tw_loop_product_before() {
  79. echo "<div class='tw_product_container'>";
  80. }
  81.  
  82. }
  83.  
  84. add_action('woocommerce_after_shop_loop_item', 'tw_close_div', 1000);
  85.  
  86.  
  87.  
  88.  
  89.  
  90. add_action('woocommerce_before_shop_loop_item_title', 'tw_loop_product_title_container', 20);
  91.  
  92. if (!function_exists('tw_loop_product_title_container')) {
  93.  
  94. function tw_loop_product_title_container() {
  95. echo "<div class='tw_product_header'>";
  96. }
  97.  
  98. }
  99. add_action('woocommerce_after_shop_loop_item_title', 'tw_close_div', 1000);
  100.  
  101.  
  102.  
  103.  
  104. /* ====== Change Pagination ====== */
  105.  
  106. remove_action('woocommerce_after_shop_loop', 'woocommerce_pagination', 10);
  107. add_action('woocommerce_after_shop_loop', 'pagination', 10);
  108.  
  109.  
  110.  
  111.  
  112. /* ====== Change Breadcrumbs ====== */
  113.  
  114. remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);
  115. add_filter('woocommerce_breadcrumb_defaults', 'tw_woocommerce_breadcrumbs');
  116.  
  117. if (!function_exists('tw_woocommerce_breadcrumbs')) {
  118.  
  119. function tw_woocommerce_breadcrumbs() {
  120. return array(
  121. 'delimiter' => ' &#47; ',
  122. 'wrap_before' => '<div class="tw-breadcrumb pull-right" itemprop="breadcrumb">',
  123. 'wrap_after' => '</div>',
  124. 'before' => '<span>',
  125. 'after' => '</span>',
  126. 'home' => _x('Home', 'breadcrumb', 'themewaves'),
  127. );
  128. }
  129.  
  130. }
  131.  
  132. /* ====== Change Products Columns ====== */
  133.  
  134. add_filter('loop_shop_columns', 'loop_columns');
  135. if (!function_exists('loop_columns')) {
  136.  
  137. function loop_columns() {
  138. if (tw_option('woo_shopsidebar'))
  139. return 3; // 3 products per row
  140. return 4; // 3 products per row
  141. }
  142.  
  143. }
  144.  
  145.  
  146.  
  147.  
  148. /* ====== Product thumbs columns ====== */
  149. add_filter('woocommerce_product_thumbnails_columns', 'tw_woocommerce_product_thumbs_columns');
  150.  
  151. if (!function_exists('tw_woocommerce_product_thumbs_columns')) {
  152.  
  153. function tw_woocommerce_product_thumbs_columns() {
  154. return 4;
  155. }
  156.  
  157. }
  158.  
  159. /* ====== Product single thumb size ====== */
  160. add_image_size('tw_shop_single', 260, 260, false);
  161. add_filter('single_product_large_thumbnail_size', 'tw_single_product_large_thumb');
  162.  
  163. if (!function_exists('tw_single_product_large_thumb')) {
  164.  
  165. function tw_single_product_large_thumb() {
  166. return 'tw_shop_single';
  167. }
  168.  
  169. }
  170.  
  171. /* ====== Change Thumbnail ====== */
  172.  
  173. remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
  174. add_action('woocommerce_before_shop_loop_item_title', 'tw_woocommerce_thumb', 10);
  175.  
  176. if (!function_exists('tw_woocommerce_thumb')) {
  177.  
  178. function tw_woocommerce_thumb() {
  179. $id = get_the_ID();
  180. $width = 300;
  181. echo "<div class='thumbnail_container'>";
  182. echo post_image_show($width);
  183. echo tw_woocommerce_gallery_first_thumb($id, $width);
  184. echo "<span class='cart-load'></span>";
  185. echo "</div>";
  186. }
  187.  
  188. }
  189.  
  190. if (!function_exists('tw_woocommerce_gallery_first_thumb')) {
  191.  
  192. function tw_woocommerce_gallery_first_thumb($id, $width) {
  193. $gallery = get_post_meta($id, '_product_image_gallery', true);
  194. if (!empty($gallery)) {
  195. $woo_gallery = explode(',', $gallery);
  196. $image_id = $woo_gallery[0];
  197. $image = aq_resize(wp_get_attachment_url($image_id), $width, "", true);
  198. $alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
  199. $alt = !empty($alt) ? $alt : get_the_title();
  200. if (!empty($image))
  201. return '<img src="' . $image . '" class="product-hover-thumb" alt="' . $alt . '">';
  202. }
  203. }
  204.  
  205. }
  206. /* ====== Change Add To Cart ====== */
  207.  
  208. remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
  209. add_action('woocommerce_after_shop_loop_item', 'tw_add_cart_button', 16);
  210.  
  211. if (!function_exists('tw_add_cart_button')) {
  212.  
  213. function tw_add_cart_button() {
  214. global $product;
  215.  
  216. if ($product->product_type == 'bundle') {
  217. $product = new WC_Product_Bundle($product->id);
  218. }
  219.  
  220. ob_start();
  221. woocommerce_template_loop_add_to_cart();
  222. $output = ob_get_clean();
  223.  
  224. if ($product->product_type == 'variable' && empty($output)) {
  225. $output = "<a class='add_to_cart_button product_type_variable button' href='" . get_permalink($product->id) . "'>" . __('Select options', 'themewaves') . "</a>";
  226. }
  227.  
  228. if ($product->product_type == 'simple') {
  229. $output .= "<a class='show_details_button button' href='" . get_permalink($product->id) . "'>" . __('Show Details', 'themewaves') . "</a>";
  230. }
  231.  
  232. if ($output)
  233. echo "<div class='tw_cart_buttons button'>$output</div>";
  234. }
  235.  
  236. }
  237. /* ====== Products per page ====== */
  238.  
  239. add_filter('loop_shop_per_page', 'tw_products_count');
  240.  
  241. if (!function_exists('tw_products_count')) {
  242.  
  243. function tw_products_count() {
  244. global $tw_woocommerce;
  245. if (!empty($tw_woocommerce['products_per_page']))
  246. return $tw_woocommerce['products_per_page'];
  247. return tw_option('woo_per_page');
  248. }
  249.  
  250. }
  251.  
  252. /* ====== Products sort ====== */
  253.  
  254. add_action('woocommerce_before_shop_loop', 'tw_woocommerce_sorting', 20);
  255.  
  256. if (!function_exists('tw_woocommerce_sorting')) {
  257.  
  258. function tw_woocommerce_sorting() {
  259. global $tw_woocommerce;
  260.  
  261. if (!tw_option('woo_sorting'))
  262. return false;
  263.  
  264. $product_order['default'] = __("Default Order", "themewaves");
  265. $product_order['title'] = __("Name", "themewaves");
  266. $product_order['price'] = __("Price", "themewaves");
  267. $product_order['date'] = __("Date", "themewaves");
  268. $product_order['popularity'] = __("Popularity", "themewaves");
  269.  
  270. $product_sort['asc'] = __("Click to order products ascending", "themewaves");
  271. $product_sort['desc'] = __("Click to order products descending", "themewaves");
  272.  
  273. $per_page_string = __("Products per page", "themewaves");
  274.  
  275. $per_page = tw_option('woo_per_page');
  276. if (!$per_page)
  277. $per_page = get_option('posts_per_page');
  278.  
  279. parse_str($_SERVER['QUERY_STRING'], $params);
  280.  
  281. $po_key = !empty($tw_woocommerce['product_order']) ? $tw_woocommerce['product_order'] : 'default';
  282. $ps_key = !empty($tw_woocommerce['product_sort']) ? $tw_woocommerce['product_sort'] : 'asc';
  283. $pc_key = !empty($tw_woocommerce['product_count']) ? $tw_woocommerce['product_count'] : $per_page;
  284.  
  285. $ps_key = strtolower($ps_key);
  286.  
  287. //generate markup
  288. $output = "";
  289. $output .= "<div class='tw-product-ordering'>";
  290. $output .= " <ul class='product-order'>";
  291. $output .= " <li><span class='tw-order-current'>" . __("Sort by", "themewaves") . " <span>" . $product_order[$po_key] . "</span><i class='fa fa-sort'></i></span>";
  292. $output .= " <ul>";
  293. $output .= " <li><a href='" . avia_woo_build_query_string($params, 'product_order', 'default') . "'> <span class='avia-bullet'></span>" . $product_order['default'] . "</a></li>";
  294. $output .= " <li><a href='" . avia_woo_build_query_string($params, 'product_order', 'title') . "'> <span class='avia-bullet'></span>" . $product_order['title'] . "</a></li>";
  295. $output .= " <li><a href='" . avia_woo_build_query_string($params, 'product_order', 'price') . "'> <span class='avia-bullet'></span>" . $product_order['price'] . "</a></li>";
  296. $output .= " <li><a href='" . avia_woo_build_query_string($params, 'product_order', 'date') . "'> <span class='avia-bullet'></span>" . $product_order['date'] . "</a></li>";
  297. $output .= " <li><a href='" . avia_woo_build_query_string($params, 'product_order', 'popularity') . "'> <span class='avia-bullet'></span>" . $product_order['popularity'] . "</a></li>";
  298. $output .= " </ul>";
  299. $output .= " </li>";
  300. $output .= " </ul>";
  301.  
  302. $output .= " <ul class='product-count'>";
  303. $output .= " <li><span class='tw-order-current'>" . __("Display", "themewaves") . " <span>" . $pc_key . " " . $per_page_string . "</span><i class='fa fa-sort'></i></span>";
  304. $output .= " <ul>";
  305. $output .= " <li><a href='" . avia_woo_build_query_string($params, 'product_count', $per_page) . "'> <span class='avia-bullet'></span>" . $per_page . " " . $per_page_string . "</a></li>";
  306. $output .= " <li><a href='" . avia_woo_build_query_string($params, 'product_count', $per_page * 2) . "'> <span class='avia-bullet'></span>" . ($per_page * 2) . " " . $per_page_string . "</a></li>";
  307. $output .= " <li><a href='" . avia_woo_build_query_string($params, 'product_count', $per_page * 3) . "'> <span class='avia-bullet'></span>" . ($per_page * 3) . " " . $per_page_string . "</a></li>";
  308. $output .= " </ul>";
  309. $output .= " </li>";
  310. $output .= " </ul>";
  311.  
  312. $output .= " <ul class='product-sort'>";
  313. $output .= " <li>";
  314. if ($ps_key == 'desc')
  315. $output .= "<a title='" . $product_sort['asc'] . "' class='sort-asc' href='" . avia_woo_build_query_string($params, 'product_sort', 'asc') . "'><i class='fa fa-long-arrow-down'></i></a>";
  316. if ($ps_key == 'asc')
  317. $output .= "<a title='" . $product_sort['desc'] . "' class='sort-desc' href='" . avia_woo_build_query_string($params, 'product_sort', 'desc') . "'><i class='fa fa-long-arrow-up'></i></a>";
  318. $output .= " </li>";
  319. $output .= " </ul>";
  320.  
  321. $output .= "</div>";
  322. echo $output;
  323. }
  324.  
  325. }
  326.  
  327. //helper function to build the query strings for the catalog ordering menu
  328. if (!function_exists('avia_woo_build_query_string')) {
  329.  
  330. function avia_woo_build_query_string($params = array(), $overwrite_key, $overwrite_value) {
  331. $params[$overwrite_key] = $overwrite_value;
  332. return "?" . http_build_query($params);
  333. }
  334.  
  335. }
  336.  
  337. //function that actually overwrites the query parameters
  338. add_action('woocommerce_get_catalog_ordering_args', 'avia_woocommerce_overwrite_catalog_ordering', 20);
  339.  
  340. if (!function_exists('avia_woocommerce_overwrite_catalog_ordering')) {
  341.  
  342. function avia_woocommerce_overwrite_catalog_ordering($args) {
  343. global $tw_woocommerce;
  344.  
  345. if (!tw_option('woo_sorting'))
  346. return $args;
  347.  
  348. //check the folllowing get parameters and session vars. if they are set overwrite the defaults
  349. $check = array('product_order', 'product_count', 'product_sort');
  350. if (empty($tw_woocommerce))
  351. $tw_woocommerce = array();
  352.  
  353. foreach ($check as $key) {
  354. if (isset($_GET[$key]))
  355. $_SESSION['tw_woocommerce'][$key] = esc_attr($_GET[$key]);
  356. if (isset($_SESSION['tw_woocommerce'][$key]))
  357. $tw_woocommerce[$key] = $_SESSION['tw_woocommerce'][$key];
  358. }
  359.  
  360.  
  361. // is user wants to use new product order remove the old sorting parameter
  362. if (isset($_GET['product_order']) && !isset($_GET['product_sort']) && isset($_SESSION['tw_woocommerce']['product_sort'])) {
  363. unset($_SESSION['tw_woocommerce']['product_sort'], $tw_woocommerce['product_sort']);
  364. }
  365.  
  366. extract($tw_woocommerce);
  367.  
  368. // set the product order
  369. if (!empty($product_order)) {
  370. switch ($product_order) {
  371. case 'date' : $orderby = 'date';
  372. $order = 'desc';
  373. $meta_key = '';
  374. break;
  375. case 'price' : $orderby = 'meta_value_num';
  376. $order = 'asc';
  377. $meta_key = '_price';
  378. break;
  379. case 'popularity' : $orderby = 'meta_value_num';
  380. $order = 'desc';
  381. $meta_key = 'total_sales';
  382. break;
  383. case 'title' : $orderby = 'title';
  384. $order = 'asc';
  385. $meta_key = '';
  386. break;
  387. case 'default':
  388. default : $orderby = 'menu_order title';
  389. $order = 'asc';
  390. $meta_key = '';
  391. break;
  392. }
  393. }
  394.  
  395. // set the product count
  396. if (!empty($product_count) && is_numeric($product_count)) {
  397. $tw_woocommerce['products_per_page'] = (int) $product_count;
  398. }
  399.  
  400. //set the product sorting
  401. if (!empty($product_sort)) {
  402. switch ($product_sort) {
  403. case 'desc' : $order = 'desc';
  404. break;
  405. case 'asc' : $order = 'asc';
  406. break;
  407. default : $order = 'asc';
  408. break;
  409. }
  410. }
  411.  
  412.  
  413. if (isset($orderby))
  414. $args['orderby'] = $orderby;
  415. if (isset($order))
  416. $args['order'] = $order;
  417. if (!empty($meta_key)) {
  418. $args['meta_key'] = $meta_key;
  419. }
  420.  
  421.  
  422. $tw_woocommerce['product_sort'] = $args['order'];
  423.  
  424. return $args;
  425. }
  426.  
  427. }
  428.  
  429. /**
  430. * WooCommerce Extra Feature
  431. * --------------------------
  432. *
  433. * Change number of related products on product page
  434. * Set your own value for 'posts_per_page'
  435. *
  436. */
  437. if (!function_exists('woo_related_products_limit')) {
  438.  
  439. function woo_related_products_limit() {
  440. global $product;
  441.  
  442. $args['posts_per_page'] = 6;
  443. return $args;
  444. }
  445.  
  446. }
  447. add_filter('woocommerce_output_related_products_args', 'jk_related_products_args');
  448.  
  449. if (!function_exists('jk_related_products_args')) {
  450.  
  451. function jk_related_products_args($args) {
  452.  
  453. $args['posts_per_page'] = 3; // 4 related products
  454. $args['columns'] = 3; // arranged in 2 columns
  455. return $args;
  456. }
  457.  
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement