Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.62 KB | None | 0 0
  1. <?php
  2.  
  3. // ativando suporte do tema ao woocommerce
  4. add_theme_support( 'woocommerce' );
  5.  
  6. // Ativando galeria padrão do Woo
  7. //add_theme_support( 'wc-product-gallery-zoom' );
  8. add_theme_support( 'wc-product-gallery-lightbox' );
  9. add_theme_support( 'wc-product-gallery-slider' );
  10.  
  11. // remove as tabs de informações necessárias do produto
  12. add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
  13.  
  14. function woo_remove_product_tabs( $tabs ) {
  15.  
  16. unset( $tabs['description'] ); // Remove the description tab
  17. //unset( $tabs['reviews'] ); // Remove the reviews tab
  18. unset( $tabs['additional_information'] ); // Remove the additional information tab
  19.  
  20. return $tabs;
  21.  
  22. }
  23.  
  24. /**
  25. * Set WooCommerce image dimensions upon theme activation
  26. */
  27. // Remove each style one by one
  28. add_filter( 'woocommerce_enqueue_styles', 'jk_dequeue_styles' );
  29. function jk_dequeue_styles( $enqueue_styles ) {
  30. unset( $enqueue_styles['woocommerce-general'] ); // Remove the gloss
  31. unset( $enqueue_styles['woocommerce-layout'] ); // Remove the layout
  32. unset( $enqueue_styles['woocommerce-smallscreen'] ); // Remove the smallscreen optimisation
  33. return $enqueue_styles;
  34. }
  35.  
  36. // Or just remove them all in one line
  37. add_filter( 'woocommerce_enqueue_styles', '__return_false' );
  38.  
  39.  
  40. /** Mostrar imagem da categoria **/
  41. function woocommerce_category_image() {
  42. global $wp_query;
  43. $taxonomy = $wp_query->get_queried_object();
  44. $taxonomy_image_desktop = wp_get_attachment_image_src(get_field('desktop', $taxonomy), 'full');
  45. $taxonomy_image_mobile = wp_get_attachment_image_src(get_field('mobile', $taxonomy), 'full');
  46. $taxonomy_image_link = esc_url(get_field('link', $taxonomy));
  47.  
  48. if ($taxonomy_image_link) {
  49. echo '<a class="main-slider__link" href="' . $taxonomy_image_link . '">';
  50. }
  51.  
  52. if ($taxonomy_image_desktop && $taxonomy_image_mobile) {
  53. echo '<picture class="taxonomy_image">';
  54. echo '<source media="(max-width: 500px)" srcset="' . $taxonomy_image_mobile[0] . '" alt="' . $taxonomy->name . '">';
  55. echo '<img src="' . $taxonomy_image_desktop[0] . '" alt="' . $taxonomy->name . '">';
  56. echo '</picture>';
  57. } elseif ($taxonomy_image_desktop) {
  58. echo '<img class="taxonomy_image" src="' . $taxonomy_image_desktop . '" alt="' . $taxonomy->name . '" />';
  59. }
  60.  
  61. if ($taxonomy_image_link) {
  62. echo '</a>';
  63. }
  64. }
  65.  
  66. // Não lista categorias "vazias" no Widget de produtos WooComerce
  67. function woo_hide_product_categories_widget( $list_args )
  68. {
  69. $list_args[ 'hide_empty' ] = 1;
  70. return $list_args;
  71. }
  72. add_filter( 'woocommerce_product_categories_widget_args', 'woo_hide_product_categories_widget' );
  73.  
  74. // troca a mensagem EM ESTOQUE
  75. add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);
  76.  
  77. function custom_get_availability( $availability, $_product ) {
  78. //change text "In Stock' to 'SPECIAL ORDER'
  79. if ( $_product->is_in_stock() ) $availability['availability'] = __('Disponível', 'woocommerce');
  80.  
  81. //change text "Out of Stock' to 'SOLD OUT'
  82. if ( !$_product->is_in_stock() ) $availability['availability'] = __('Fora de Estoque', 'woocommerce');
  83. return $availability;
  84.  
  85. }
  86.  
  87.  
  88. // Change number of products that are displayed per page (shop page)
  89. add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );
  90.  
  91. function new_loop_shop_per_page( $cols ) {
  92. // $cols contém o número atual de produtos por página com base no valor armazenado em opções-> leitura
  93. // Retorne o número de produtos que você deseja mostrar por página.
  94. $cols = 30;
  95. return $cols;
  96. }
  97.  
  98. // campo bairro obrigatório no woocommerce
  99. function custom_wcbcf_billing_fields( $fields ) {
  100. $fields['billing_neighborhood']['required'] = true;
  101. return $fields;
  102. }
  103. add_filter( 'wcbcf_billing_fields', 'custom_wcbcf_billing_fields' );
  104. function custom_wcbcf_shipping_fields( $fields ) {
  105. $fields['shipping_neighborhood']['required'] = true;
  106. return $fields;
  107. }
  108. add_filter( 'wcbcf_shipping_fields', 'custom_wcbcf_shipping_fields' );
  109.  
  110.  
  111. /**
  112. * Campo de telefone obrigatório
  113. */
  114. function checkout_fields_phone_required( $fields ) {
  115. $fields['billing_phone']['required'] = true;
  116. return $fields;
  117. }
  118. add_filter( 'woocommerce_billing_fields', 'checkout_fields_phone_required' );
  119.  
  120.  
  121. /**
  122. * Remove a imagem de categoria padrão do woocommerce
  123. */
  124. //add_action('admin_head', 'my_custom_fonts');
  125. function my_custom_fonts() {
  126. echo '
  127. <style>
  128. .term-thumbnail-wrap {
  129. display: none;
  130. }
  131. </style>';
  132. }
  133.  
  134.  
  135. // atualizar o carrinho automaticamente
  136. add_action( 'wp_footer', 'bbloomer_cart_refresh_update_qty' );
  137.  
  138. function bbloomer_cart_refresh_update_qty() {
  139. if (is_cart()) {
  140. ?>
  141. <script type="text/javascript">
  142. jQuery('div.woocommerce').on('click', 'input.qty', function(){
  143. jQuery("[name='update_cart']").trigger("click");
  144. });
  145. </script>
  146. <?php
  147. }
  148. }
  149.  
  150. // Calcula o parcelamento
  151. add_action('woocommerce_after_shop_loop_item_title', 'product_parceled_price', 20);
  152. add_action('woocommerce_single_product_summary', 'product_parceled_price', 11);
  153.  
  154. function product_parceled_price() {
  155. $product = wc_get_product();
  156.  
  157. $html = '<div class="installments">';
  158.  
  159. if ( wc_get_price_including_tax($product) > $valor_min_compra ) {
  160. $value = wc_price( wc_get_price_including_tax($product) / 6 );
  161. $html .= '<span class="installments_prefix">ou até 6x de </span>' . $value;
  162. }
  163.  
  164. $html .= '</div>';
  165. echo $html;
  166. }
  167.  
  168.  
  169. /**
  170. * Remove produtos que a variação não tem em estoque
  171. * @see https://github.com/woocommerce/woocommerce/issues/20689
  172. */
  173. add_action( 'woocommerce_before_shop_loop_item_title', 'remove_out_of_stock_products_from_active_filter' );
  174. function remove_out_of_stock_products_from_active_filter(){
  175. if (isset($_GET['filter_tamanho'])) {
  176. global $product;
  177. if ($product->is_type('variable')) {
  178. $variations = $product->get_available_variations();
  179. $is_available = false;
  180.  
  181. foreach ($variations as $variation) {
  182. if (isset($variation['attributes']['attribute_pa_tamanho'])) {
  183. if ($variation['attributes']['attribute_pa_tamanho'] == $_GET['filter_tamanho'] && $variation['is_in_stock']){
  184. $is_available = true;
  185. }
  186. }
  187. }
  188.  
  189. if (!$is_available) {
  190. global $product;
  191. $id = $product->get_id();
  192. echo "
  193. <style>
  194. .woocommerce-result-count { visibility: hidden }
  195. .post-$id { display: none !important }
  196. .woocommerce-pagination { display: none !important }
  197. </style>
  198. ";
  199. }
  200. }
  201. }
  202. }
  203.  
  204. // adiciona tag de desconto
  205. add_action( 'woocommerce_before_shop_loop_item_title', 'product_sale_percentage', 0 );
  206. add_action( 'woocommerce_single_product_summary', 'product_sale_percentage', 6 );
  207. function product_sale_percentage() {
  208. $product = wc_get_product();
  209. $is_sale = $product->is_on_sale();
  210.  
  211. if ( $product->is_on_sale() ) {
  212.  
  213. if ( $product->is_type( 'variable' ) ) {
  214. $percentage = round( ( ( $product->get_variation_regular_price('max') - $product->get_variation_sale_price('max') ) / $product->get_variation_regular_price('max') ) * 100 );
  215. echo '<span class="product-on-sale-percentage-tag">' . $percentage . '% OFF</span>';
  216. }
  217.  
  218. if ( $product->is_type( 'simple' ) ) {
  219. $percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 );
  220. echo '<span class="product-on-sale-percentage-tag">' . $percentage . '% OFF</span>';
  221. }
  222.  
  223. }
  224. }
  225.  
  226. // adiciona a tag de fora de estoque
  227. add_action( 'woocommerce_before_shop_loop_item_title', function() {
  228. global $product;
  229. if ($product->managing_stock() && !$product->is_in_stock() ) {
  230. echo '<span class="product_out_of_stock_label">Fora de estoque</span>';
  231. }
  232. });
  233.  
  234. // remove a necessidade de senha forte para clientes woocommerce
  235. function fa_remove_password_strength() {
  236. wp_dequeue_script( 'wc-password-strength-meter' );
  237. wp_deregister_script( 'wc-password-strength-meter' );
  238. }
  239. add_action( 'wp_enqueue_scripts', 'fa_remove_password_strength', 99999 );
  240.  
  241.  
  242. /**
  243. * Adiciona desconto baseado nos produtos do carrinho
  244. */
  245. function add_cart_fee_based_on_products($cart_object) {
  246. global $woocommerce;
  247. $fee_products = array(74445, 70749, 74496, 72872);
  248. $fee_percent = 25;
  249. $cart_products = array();
  250. foreach ($cart_object->cart_contents as $key => $value) {
  251. $cart_products[] = $value['product_id'];
  252. }
  253. $cart_has_fee_preducts = count(array_intersect($fee_products, $cart_products)) == count($fee_products);
  254. if ($cart_has_fee_preducts) {
  255. $fee_value = ($woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * ($fee_percent / 100) * -1;
  256. $woocommerce->cart->add_fee('Desconto de 25% dia dos Namorados.)', $fee_value, true, 'standard');
  257. }
  258. }
  259. add_action('woocommerce_cart_calculate_fees', 'add_cart_fee_based_on_products');
  260.  
  261.  
  262. // corrige o breadcrumb na single do produto
  263. add_filter( 'wpseo_breadcrumb_single_link' ,'wpseo_remove_breadcrumb_link', 10 ,2);
  264. function wpseo_remove_breadcrumb_link( $link_output , $link ){
  265. $text_to_remove = 'Produtos';
  266.  
  267. if( $link['text'] == $text_to_remove ) {
  268. $link_output = '';
  269. }
  270.  
  271. return $link_output;
  272. }
  273.  
  274.  
  275. // Remove o cross sell
  276. remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );
  277.  
  278. // Adiciona ele em outro lugar
  279. add_action( 'woocommerce_after_cart', 'woocommerce_cross_sell_display', 50 );
  280.  
  281.  
  282.  
  283. /**
  284. * Remove produtos fora de estoque do cross sell
  285. * @See https://github.com/woocommerce/woocommerce/blob/444dffdda27750b98ce1517d5f6f4d8153c53967/includes/class-wc-cart.php#L817
  286. */
  287. add_filter('woocommerce_cart_crosssell_ids', 'remove_out_of_stock_from_crosssell', 10, 1 );
  288. function remove_out_of_stock_from_crosssell( $wp_parse_id_list ){
  289. if (!empty($wp_parse_id_list)) {
  290. foreach ($wp_parse_id_list as $product_id) {
  291. $product = wc_get_product($product_id);
  292. // Verifica de o produto esta fora de estoque
  293. if ( $product->managing_stock() && !$product->is_in_stock() ) {
  294.  
  295. // Remove o produto da listagem
  296. if (($key = array_search($product_id, $wp_parse_id_list)) !== false) {
  297. unset($wp_parse_id_list[$key]);
  298. }
  299. }
  300. }
  301. }
  302. return $wp_parse_id_list;
  303. }
  304.  
  305. /**
  306. * Ordena produtos fora de estoque no final do catálogo
  307. * source: https://stackoverflow.com/a/44597448
  308. */
  309. add_filter('posts_clauses', 'order_by_stock_status');
  310. function order_by_stock_status($posts_clauses) {
  311. global $wpdb;
  312. // only change query on WooCommerce loops
  313. if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
  314. $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
  315. $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
  316. $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
  317. }
  318. return $posts_clauses;
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement