Advertisement
Guest User

functions.php

a guest
Oct 16th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.86 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * CONSTANTS
  5.  */
  6. define('WC_MAX_LINKED_VARIATIONS', 250);
  7.  
  8. /* Enables WPML for Extra Product Option */
  9. add_filter('thwepo_ignore_wpml_translation_for_product_category', '__return_true');
  10.  
  11. /* Change the amount of prodcuts per page */
  12. // add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 8;' ), 50 );
  13.  
  14. /**
  15.  * Enqueue Styles
  16.  */
  17. function nm_child_theme_styles() {
  18.      // Enqueue child theme styles
  19.      wp_enqueue_style( 'nm-child-theme', get_stylesheet_directory_uri() . '/style.css' );
  20. }
  21. add_action( 'wp_enqueue_scripts', 'nm_child_theme_styles', 1000 ); // Note: Use priority "1000" to include the stylesheet after the parent theme stylesheets
  22.  
  23. function wpb_adding_scripts() {
  24.     wp_register_script('myscript', get_stylesheet_directory_uri() . '/myscript.js', array('jquery'),'1.1', true);
  25.     wp_enqueue_script('myscript');
  26. }
  27. add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
  28.  
  29. /*
  30.  * Custom Woocommerce Options
  31.  * _brochure renders a field which can contain an url.
  32.  * _titel renders a button on the single product page redirecting to this url.
  33.  * _product_sub_title adds a subtitle to the product.
  34.  **/
  35. function k2i_add_custom_general_fields(){
  36.  
  37.   global $woocommerce, $post;
  38.  
  39.     echo '<div class="options_group">';    
  40.         woocommerce_wp_text_input(
  41.             array(
  42.                 'id'          => '_product_sub_title',
  43.                 'label'       => __( 'Product Sub Title', 'woocommerce' ),
  44.                 'placeholder' => 'Your text here...',
  45.                 'desc_tip'    => 'true',
  46.                 'description' => __( 'Please enter your product subtitle.', 'woocommerce' )
  47.             )
  48.         );
  49.         woocommerce_wp_text_input(
  50.             array(
  51.                 'id'          => '_product_set_price',
  52.                 'label'       => __( 'Set price', 'woocommerce' ),
  53.                 'placeholder' => 'Enter price...',
  54.                 'desc_tip'    => 'true',
  55.                 'description' => __( 'Please enter the set price.', 'woocommerce' )
  56.             )
  57.         );
  58.     echo '</div>';
  59.  
  60. }
  61. add_action( 'woocommerce_product_options_general_product_data', 'k2i_add_custom_general_fields' );
  62.  
  63. /*
  64.  * It's important to save these options.
  65.  **/
  66. function k2i_add_custom_general_fields_save( $post_id ){
  67.  
  68.     $woocommerce_custom_option_product_sub_title = $_POST['_product_sub_title'];
  69.     if( !empty( $woocommerce_custom_option_product_sub_title ) )
  70.         update_post_meta( $post_id, '_product_sub_title', esc_attr( $woocommerce_custom_option_product_sub_title ) );
  71.     if( empty( $woocommerce_custom_option_product_sub_title ) )
  72.         update_post_meta( $post_id, '_product_sub_title', null );      
  73.  
  74.     $woocommerce_custom_option_product_set_price = $_POST['_product_set_price'];
  75.     if( !empty( $woocommerce_custom_option_product_set_price ) )
  76.         update_post_meta( $post_id, '_product_set_price', esc_attr( $woocommerce_custom_option_product_set_price ) );
  77.     if( empty( $woocommerce_custom_option_product_set_price ) )
  78.         update_post_meta( $post_id, '_product_set_price', null );
  79.  
  80. }
  81. add_action( 'woocommerce_process_product_meta', 'k2i_add_custom_general_fields_save' );
  82.  
  83. /**
  84.  * Add checkbox field to the checkout
  85.  **/
  86. add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
  87.  
  88. function my_custom_checkout_field( $checkout ) {
  89.  
  90.     echo '<div id="invoice-checkbox">';
  91.  
  92.     woocommerce_form_field( 'my_checkbox', array(
  93.         'type'          => 'checkbox',
  94.         'class'         => array('input-checkbox'),
  95.         'label'         => __('Send me an invoice for this order.'),
  96.         'required'  => false,
  97.         ), $checkout->get_value( 'my_checkbox' ));
  98.  
  99.     echo '</div>';
  100. }
  101.  
  102. /**
  103.  * Process the checkout
  104.  **/
  105. // add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
  106.  
  107. // function my_custom_checkout_field_process() {
  108. //     global $woocommerce;
  109.  
  110. //     // Check if set, if its not set add an error.
  111. //     if (!$_POST['my_checkbox'])
  112. //          $woocommerce->add_error( __('Please agree to my checkbox.') );
  113. // }
  114.  
  115. /**
  116.  * Update the order meta with field value
  117.  **/
  118. function my_custom_checkout_field_update_order_meta( $order_id ) {
  119.     if ($_POST['my_checkbox']) update_post_meta( $order_id, 'Invoice', esc_attr($_POST['my_checkbox']));
  120. }
  121. add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
  122.  
  123. /**
  124.  * Display WooCommerce product category description on all category archive pages
  125.  **/
  126. function k2i_woocommerce_taxonomy_archive_description_desktop() {
  127.     $description = wc_format_content( term_description() );
  128.     if ( $description  ) {
  129.         echo '<div class="term-description-desktop">' . $description . '</div>';
  130.     }
  131. }
  132. function k2i_woocommerce_taxonomy_archive_description_memorial_jewelry() {
  133.     $description = wc_format_content( term_description() );
  134.     if ( $description  ) {
  135.         if( is_product_category('memorial-jewelry')) {
  136.             echo '<div class="term-description-mobile">' . $description . '</div>';
  137.         }
  138.     }
  139. }
  140. function k2i_woocommerce_taxonomy_archive_description_mobile() {
  141.     $description = wc_format_content( term_description() );
  142.     if ( $description ) {
  143.         if( !is_product_category('memorial-jewelry'))  {
  144.             echo '<div class="term-description-mobile">' . $description . '</div>';
  145.         }
  146.     }
  147. }
  148. add_action( 'woocommerce_before_shop_loop', 'k2i_woocommerce_taxonomy_archive_description_desktop');
  149. add_action( 'woocommerce_before_shop_loop', 'k2i_woocommerce_taxonomy_archive_description_memorial_jewelry');
  150. add_action( 'woocommerce_after_shop_loop', 'k2i_woocommerce_taxonomy_archive_description_mobile');
  151.  
  152. function k2i_text_on_woocommerce_shop_page_desktop() {
  153.     if (is_shop()) {
  154.         echo '<div class="term-description-desktop"><div class="category-text-container"><div class="category-text-left-column"><h1>Hier komt de beschrijving</h1>Het is al geruime tijd een bekend gegeven dat een lezer, tijdens het bekijken van de layout van een pagina, afgeleid wordt door de tekstuele inhoud. Het belangrijke punt van het gebruik van Lorem Ipsum is dat het uit een min of meer normale verdeling van letters bestaat, in tegenstelling tot "Hier uw tekst, hier uw tekst" wat het tot min of meer leesbaar nederlands maakt. Veel desktop publishing pakketten en web pagina editors gebruiken tegenwoordig Lorem Ipsum als hun standaard model tekst, en een zoekopdracht naar "lorem ipsum" ontsluit veel websites die nog in aanbouw zijn.Verscheidene versies hebben zich ontwikkeld in de loop van de jaren, soms per ongeluk soms expres (ingevoegde humor en dergelijke).</div><div class="category-text-right-column"><img src="http://dssprod.be/inesbouwen/wp-content/uploads/cat-image-necklace.jpg" alt="Necklaces page banner" /></div></div></div>';
  155.     }
  156. }
  157. function k2i_text_on_woocommerce_shop_page_mobile() {
  158.     if (is_shop()) {
  159.         echo '<div class="term-description-mobile">Hier komt de description</div>';
  160.     }
  161. }
  162. // add_action( 'woocommerce_before_shop_loop', 'k2i_text_on_woocommerce_shop_page_desktop');
  163. // add_action( 'woocommerce_after_shop_loop', 'k2i_text_on_woocommerce_shop_page_mobile');
  164.  
  165. /*
  166.  * Unset additional information tab
  167.  **/
  168. // function k2i_remove_product_tabs( $tabs ) {
  169. //     unset( $tabs['additional_information'] );
  170. //     return $tabs;
  171. // }
  172. // add_filter( 'woocommerce_product_tabs', 'k2i_remove_product_tabs' );
  173.  
  174. /**
  175.  * Custom Variable Product price
  176.  * Show the total price instead of a price-range
  177.  **/
  178. // function k2i_custom_min_max_variable_price_html( $price, $product ) {
  179. //     $prices      = $product->get_variation_prices( true );
  180. //     $min_price       = current( $prices['price'] );
  181. //     $max_price       = end( $prices['price'] );
  182.  
  183. //     $min_price_int   = (int)$min_price;
  184. //     $max_price_int   = (int)$max_price;
  185.  
  186. //     $total_price = $min_price_int + $max_price_int;
  187. //     $price = sprintf( __( '%1$s', 'woocommerce' ), wc_price($total_price) );
  188.  
  189. //     return $price;
  190. // }
  191. // add_filter( 'woocommerce_variable_price_html', 'k2i_custom_min_max_variable_price_html', 10, 2 );
  192.  
  193. /**
  194.  * Custom Add To Cart text for simple products
  195.  * Changes the URL & Text
  196.  **/
  197. function k2i_replace_loop_add_to_cart_button( $button, $product  ) {
  198.     if( $product->is_type( 'simple' ) ){
  199.         $button_text = __( "Read more", "woocommerce" );
  200.         $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
  201.     }
  202.  
  203.     return $button;
  204. }
  205. add_filter( 'woocommerce_loop_add_to_cart_link', 'k2i_replace_loop_add_to_cart_button', 30, 2 );
  206.  
  207. function k2i_add_subtitle_to_product_name( $product ){
  208.     global $product;
  209.     $id = $product->get_id();
  210.  
  211.     $the_meta = get_post_meta($id, '_product_sub_title');
  212.    
  213.     if($the_meta){
  214.         if(is_product()){
  215.             echo '<span class="single-product-sub-title">' . $the_meta[0] . '</span>';
  216.         } else {
  217.             $subtitle_with_url = '<a class="archive-product-sub-title" href="' . $product->get_permalink() . '">' . $the_meta[0] . '</a>';
  218.             echo $subtitle_with_url;
  219.         }
  220.     }
  221. }
  222. /* Add action to the product-archive page */
  223. add_action( 'woocommerce_shop_loop_item_title', 'k2i_add_subtitle_to_product_name' );
  224. /* Add action to the single product page */
  225. add_action( 'woocommerce_single_product_summary', 'k2i_add_subtitle_to_product_name', 6 );
  226.  
  227. function k2i_add_set_price( $product ){
  228.     global $product;
  229.     $id = $product->get_id();
  230.  
  231.     $the_set_price = get_post_meta($id, '_product_set_price');
  232.    
  233.     if($the_set_price){
  234.         if(is_product()){
  235.             echo '<span class="single-product-set-price">' . __( 'Set Price: ', 'woocommerce' )  . '<strong>€' . $the_set_price[0] . '</strong></span>';
  236.         } else {
  237.             $set_price_with_url = '<a class="archive-product-set-price" href="' . $product->get_permalink() . '">' . __( 'Set Price: €', 'woocommerce' ) . $the_set_price[0] . '</a>';
  238.             echo $set_price_with_url;
  239.         }
  240.     }
  241. }
  242. /* Add action to the product-archive page */
  243. add_action( 'woocommerce_shop_loop_item_title', 'k2i_add_set_price' );
  244. /* Add action to the single product page */
  245. add_action( 'woocommerce_single_product_summary', 'k2i_add_set_price', 6 );
  246.  
  247.  
  248. /**
  249.  * Add Yoast Bread Crumbs
  250.  **/
  251. function k2i_add_yoast_breadcrumbs() {
  252.     if ( function_exists('yoast_breadcrumb') ) {
  253.         yoast_breadcrumb( '
  254.             <div class="nm-row">
  255.                 <div class="col-xs-12">
  256.                     <div class="yoast-breadcrumbs">','</div>
  257.                 </div>
  258.             </div>
  259.             ' );
  260.     }
  261. }
  262. add_action( 'woocommerce_before_main_content', 'k2i_add_yoast_breadcrumbs' );
  263.  
  264. /*
  265.     * Rewrite the rules to make category & product base: shop
  266.     */
  267. function k2i_product_category_base_same_shop_base( $flash = false ){
  268.     add_rewrite_rule('^shop/([^/]+)/?$','index.php?product_cat=$matches[1]','top');
  269.     if ($flash == true) {
  270.         flush_rewrite_rules(false);
  271.     }
  272. }
  273. add_filter( 'init', 'k2i_product_category_base_same_shop_base');
  274.  
  275. function k2i_product_cat_same_shop_edit_success( $term_id, $taxonomy ) {
  276.     k2i_product_category_base_same_shop_base(true);
  277. }
  278. add_action( 'create_term', 'k2i_product_cat_same_shop_edit_success', 10, 2 );
  279.  
  280. // function k2i_add_text_to_shop() {
  281. //  if( is_shop() ) {
  282. //      echo '<div class="nm-row"><div class="col-xs-12>';
  283. //      echo 'My text here';
  284. //      echo '</div></div>';
  285. //  }
  286. // }
  287. // add_action( 'woocommerce_before_main_content', 'k2i_add_text_to_shop', 1 );
  288.  
  289. function language_selector(){
  290.     $languages = icl_get_languages('skip_missing=0&orderby=code');
  291.     if(!empty($languages)){
  292.         foreach($languages as $l){
  293.             if(!$l['active']) echo '<a href="'.$l['url'].'">';
  294.             echo '<img class="country-flag" src="'.$l['country_flag_url'].'" height="12" alt="'.$l['language_code'].'" width="18" />';
  295.             if(!$l['active']) echo '</a>';
  296.         }
  297.     }
  298. }
  299.  
  300. /* Flags but in opposite order */
  301. function language_selector_flags_opposite() {
  302.     $languages = icl_get_languages('skip_missing=0');
  303.     if(!empty($languages)){
  304.         foreach($languages as $l){
  305.             if(!$l['active']){
  306.                 echo '<a href="' . $l['url'] . '"> ' . $l['translated_name'] . ' <img class="country-flag" src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /></a>';
  307.             }
  308.         }
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement