Add remove shipping method when free shipping class is available WooCommerce

add_filter( ‘woocommerce_package_rates’,’hide_shipping_when_free_is_available’);

function hide_shipping_when_free_is_available( $rates, $package ) {
$freeship = array();
$flatship = array();
// Only modify rates if free_shipping is present
$total = WC()->cart->cart_contents_total;
if( $total >= 150 ) {
foreach ( $rates as $rate_id => $rate ) {
if ( ‘free_shipping’ === $rate->method_id ) {
$freeship[ $rate_id ] = $rate;
break;
}
}
foreach ( $rates as $rate_id => $rate ) {

if ( ‘flat_rate’ === $rate->method_id ) {
continue;

}
$flatship[ $rate_id ] = $rate;
}
return ! empty( $freeship ) ? $flatship : $rates;
}else{
return $rates;
}

}

woocommerce add js in payment gateway option

jQuery(function(){
    jQuery( 'body' )
    .on( 'updated_checkout', function() {
          usingGateway();

        jQuery('input[name="payment_method"]').change(function(){
            console.log("payment method changed");
              usingGateway();

        });
    });
});


function usingGateway(){
    console.log(jQuery("input[name='payment_method']:checked").val());
    if(jQuery('form[name="checkout"] input[name="payment_method"]:checked').val() == 'my_gateway'){
        console.log("Using my gateway");
        //Etc etc
    }else{
         console.log("Not using my gateway. Proceed as usual");
    }
}   

Add a surcharge to cart and checkout

function df_add_ticket_surcharge( $cart_object ) {

global $woocommerce;
$specialfeecat = 14; // category id for the special fee
$spfee = 0.00; // initialize special fee
$spfeeperprod = 15; //special fee per product

foreach ( $cart_object->cart_contents as $key => $value ) {

$proid = $value[‘product_id’]; //get the product id from cart
$quantiy = $value[‘quantity’]; //get quantity from cart
$itmprice = $value[‘data’]->price; //get product price

$terms = get_the_terms( $proid, ‘product_cat’ ); //get taxonamy of the prducts
if ( $terms && ! is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
$catid = $term->term_id;
if( 14 == $catid ) {
$spfee += ($quantiy * 15);
}elseif( 12 == $catid ) {
$spfee += ($quantiy * 10);
}elseif( 87 == $catid ) {
$spfee += ($quantiy * 15);
}
}
endif;
}

if($spfee > 0 ) {

$woocommerce->cart->add_fee( ‘Shipping Cost’, $spfee, true, ‘standard’ );
}

}

add_action( ‘woocommerce_cart_calculate_fees’, ‘df_add_ticket_surcharge’ );

How to get related product by category

<?php
/**
* Related Products
*

*/

if ( ! defined( ‘ABSPATH’ ) ) {
exit;
}

global $post, $product, $woocommerce_loop;

if ( is_singular(‘product’) ) {
$terms = wp_get_post_terms( $post->ID, ‘product_cat’ );

foreach ( $terms as $term ) $cats_array[] = $term->term_id;
$query_args = array( ‘orderby’ => ‘rand’, ‘post__not_in’ => array( $post->ID ), ‘posts_per_page’ => 4, ‘no_found_rows’ => 1, ‘post_status’ => ‘publish’, ‘post_type’ => ‘product’, ‘tax_query’ => array(
array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘id’,
‘terms’ => $cats_array
)));
$r = new WP_Query($query_args);
if ( $r->have_posts() ) : ?>

How to list all Categories, Sub-Categories and Products in WooCommerce

<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;

$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>

<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{

if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo "<ul class='category'><li>".$cat->name;
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty

);

$sub_cats = get_categories( $args2 );
if($sub_cats)
{

foreach($sub_cats as $sub_category)
{
echo "<ul class='subcategory'>";
if($sub_cats->$sub_category == 0)
{
echo "<li>".$sub_category->cat_name;

?>
<?php
/*echo "<pre>";
print_r($sub_category);
echo "</pre>";*/

$args = array( 'post_type' => 'product','product_cat' => $sub_category->slug);
$loop = new WP_Query( $args );
echo "<ul class='products'>";
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>
<?php wp_reset_query(); ?>

<?php
}
else
{
echo "</li></ul></li>";
}
echo "</ul>";
}
}
else
{
$args = array( 'post_type' => 'product', 'product_cat' => $cat->slug );
$loop = new WP_Query( $args );
echo "<ul class='products'>";
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul></li></ul>
<?php wp_reset_query();
}
}
else
{
echo "</li></ul>";
}
}
?>