imnguyen

function.php backup

Feb 27th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.61 KB | None | 0 0
  1. <?php
  2.  
  3. /*** Child Theme Function ***/
  4.  
  5. function depot_mikado_child_theme_enqueue_scripts() {
  6. $parent_style = 'depot_mikado_default_style';
  7.  
  8. wp_enqueue_style($parent_style, get_template_directory_uri() . '/style.css');
  9.  
  10. wp_enqueue_style('depot_mikado_child_style',
  11. get_stylesheet_directory_uri() . '/style.css',
  12. array($parent_style),
  13. wp_get_theme()->get('Version')
  14. );
  15. }
  16.  
  17. add_action( 'wp_enqueue_scripts', 'depot_mikado_child_theme_enqueue_scripts' );
  18.  
  19.  
  20. // Register and load the widget
  21. function wwpb_load_widget() {
  22. register_widget( 'wwpb_widget' );
  23. }
  24. add_action( 'widgets_init', 'wwpb_load_widget' );
  25.  
  26. // Creating the widget
  27. class wwpb_widget extends WP_Widget {
  28.  
  29. function __construct() {
  30. parent::__construct(
  31.  
  32. // Base ID of your widget
  33. 'wwpb_widget',
  34.  
  35. // Widget name will appear in UI
  36. __('Add redemption codes', 'wwpb_widget_domain'),
  37.  
  38. // Widget description
  39. array( 'description' => __( 'Admin link to add redemption codes', 'wwpb_widget_domain' ), )
  40. );
  41. }
  42.  
  43. // Creating widget front-end
  44. public function widget( $args, $instance ) {
  45.  
  46. echo $args['before_widget']; ?>
  47.  
  48.  
  49.  
  50. <div class="mkd-shopping-cart-holder mkd-cart-info-is-active" style="display: table-cell; min-height: 97px;" >
  51.  
  52. <div class="mkd-shopping-cart">
  53.  
  54.  
  55. <a class="mkd-header-cart" href="<?php echo esc_url( home_url( '/product-category/admin/' ) ); ?>">
  56. <span class="mkd-cart-icon-text">REDEMPTION CODES</span>
  57. </a>
  58.  
  59.  
  60.  
  61.  
  62. </div>
  63.  
  64. </div>
  65.  
  66. <?php echo $args['after_widget'];
  67. }
  68.  
  69. // Widget Backend
  70. public function form( $instance ) {
  71. if ( isset( $instance[ 'title' ] ) ) {
  72. $title = $instance[ 'title' ];
  73. }
  74. else {
  75. $title = __( 'New title', 'wpb_widget_domain' );
  76. }
  77. // Widget admin form
  78. ?>
  79. <p>
  80. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  81. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  82. </p>
  83. <?php
  84. }
  85.  
  86. // Updating widget replacing old instances with new
  87. public function update( $new_instance, $old_instance ) {
  88. $instance = array();
  89. $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
  90. return $instance;
  91. }
  92. } // Class wpb_widget ends here
  93.  
  94.  
  95.  
  96. // Register and load the widget
  97. function wpb_load_widget() {
  98. register_widget( 'wpb_widget' );
  99. }
  100. add_action( 'widgets_init', 'wpb_load_widget' );
  101.  
  102. // Creating the widget
  103. class wpb_widget extends WP_Widget {
  104.  
  105. function __construct() {
  106. parent::__construct(
  107.  
  108. // Base ID of your widget
  109. 'wpb_widget',
  110.  
  111. // Widget name will appear in UI
  112. __('Redemption Codes', 'wpb_widget_domain'),
  113.  
  114. // Widget description
  115. array( 'description' => __( 'Display redemption codes', 'wpb_widget_domain' ), )
  116. );
  117. }
  118.  
  119. // Creating widget front-end
  120. public function widget( $args, $instance ) {
  121.  
  122. echo $args['before_widget']; ?>
  123.  
  124.  
  125.  
  126. <div class="mkd-shopping-cart-holder mkd-cart-info-is-active" style="padding: 35px;">
  127.  
  128. <div class="mkd-shopping-cart">
  129.  
  130.  
  131. <a class="mkd-header-cart" href="<?php echo esc_url( home_url( '/' ) ); ?>">
  132. <span class="mkd-cart-icon-text">MY ACCOUNT</span>
  133. </a>
  134.  
  135.  
  136.  
  137. <div class="mkd-shopping-cart-dropdown">
  138.  
  139. <ul id="nav_voucher">
  140. <?php do_action("giftbal"); ?>
  141. </ul>
  142.  
  143. <div class="mkd-cart-bottom" style="padding: 0px 25px 20px 25px; height: auto;">
  144.  
  145.  
  146.  
  147. <div class="mkd-btn-holder clearfix" style="margin-top: -15px !important;">
  148.  
  149. <a itemprop="url" href="<?php echo esc_url( home_url( '/customer-logout' ) ); ?>" class="mkd-view-cart" data-title="LOGOUT" style="border: 1px solid #7b7b7b;"><span>LOGOUT</span></a>
  150. </div>
  151.  
  152. </div>
  153.  
  154.  
  155.  
  156.  
  157. </div>
  158. </div>
  159.  
  160. </div>
  161.  
  162. <?php echo $args['after_widget'];
  163. }
  164.  
  165. // Widget Backend
  166. public function form( $instance ) {
  167. if ( isset( $instance[ 'title' ] ) ) {
  168. $title = $instance[ 'title' ];
  169. }
  170. else {
  171. $title = __( 'New title', 'wpb_widget_domain' );
  172. }
  173. // Widget admin form
  174. ?>
  175. <p>
  176. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  177. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  178. </p>
  179. <?php
  180. }
  181.  
  182. // Updating widget replacing old instances with new
  183. public function update( $new_instance, $old_instance ) {
  184. $instance = array();
  185. $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
  186. return $instance;
  187. }
  188. } // Class wpb_widget ends here
  189.  
  190.  
  191.  
  192.  
  193.  
  194. function authorize_less($available_gateways) {
  195. $maximum = 0;
  196. if ( WC()->cart->total < $maximum ) {
  197. unset( $available_gateways['authorizenet'] );
  198. }
  199. return $available_gateways;
  200. }
  201. add_filter( 'woocommerce_available_payment_gateways', 'authorize_less');
  202. add_action( 'woocommerce_cart_calculate_fees' , 'authorize_less' );
  203. add_action( 'woocommerce_after_cart_item_quantity_update', 'authorize_less' );
  204.  
  205.  
  206.  
  207. function authorize_more($available_gateways) {
  208. $maximum = 0;
  209. if ( WC()->cart->total > $maximum ) {
  210. unset( $available_gateways['cod'] );
  211. }
  212. return $available_gateways;
  213. }
  214. add_filter( 'woocommerce_available_payment_gateways', 'authorize_more');
  215. add_action( 'woocommerce_cart_calculate_fees' , 'authorize_more' );
  216. add_action( 'woocommerce_removed_coupon', 'authorize_more' );
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224. add_action('pre_get_posts', 'wpse223576_search_woocommerce_only');
  225.  
  226. function wpse223576_search_woocommerce_only( $query ) {
  227. if( ! is_admin() && is_search() && $query->is_main_query() ) {
  228. $query->set( 'post_type', 'product' );
  229. }
  230. }
  231.  
  232.  
  233. add_action('init', 'woocommerce_clear_cart_url');
  234. function woocommerce_clear_cart_url() {
  235. global $woocommerce;
  236. if( isset($_REQUEST['clear-cart']) ) {
  237. $woocommerce->cart->empty_cart();
  238. }
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246. function iconic_bypass_logout_confirmation() {
  247. global $wp;
  248.  
  249. if ( isset( $wp->query_vars['customer-logout'] ) ) {
  250. wp_redirect( str_replace( '&amp;', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) );
  251. exit;
  252. }
  253. }
  254.  
  255. add_action( 'template_redirect', 'iconic_bypass_logout_confirmation' );
  256.  
  257.  
  258.  
  259. function cart_update_script() {
  260. if (is_checkout()) :
  261. ?>
  262. <script>
  263. jQuery( function( $ ) {
  264.  
  265. // woocommerce_params is required to continue, ensure the object exists
  266. if ( typeof woocommerce_params === 'undefined' ) {
  267. return false;
  268. }
  269.  
  270. $checkout_form = $( 'form.checkout' );
  271.  
  272. $checkout_form.on( 'change', 'input[name="payment_method"]', function() {
  273. $checkout_form.trigger( 'update' );
  274. });
  275.  
  276.  
  277. });
  278. </script>
  279. <?php
  280. endif;
  281. }
  282. add_action( 'wp_footer', 'cart_update_script', 999 );
  283.  
  284.  
  285.  
  286.  
  287.  
  288. // WooCommerce Rename Checkout Fields
  289. add_filter( 'woocommerce_checkout_fields' , 'custom_rename_wc_checkout_fields' );
  290.  
  291. // Change placeholder and label text
  292. function custom_rename_wc_checkout_fields( $fields ) {
  293. $fields['shipping']['shipping_address_2']['label'] = '<i style="font-size: 12px;">For proper delivery, PO Box not accepted</i>';
  294. return $fields;
  295. }
  296.  
  297.  
  298. // rename the "Have a Coupon?" message on the checkout page
  299. function woocommerce_rename_coupon_message_on_checkout() {
  300. return 'Have a redemption code?' . ' <a href="#" class="showcoupon">' . __( 'Click here to enter your code', 'woocommerce' ) . '</a>';
  301. }
  302. add_filter( 'woocommerce_checkout_coupon_message', 'woocommerce_rename_coupon_message_on_checkout' );
  303. // rename the coupon field on the checkout page
  304. function woocommerce_rename_coupon_field_on_checkout( $translated_text, $text, $text_domain ) {
  305. // bail if not modifying frontend woocommerce text
  306. if ( is_admin() || 'woocommerce' !== $text_domain ) {
  307. return $translated_text;
  308. }
  309. if ( 'Coupon code' === $text ) {
  310. $translated_text = 'Redemption Code';
  311.  
  312. } elseif ( 'Apply Coupon' === $text ) {
  313. $translated_text = 'Apply Code';
  314. }
  315. return $translated_text;
  316. }
  317. add_filter( 'gettext', 'woocommerce_rename_coupon_field_on_checkout', 10, 3 );
  318.  
  319.  
  320.  
  321. function my_wp_nav_menu_args( $args = '' ) {
  322.  
  323. if( is_user_logged_in() ) {
  324. $args['menu'] = 'main-menu-header';
  325. } else {
  326. $args['menu'] = 'main';
  327. }
  328. return $args;
  329. }
  330. add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
  331.  
  332.  
  333.  
  334. add_action( 'woocommerce_order_status_processing', 'coupon_email_restriction_on_purchase', 20, 2 );
  335. function coupon_email_restriction_on_purchase( $order_id, $order ) {
  336. $applied_coupons = $order->get_used_coupons();
  337. if( count( $applied_coupons ) == 0 )
  338. return; // Exit if there is no coupons
  339.  
  340. // Get the Customer billing email
  341. $customer_email = $order->get_billing_email();
  342.  
  343. foreach( $applied_coupons as $coupon_code ){
  344. // Get an instance of the WC_Coupon object
  345. $coupon = new WC_Coupon( $coupon_code );
  346.  
  347. // Get email restrictions (even if is an empty array)
  348. $email_restrictions = $coupon->get_email_restrictions();
  349. // Add the customer email to the restrictions array
  350. $email_restrictions[] = $customer_email;
  351. // set the new array of email restrictions
  352. $coupon->set_email_restrictions( $email_restrictions );
  353.  
  354. // Save the coupon data
  355. $coupon->save();
  356. }
  357. }
  358.  
  359.  
  360. add_filter( 'woocommerce_cart_subtotal', 'bbloomer_slash_cart_subtotal_if_discount', 99, 3 );
  361.  
  362. function bbloomer_slash_cart_subtotal_if_discount( $cart_subtotal, $compound, $obj ){
  363. global $woocommerce;
  364. if ( WC()->cart->total > 0 ) {
  365. $new_cart_subtotal = wc_price( WC()->cart->subtotal - $woocommerce->cart->get_cart_discount_tax_total() - $woocommerce->cart->get_cart_discount_total() );
  366. $cart_subtotal = sprintf( '<del>%s</del> <b>%s</b>', $cart_subtotal , $new_cart_subtotal );
  367. }
  368. return $cart_subtotal;
  369.  
  370. }
  371.  
  372.  
  373.  
  374.  
  375.  
  376. function wpse_131562_redirect() {
  377. if (
  378. ! is_user_logged_in()
  379. && (is_woocommerce() || is_cart() || is_checkout())
  380. ) {
  381. // feel free to customize the following line to suit your needs
  382. wp_redirect(home_url());
  383. exit;
  384. }
  385. }
  386. add_action('template_redirect', 'wpse_131562_redirect');
  387.  
  388.  
  389.  
  390. if (!function_exists('depot_mikado_woocommerce_cart_back_to_home')) {
  391. function depot_mikado_woocommerce_cart_back_to_home(){
  392. print '<a class="mkd-cart-go-back" itemprop="url" href="' . esc_url(home_url('/shop')) . '">' . esc_html__('Continue shopping', 'depot') . '</a>';
  393. }
  394. }
  395.  
  396.  
  397.  
  398.  
  399.  
  400. function wooc_extra_register_fields() {
  401. ?>
  402.  
  403. <p class="form-row form-row-wide">
  404. <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?> <span class="required">*</span></label>
  405. <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="" />
  406.  
  407.  
  408.  
  409. <?php
  410. }
  411.  
  412. add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
  413. add_action( 'woocommerce_edit_account_form', 'wooc_extra_register_fields');
  414.  
  415.  
  416.  
  417. /**
  418. * Validate the extra register fields.
  419. *
  420. * @param string $username Current username.
  421. * @param string $email Current email.
  422. * @param object $validation_errors WP_Error object.
  423. *
  424. * @return void
  425. */
  426. function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
  427.  
  428.  
  429. if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
  430. $validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone is required!.', 'woocommerce' ) );
  431. }
  432. }
  433.  
  434. add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
  435. add_action( 'woocommerce_edit_account_form', 'wooc_validate_extra_register_fields', 10, 3 );
  436.  
  437. /**
  438. * Save the extra register fields.
  439. *
  440. * @param int $customer_id Current customer ID.
  441. *
  442. * @return void
  443. */
  444. function wooc_save_extra_register_fields( $customer_id ) {
  445.  
  446.  
  447. if ( isset( $_POST['billing_phone'] ) ) {
  448. // WooCommerce billing phone
  449. update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
  450. }
  451. }
  452.  
  453. add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
  454. add_action( 'woocommerce_save_account_details', 'wooc_save_extra_register_fields' );
  455.  
  456.  
  457.  
  458. function custom_scripts() {
  459. wp_enqueue_script( 'cust-js', get_stylesheet_directory_uri() . '/custom.js', array('jquery'), '20180101', true );
  460. }
  461. // add_action( 'wp_enqueue_scripts', 'custom_scripts' );
  462.  
  463.  
  464.  
  465. // @to-do potential starting point for tax work below commented out
  466. // https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html
  467. //
  468. // We want to apply taxable percentage to only remaining ammount if a
  469. // coupon is used on a transaction.
  470. // 1) Get coupon\s total
  471. // 2) Get subtotal
  472. // 3) Adjust new total to be minus coupon ammount
  473. // 4) If a positive value tax that amount
  474. // 5) Update/return
  475. //
  476. //add_action( 'woocommerce_calculate_totals', 'custom_cart_calculate_totals', 10, 1 );
  477. function custom_cart_calculate_totals( $cart_object ) {
  478.  
  479. if ( !WC()->cart->is_empty() ){
  480. // check for whatever flags the taxes to be applied
  481. $flagForTaxes = true; // set to true for testing
  482. if($flagForTaxes){
  483. // get total of coupons
  484. $coupons = WC()->cart->get_coupon_discount_totals();
  485. $subtotal = WC()->cart->get_subtotal();
  486. $shipping = WC()->cart->get_shipping_total();
  487. // $total = ($subtotal - $coupons);
  488. // WC()->cart->set_total(1.11);
  489. // return $total;
  490. }
  491. }
  492. }
  493.  
  494.  
  495.  
  496.  
  497.  
  498. $new_cart_subtotal = wc_price( WC()->cart->subtotal - $woocommerce->cart->get_cart_discount_total() );
  499.  
  500.  
  501.  
  502.  
  503. //add_filter( 'woocommerce_cart_total', 'bbloomer_slash_cart_subtotal_if_discount', 99, 3 );
  504.  
  505. function bbloomer_slash_cart_subtotal_if_discount( $cart_subtotal, $compound, $obj ){
  506. global $woocommerce;
  507. if ( WC()->cart->total > 0 ) {
  508. $new_cart_subtotal = $woocommerce->cart->subtotal - $woocommerce->cart->get_cart_discount_total() ;
  509. $cart_subtotal = sprintf( '<del>%s</del> <b>%s</b>', $cart_subtotal , $new_cart_subtotal );
  510. }
  511. return $cart_subtotal;
  512. }
  513.  
  514.  
  515.  
  516. add_filter( 'woocommerce_cart_total', 'bbloomer_slash_cart_subtotal_if_discount', 99, 3 );
  517.  
  518. function bbloomer_slash_cart_subtotal_if_discount( $cart_subtotal, $compound, $obj ){
  519. global $woocommerce;
  520. if ( !WC()->cart->is_empty() ) {
  521. $new_cart_subtotal = $woocommerce->cart->subtotal - $woocommerce->cart->get_cart_discount_total() ;
  522. $cart_total = $new_cart_subtotal;
  523. }
  524. return $cart_total;
  525. }
  526.  
  527.  
  528.  
  529.  
  530. function authorize_more($available_gateways) {
  531. $maximum = 0;
  532. if ( WC()->cart->total > $maximum ) {
  533. unset( $available_gateways['cod'] );
  534. }
  535. return $available_gateways;
  536. }
  537. add_filter( 'woocommerce_available_payment_gateways', 'authorize_more');
  538. add_action( 'woocommerce_cart_calculate_fees' , 'authorize_more' );
  539. add_action( 'woocommerce_removed_coupon', 'authorize_more' );
  540.  
  541. function authorize_less($available_gateways) {
  542. $maximum = 0;
  543. if ( WC()->cart->total < $maximum ) {
  544. unset( $available_gateways['authorizenet'] );
  545. }
  546. return $available_gateways;
  547. }
  548. add_filter( 'woocommerce_available_payment_gateways', 'authorize_less');
  549. add_action( 'woocommerce_cart_calculate_fees' , 'authorize_less' );
  550. add_action( 'woocommerce_after_cart_item_quantity_update', 'authorize_less' );
  551.  
  552.  
  553.  
  554.  
  555.  
  556. add_filter( 'woocommerce_cart_total', 'bbloomer_slash_cart_subtotal_if_discount', 99, 3 );
  557.  
  558. function bbloomer_slash_cart_subtotal_if_discount( $cart_subtotal, $compound, $obj ){
  559. global $woocommerce;
  560. if ( !WC()->cart->is_empty() ) {
  561. $new_cart_subtotal = $woocommerce->cart->subtotal - $woocommerce->cart->get_cart_discount_total();
  562. $cart_total = $woocommerce->cart->get_cart_discount_total();
  563. }
  564. return $cart_total;
  565. }
Add Comment
Please, Sign In to add comment