Showing posts with label woocommerce. Show all posts
Showing posts with label woocommerce. Show all posts

Friday, June 5, 2015

Filters and hooks for modifying woocommerce

1. Add email recipient when order completed

function woo_extra_email_recipient($recipient, $object) {
    $recipient = $recipient . ', your@email.com';
    return $recipient; }

add_filter( 'woocommerce_email_recipient_customer_completed_order', 'woo_extra_email_recipient', 10, 2);

2.  Redirect add to cart button to checkout page

add_filter ('add_to_cart_redirect', 'redirect_to_checkout');

function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;

}

3. Add custom field to edit address page

function woo_add_edit_address_fields( $fields ) {
    $new_fields = array(
                'date_of_birth'     => array(
                'label'             => __( 'Date of birth', 'woocommerce' ),
                'required'          => false,
                'class'             => array( 'form-row' ),
            ),
        );      

    $fields = array_merge( $fields, $new_fields );    
    return $fields;  

}


add_filter( 'woocommerce_default_address_fields', 'woo_add_edit_address_fields' );

4. Change “from” email address

function woo_custom_wp_mail_from() {
        global $woocommerce;
        return html_entity_decode( 'your@email.com' );
}

add_filter( 'wp_mail_from', 'woo_custom_wp_mail_from', 99 );

5. Change shop page title

add_filter( 'woocommerce_page_title', 'woo_shop_page_title');

function woo_shop_page_title( $page_title ) {  

    if( 'Shop' == $page_title) {
        return "My new title";
    }

}

6. Rename a product tab

add_filter( 'woocommerce_product_tabs', 'woo_rename_tab', 98);

function woo_rename_tab($tabs) {
 $tabs['description']['title'] = 'More info';
 return $tabs;

}

7. Remove “Products” from breadcrumb

function woo_custom_filter_breadcrumbs_trail ( $trail ) {
  foreach ( $trail as $k => $v ) {
    if ( strtolower( strip_tags( $v ) ) == 'products' ) {
      unset( $trail[$k] );
      break;
    }
  }
  return $trail;

}

add_filter( 'woo_breadcrumbs_trail', 'woo_custom_filter_breadcrumbs_trail', 10 );

8. Empty cart

function my_empty_cart(){
    global $woocommerce;
    $woocommerce->cart->empty_cart();
}


add_action('init', 'my_empty_cart');

9. Change related products number

function woo_related_products_limit() {
  global $product;  

    $args = array(
        'post_type'             => 'product',
        'no_found_rows'         => 1,
        'posts_per_page'        => 6,
        'ignore_sticky_posts'   => 1,
        'orderby'               => $orderby,
        'post__in'              => $related,
        'post__not_in'          => array($product->id)
    );
    return $args;
}


add_filter( 'woocommerce_related_products_args', 'woo_related_products_limit' );

10. Remove breadcrumb

remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);



Friday, April 24, 2015

Customizing product search with meta key in wordpress woocommerce

Suppose we have a custom fields like price, color etc in woocommerce product. How we will include these fields in wordpress search? Below is the step to include these keywords in wordpress search.

Make an array like below for meta search.

$args = array(
'post_type'=>'product',
'posts_per_page' => -1,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'price',               //your meta key
'value' => $_GET['price'],  //your search post value
'compare' => '='
)
)
);

Instead of =, we can use LIKE ,> or < etc.

$the_query = new WP_Query( $args ); // make query in wordpress
$q1 = $the_query->get_posts(); get all product with search.

Make an array like below to include normal search in wordpress:

$args2 = array(
'post_type'=>'product',
's' => $_GET['s'],

);


$the_query2 = new WP_Query( $args2 );
$q2 = $the_query2->get_posts(); // get all product whose title are related to the fields value.

Now merge both the result :
$merged = array_merge( $q1, $q2 );

and get the post id of all product in a array, like below :

$post_ids = array();
foreach( $merged as $item ) {
$post_ids[] = $item->ID;
}

Now get the unique id from above array.
$unique = array_unique($post_ids);

Now again make a query with these ids

$args3 = array(
'post_type'=>'product',
'post__in' => $unique,
'post_status' => 'publish',

);
$the_query3 = new WP_Query( $args3 );

Now we can use normal wordpress function to get product detail:

<?php
if ( $the_query3->have_posts() ) {
while ( $the_query3->have_posts() ) : $the_query3->the_post();
// product detail goes here.
endwhile; // end of the loop.
} // end of if
?>