Categories
WooCommerce

WooCommerce – Hide trailing zeros on prices

<?php
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
Categories
WooCommerce WordPress

WooCommerce – How to add custom tracking code for the thank you page

<?php
/**
 * Add custom tracking code to the thank-you page
 */
add_action( 'woocommerce_thankyou', 'cs_custom_tracking' );

function cs_custom_tracking( $order_id ) {

	// Lets grab the order
	$order = wc_get_order( $order_id );

	/**
	 * Put your tracking code here
	 * You can get the order total etc e.g. $order->get_total();
	 */
	 
	// This is the order total
	$order->get_total();
 
	// This is how to grab line items from the order 
	$line_items = $order->get_items();

	// This loops over line items
	foreach ( $line_items as $item ) {
  		// This will be a product
  		$product = $order->get_product_from_item( $item );
  
  		// This is the products SKU
		$sku = $product->get_sku();
		
		// This is the qty purchased
		$qty = $item['qty'];
		
		// Line item total cost including taxes and rounded
		$total = $order->get_line_total( $item, true, true );
		
		// Line item subtotal (before discounts)
		$subtotal = $order->get_line_subtotal( $item, true, true );
	}
}
Categories
WooCommerce

WooCommerce – Exclude products from a particular category on the shop page

/**
 * Exclude products from a particular category on the shop page
 */
function cs_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'clothing' ), // Don't display products in the clothing category on the shop page.
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}
add_action( 'woocommerce_product_query', 'cs_pre_get_posts_query' ); 
Categories
WooCommerce WordPress

Change “Add to Cart” text on single product page

<?php

function chetan_custom_cart_button_text() {

    return __( 'My Button Text', 'chetan' );

}

add_filter( 'add_to_cart_text', 'chetan_custom_cart_button_text' ); // WooCommerce < 2.1
add_filter( 'woocommerce_product_single_add_to_cart_text', 'chetan_custom_cart_button_text' ); // WooCommerce 2.1 +
Categories
WooCommerce WordPress

WooCommerce – Automatically mark orders as “Complete”

<?php

function chetan_order_payment_complete_order_status( $order_status, $order_id ) {
  $order = new WC_Order( $order_id );
  
  if ( 'processing' === $order_status && ( 'on-hold' === $order->status || 'pending' === $order->status || 'failed' === $order->status ) ) {
    
    $is_virtual_order = null;
    if ( count( $order->get_items() ) > 0 ) {
      
      foreach( $order->get_items() as $item ) {
        
        if ( 'line_item' == $item['type'] ) {
          $_product = $order->get_product_from_item( $item );
          
          if ( ! $_product->is_virtual() ) {
            // once we've found one non-virtual product we know we're done, break out of the loop
            $is_virtual_order = false;
            break;
          } else {
            $is_virtual_order = true;
          }
          
        }
        
      }
      
    }
    
   if ( $is_virtual_order ) {
      return 'completed';
    }
    
  }

  return $order_status;
  
}

add_filter( 'woocommerce_payment_complete_order_status', 'chetan_order_payment_complete_order_status', 10, 2 );
Categories
WooCommerce WordPress

Remove Specific Product Tabs

<?php

function chet_remove_product_tabs( $tabs ) {

    unset( $tabs['reviews'] );
    
    return $tabs;

}

add_filter( 'woocommerce_product_tabs', 'chet_remove_product_tabs', 99 );
Categories
Imagick PHP WordPress

Imagick – Generate Preview Image of PDF

<?php

$file          = get_attached_file( $attachment_id );
$output_format = "jpeg";
$antialiasing  = "4";
$preview_page  = "1";
$resolution    = "300";
$output_file   = 'srv/www/chetan/doc/' . $attachment_id . '_imagick_preview.jpg';
$exec_command  = "gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=" . $output_format . " ";
$exec_command  .= "-dTextAlphaBits=" . $antialiasing . " -dGraphicsAlphaBits=" . $antialiasing . " ";
$exec_command  .= "-dFirstPage=" . $preview_page . " -dLastPage=" . $preview_page . " ";
$exec_command  .= "-r" . $resolution . " ";
$exec_command  .= "-sOutputFile=" . $output_file . " '" . $file . "'";

exec( $exec_command, $command_output, $return_val );
Categories
BuddyPress WordPress

BuddyPress – Get Group Object


<?php

// Get group object.
$args  = array(
	'group_id' => $current_group_id,
);
$group = groups_get_group( $args );

Categories
Uncategorized WordPress

Get WordPress post content by post id

$my_postid = 12;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
echo get_post_field('post_content', $post_id);
Categories
WooCommerce WordPress

WooCommerce – Update number of items in cart and total after Ajax

add_filter('add_to_cart_fragments', 'chet_tips_woocommerce_header_add_to_cart_fragment');

function chet_tips_woocommerce_header_add_to_cart_fragment( $fragments ) {
  global $woocommerce;

  ob_start();

  ?>
  <span class="cart-count"><?php echo $woocommerce->cart->cart_contents_count; ?></span>
  <?php

  $fragments['span.cart-count'] = ob_get_clean();

  return $fragments;

}