bux23
2/28/2017 - 9:30 AM

woocoommerce snippets

woocoommerce snippets

<?php

//////////////////////////////////////////////////////////////
// WOO COMMERCE SNIPPETS
//////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////
// PAYMENT TYPE IN MAIL
///////////////////////////////////////////////////////////
add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );
 
function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {
  if ( $is_admin_email ) {
    echo '<p><strong>Metodo di pagamento:</strong> ' . $order->payment_method_title . '</p>';
  }
}

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {
 
	if ( ! $q->is_main_query() ) return;
	if ( ! $q->is_post_type_archive() ) return;
	
	if ( ! is_admin() && is_shop() && ! is_user_logged_in() ) {
 
		$q->set( 'tax_query', array(array(
			'taxonomy' => 'product_cat',
			'field' => 'slug',
			'terms' => array( 'color', 'flavor', 'spices', 'vanilla' ), // Don't display products in these categories on the shop page
			'operator' => 'NOT IN'
		)));
	
	}
 
	remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
 
}

///////////////////////////////////////////////////////////
// CUSTOM ADD TO CART BUTTON TEXT
///////////////////////////////////////////////////////////
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' );    // 2.1 +
 
function woo_archive_custom_cart_button_text() {
 
        return __( 'custom text', 'woocommerce' );
 
}

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );    // 2.1 +
 
function woo_custom_cart_button_text() {
 
        return __( 'custom text', 'woocommerce' );
 
}

///////////////////////////////////////////////////////////
// CUSTOM CHECKOUT BUTTON
///////////////////////////////////////////////////////////
function woocommerce_button_proceed_to_checkout() {
   $checkout_url = WC()->cart->get_checkout_url();
   ?>
   <a href="<?php echo $checkout_url; ?>" class="checkout-button button alt wc-forward"><?php _e( 'custom text', 'woocommerce' ); ?></a>
   <?php
}

///////////////////////////////////////////////////////////
// CUSTOM CURRENCY SYMBOL
///////////////////////////////////////////////////////////
add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2);

function add_my_currency_symbol( $currency_symbol, $currency ) {
     switch( $currency ) {
          case 'MYC': $currency_symbol = 'custom name'; break;
     }
     return $currency_symbol;
}

///////////////////////////////////////////////////////////
// REMOVE CHECKOUT DETAIL FIELDS
///////////////////////////////////////////////////////////
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
 
function custom_override_checkout_fields( $fields ) {
    unset($fields['billing']['billing_first_name']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_address_1']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_state']);
    unset($fields['billing']['billing_phone']);
    unset($fields['order']['order_comments']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_email']);
    unset($fields['billing']['billing_city']);
    return $fields;
}

///////////////////////////////////////////////////////////
// AUTO COMPLETE ORDERS
///////////////////////////////////////////////////////////
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

///////////////////////////////////////////////////////////
// ADD TAB TO ORDER DETAILS CHECKOUT
///////////////////////////////////////////////////////////
add_action( 'woocommerce_order_details_after_order_table', "custom_tab_checkout", 10, 1 );

function custom_tab_checkout() {
    echo '<div class="wc-extra-checkout-field">CUSTOM TAB CONTENT</div>';
}

///////////////////////////////////////////////////////////
// ADD TAB TO CART PAGE
///////////////////////////////////////////////////////////
add_action( 'woocommerce_after_cart_table', "custom_tab_cart", 10, 1 );

function custom_tab_cart() {
    echo '<div class="wc-extra-checkout-field">CUSTOM TAB CONTENT</div>';
}

///////////////////////////////////////////////////////////
// BINDED EMAIL COUPON COLUMN
///////////////////////////////////////////////////////////
add_filter( 'manage_shop_coupon_posts_columns', 'show_binded_email',15 );
function show_binded_email($columns){

   //add column
   $columns['Emails'] = __( 'Emails'); 

   return $columns;
}

function woo_populate_binded_email_col($column, $post_id) {
  if ( $column == 'Emails' ) {
        $emails = get_post_meta( $post_id, 'customer_email' );
        $output = "";
        $mail_num = 0;
      foreach($emails as $email) {
          $mail_num == 0 ? $output .= $email : $output .= ', '.$email;
      }
        echo $output;
    }
}
add_action("manage_shop_coupon_posts_custom_column", "woo_populate_binded_email_col", 10, 2);

?>