WooCommerce
Free e-commerce plugin for WordPress. Combines WordPress flexibility with e-commerce functionality.
CMS
WooCommerce
Overview
WooCommerce is a free e-commerce plugin for WordPress. Combining WordPress's flexibility with e-commerce functionality, it's a popular e-commerce solution for small and medium businesses.
Details
WooCommerce is an open-source e-commerce plugin for WordPress, released in 2011. Provided under the GPL v3 license, it's used on over 5 million sites. 28% of WordPress sites use WooCommerce, achieving a 22% market share. Developed in PHP, it maximizes the use of WordPress's plugin system and theme features. The core plugin is free, with functionality extendable through numerous extensions. It covers all essential e-commerce features including payment gateways, shipping methods, inventory management, and SEO optimization. Combining WordPress's powerful content management capabilities allows easy creation of sites integrating blogs and e-commerce. The latest HPOS (High-Performance Order Storage) system enables high performance even for large-scale sites.
Pros and Cons
Pros
- Completely Free: Core plugin available at no cost
- High Customizability: Free to edit and extend PHP code
- WordPress Compatibility: Easy to add to existing WordPress sites
- Large Community: Active forums and extensive documentation
- SEO Ready: Leverages WordPress's excellent SEO features
- Rich Themes: Thousands of WooCommerce-compatible themes available
- Extensibility: Flexible feature extension through hooks and filters
Cons
- Technical Knowledge Required: PHP knowledge needed for setup and customization
- Security Management: Security measures are user's responsibility
- Performance Optimization: Optimization essential for large sites
- Limited Support: No official support for free version
- Hosting Costs: Need to provide your own server
Key Links
- WooCommerce Official Site
- WooCommerce Documentation
- WooCommerce GitHub Repository
- WooCommerce REST API
- WooCommerce Extensions
- WooCommerce Community
Usage Examples
Basic WooCommerce Setup
// functions.php - Enable WooCommerce support
add_action( 'after_setup_theme', 'mytheme_add_woocommerce_support' );
function mytheme_add_woocommerce_support() {
// Declare WooCommerce support
add_theme_support( 'woocommerce' );
// Enable product gallery features
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
// Add custom product tabs
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
// Add custom tab
$tabs['custom_tab'] = array(
'title' => __( 'Specifications', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_custom_tab_content'
);
return $tabs;
}
function woo_custom_tab_content() {
// Display tab content
echo '<h2>Product Specifications</h2>';
echo '<p>Product detailed specifications go here.</p>';
}
Adding Custom Product Fields
// Add custom fields to product edit screen
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_fields' );
function woo_add_custom_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Text field
woocommerce_wp_text_input(
array(
'id' => '_custom_product_code',
'label' => __( 'Product Code', 'woocommerce' ),
'placeholder' => 'ABC-123',
'desc_tip' => 'true',
'description' => __( 'Enter custom product code.', 'woocommerce' )
)
);
// Select box
woocommerce_wp_select(
array(
'id' => '_product_condition',
'label' => __( 'Product Condition', 'woocommerce' ),
'options' => array(
'new' => __( 'New', 'woocommerce' ),
'used' => __( 'Used', 'woocommerce' ),
'refurbished' => __( 'Refurbished', 'woocommerce' ),
)
)
);
echo '</div>';
}
// Save custom fields
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_fields' );
function woo_save_custom_fields( $post_id ) {
// Save product code
$custom_product_code = $_POST['_custom_product_code'];
if( !empty( $custom_product_code ) ) {
update_post_meta( $post_id, '_custom_product_code', esc_attr( $custom_product_code ) );
}
// Save product condition
$product_condition = $_POST['_product_condition'];
if( !empty( $product_condition ) ) {
update_post_meta( $post_id, '_product_condition', esc_attr( $product_condition ) );
}
}
Using WooCommerce REST API
// Customize REST API endpoints
add_action( 'rest_api_init', function () {
// Register custom endpoint
register_rest_route( 'wc-custom/v1', '/products/bestsellers', array(
'methods' => 'GET',
'callback' => 'get_bestselling_products',
'permission_callback' => '__return_true',
) );
} );
function get_bestselling_products( $request ) {
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 10,
'order' => 'DESC',
);
$products = new WP_Query( $args );
$data = array();
if ( $products->have_posts() ) {
while ( $products->have_posts() ) {
$products->the_post();
$product = wc_get_product( get_the_ID() );
$data[] = array(
'id' => $product->get_id(),
'name' => $product->get_name(),
'price' => $product->get_price(),
'sales' => $product->get_total_sales(),
'image' => wp_get_attachment_url( $product->get_image_id() ),
'permalink' => $product->get_permalink(),
);
}
}
wp_reset_postdata();
return new WP_REST_Response( $data, 200 );
}
Cart Functionality Customization
// Implement auto-update cart
add_action( 'wp_footer', 'cart_update_qty_script' );
function cart_update_qty_script() {
if ( is_cart() ) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Auto-update on quantity change
$('.woocommerce').on('change', 'input.qty', function() {
var $button = $(this).closest('form').find('[name="update_cart"]');
$button.prop('disabled', false);
$button.trigger('click');
});
});
</script>
<?php
}
}
// Add custom shipping method
add_action( 'woocommerce_shipping_init', 'custom_shipping_method_init' );
function custom_shipping_method_init() {
class WC_Custom_Shipping_Method extends WC_Shipping_Method {
public function __construct() {
$this->id = 'custom_shipping';
$this->method_title = __( 'Custom Shipping', 'woocommerce' );
$this->method_description = __( 'Custom shipping method description', 'woocommerce' );
$this->init();
}
function init() {
// Initialize settings form
$this->init_form_fields();
$this->init_settings();
// Save settings
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
public function calculate_shipping( $package = array() ) {
// Shipping cost calculation logic
$cost = 0;
$weight = 0;
foreach ( $package['contents'] as $item_id => $values ) {
$_product = $values['data'];
$weight += $_product->get_weight() * $values['quantity'];
}
// Weight-based shipping
if ( $weight <= 1 ) {
$cost = 5;
} elseif ( $weight <= 5 ) {
$cost = 8;
} else {
$cost = 12;
}
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost,
);
$this->add_rate( $rate );
}
}
}
add_filter( 'woocommerce_shipping_methods', 'add_custom_shipping_method' );
function add_custom_shipping_method( $methods ) {
$methods['custom_shipping'] = 'WC_Custom_Shipping_Method';
return $methods;
}
Order Processing Customization
// Custom action on order completion
add_action( 'woocommerce_thankyou', 'custom_order_complete_action', 10, 1 );
function custom_order_complete_action( $order_id ) {
if ( ! $order_id ) return;
$order = wc_get_order( $order_id );
// Notify inventory management system
$order_data = array(
'order_id' => $order->get_id(),
'order_total' => $order->get_total(),
'customer_id' => $order->get_customer_id(),
'items' => array(),
);
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
$order_data['items'][] = array(
'product_id' => $product->get_id(),
'product_name' => $product->get_name(),
'quantity' => $item->get_quantity(),
'price' => $item->get_total(),
);
}
// Send to external API (example)
wp_remote_post( 'https://api.example.com/orders', array(
'body' => json_encode( $order_data ),
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer YOUR_API_KEY',
),
) );
// Send custom email
$to = $order->get_billing_email();
$subject = 'Thank you for your order at ' . get_bloginfo( 'name' );
$message = 'Order number: #' . $order_id . "\n";
$message .= 'You can view your order details from your account page.';
wp_mail( $to, $subject, $message );
}
Using Hooks and Filters
// Customize product price display
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ) {
if ( $product->is_on_sale() ) {
$price = '<span class="sale-badge">SALE!</span> ' . $price;
}
// Member pricing
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
if ( in_array( 'premium_member', $user->roles ) ) {
$member_price = $product->get_price() * 0.9; // 10% discount
$price .= '<br><span class="member-price">Member price: ' . wc_price( $member_price ) . '</span>';
}
}
return $price;
}
// Customize checkout fields
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields' );
function custom_checkout_fields( $fields ) {
// Add delivery date field
$fields['order']['delivery_date'] = array(
'type' => 'date',
'label' => __('Delivery Date', 'woocommerce'),
'placeholder' => __('Select delivery date', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
);
$fields['order']['delivery_time'] = array(
'type' => 'select',
'label' => __('Delivery Time', 'woocommerce'),
'options' => array(
'' => __('No preference', 'woocommerce'),
'morning' => __('Morning', 'woocommerce'),
'12-14' => __('12:00-14:00', 'woocommerce'),
'14-16' => __('14:00-16:00', 'woocommerce'),
'16-18' => __('16:00-18:00', 'woocommerce'),
'18-20' => __('18:00-20:00', 'woocommerce'),
'19-21' => __('19:00-21:00', 'woocommerce'),
),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
);
return $fields;
}
// Save checkout fields
add_action( 'woocommerce_checkout_create_order', 'save_custom_checkout_fields', 10, 2 );
function save_custom_checkout_fields( $order, $data ) {
if ( ! empty( $_POST['delivery_date'] ) ) {
$order->update_meta_data( '_delivery_date', sanitize_text_field( $_POST['delivery_date'] ) );
}
if ( ! empty( $_POST['delivery_time'] ) ) {
$order->update_meta_data( '_delivery_time', sanitize_text_field( $_POST['delivery_time'] ) );
}
}
These examples demonstrate the major development patterns of WooCommerce. From WordPress integration, feature extension through hooks and filters, REST API usage, adding custom fields, to shipping method customization, they cover a wide range of development scenarios.