25 Cuplikan WooCommerce Terbaik Saya Untuk WordPress Bagian 2


  1. 1. 25 Cuplikan WooCommerce Terbaik Saya Untuk WordPress
  2. 2. Sedang Dibaca: 25 Cuplikan WooCommerce Terbaik Saya Untuk WordPress Bagian 2

Beberapa hari yang lalu saya menulis posting di mana saya mencantumkan 25 cuplikan WooCommerce paling berguna yang saya gunakan dan banyak orang meminta lebih banyak, jadi hari ini saya memutuskan untuk memberikan beberapa cuplikan lagi untuk WooCommerce! Ini dia!

1 – Ganti logo PayPal default WooCommerce

/*
 * Replace WooCommerce default PayPal icon
 */
function paypal_checkout_icon() {
 return 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_betalen_met_paypal_nl.jpg'; // write your own image URL here
}
add_filter( 'woocommerce_paypal_icon', 'paypal_checkout_icon' );

2 – Ganti gambar placeholder produk default

/*
* goes in theme functions.php or a custom plugin. Replace the image filename/path with your own :)
*
**/
add_action( 'init', 'custom_fix_thumbnail' );
 
function custom_fix_thumbnail() {
  add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
   
	function custom_woocommerce_placeholder_img_src( $src ) {
	$upload_dir = wp_upload_dir();
	$uploads = untrailingslashit( $upload_dir['baseurl'] );
	$src = $uploads . '/2012/07/thumb1.jpg';
	 
	return $src;
	}
}

3 – Hapus “Produk” dari remah roti

/*
 * Hide "Products" in WooCommerce 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 );

4 – Keranjang kosong

/*
 * Empty WooCommerce cart
 */
function my_empty_cart(){
	global $woocommerce;
	$woocommerce->cart->empty_cart(); 
}
add_action('init', 'my_empty_cart');

5 – Secara otomatis menambahkan produk ke keranjang saat berkunjung

/*
 * Add item to cart on visit
 */
function add_product_to_cart() {
	if ( ! is_admin() ) {
		global $woocommerce;
		$product_id = 64;
		$found = false;
		//check if product already in cart
		if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
			foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
				$_product = $values['data'];
				if ( $_product->id == $product_id )
					$found = true;
			}
			// if product not found, add it
			if ( ! $found )
				$woocommerce->cart->add_to_cart( $product_id );
		} else {
			// if no products in cart, add it
			$woocommerce->cart->add_to_cart( $product_id );
		}
	}
}
add_action( 'init', 'add_product_to_cart' );

6 – Tambahkan mata uang/simbol khusus

add_filter( 'woocommerce_currencies', 'add_my_currency' );
 
function add_my_currency( $currencies ) {
     $currencies['ABC'] = __( 'Currency name', 'woocommerce' );
     return $currencies;
}
 
add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2);
 
function add_my_currency_symbol( $currency_symbol, $currency ) {
     switch( $currency ) {
          case 'ABC': $currency_symbol = '$'; break;
     }
     return $currency_symbol;
}

7 – Ubah teks tombol tambahkan ke troli

/**
 * Change the add to cart text on single product pages
 */
function woo_custom_cart_button_text() {
	return __('My Button Text', 'woocommerce');
}
add_filter('single_add_to_cart_text', 'woo_custom_cart_button_text');



/**
 * Change the add to cart text on product archives
 */
function woo_archive_custom_cart_button_text() {
	return __( 'My Button Text', 'woocommerce' );
}
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );

8 – Alihkan langganan tambahkan ke keranjang ke halaman checkout

/**
 * Redirect subscription add to cart to checkout page
 *
 * @param string $url
 */
function custom_add_to_cart_redirect( $url ) {
  
  $product_id	= (int) $_REQUEST['add-to-cart'];
	if ( class_exists( 'WC_Subscriptions_Product' ) ) {
		if ( WC_Subscriptions_Product::is_subscription( $product_id ) ) {
			return get_permalink(get_option( 'woocommerce_checkout_page_id' ) );
		} else return $url;
	} else return $url;
	
}
add_filter('add_to_cart_redirect', 'custom_add_to_cart_redirect');

Cuplikan ini memerlukan plugin Langganan.

9 – Alihkan ke halaman checkout setelah menambahkan ke keranjang

/**
 * Redirect subscription add to cart to checkout page
 *
 * @param none
 */
function add_to_cart_checkout_redirect() {
	wp_safe_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
	die();
}
add_action( 'woocommerce_add_to_cart',  'add_to_cart_checkout_redirect', 11 );

10 – CC semua email

 /**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Add another email recipient to all WooCommerce emails
 *
 */
function woo_cc_all_emails() {
  return 'Bcc: youremail@yourdomain.com' . "\r\n";
}
add_filter('woocommerce_email_headers', 'woo_cc_all_emails' );

11 – Kirim email saat pesanan baru selesai dengan kupon yang sudah digunakan

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Send an email each time an order with coupon(s) is completed
 * The email contains coupon(s) used during checkout process
 *
 */ 
function woo_email_order_coupons( $order_id ) {
        $order = new WC_Order( $order_id );
        
        if( $order->get_used_coupons() ) {
        
          $to = 'youremail@yourcompany.com';
	        $subject = 'New Order Completed';
	        $headers = 'From: My Name ' . "\r\n";
	        
	        $message = 'A new order has been completed.\n';
	        $message .= 'Order ID: '.$order_id.'\n';
	        $message .= 'Coupons used:\n';
	        
	        foreach( $order->get_used_coupons() as $coupon) {
		        $message .= $coupon.'\n';
	        }
	        @wp_mail( $to, $subject, $message, $headers );
        }
}
add_action( 'woocommerce_thankyou', 'woo_email_order_coupons' );

12 – Ubah nomor produk terkait

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Change number of related products on product page
 * Set your own value for 'posts_per_page'
 *
 */ 
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' );

13 – Kecualikan produk dari kategori tertentu di halaman toko

 /**
 * Remove products from shop page by category
 *
 */
function woo_custom_pre_get_posts_query( $q ) {
 
	if ( ! $q->is_main_query() ) return;
	if ( ! $q->is_post_type_archive() ) return;
	
	if ( ! is_admin() && is_shop() ) {
 
		$q->set( 'tax_query', array(array(
			'taxonomy' => 'product_cat',
			'field' => 'slug',
			'terms' => array( 'shoes' ), // Don't display products in the shoes category on the shop page
			'operator' => 'NOT IN'
		)));
	
	}
 
	remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
 
}
add_action( 'pre_get_posts', 'woo_custom_pre_get_posts_query' );

14 – Ubah nomor kolom toko

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Change product columns number on shop pages
 *
 */
function woo_product_columns_frontend() {
    global $woocommerce;

    // Default Value also used for categories and sub_categories
    $columns = 4;

    // Product List
    if ( is_product_category() ) :
        $columns = 4;
    endif;

    //Related Products
    if ( is_product() ) :
        $columns = 2;
    endif;

    //Cross Sells
    if ( is_checkout() ) :
        $columns = 4;
    endif;

	return $columns;
}
add_filter('loop_shop_columns', 'woo_product_columns_frontend');

15 – Nonaktifkan tab WooCommerce

 /**
 * Remove product tabs
 *
 */
function woo_remove_product_tab($tabs) {

    unset( $tabs['description'] );      		// Remove the description tab
    unset( $tabs['reviews'] ); 					// Remove the reviews tab
    unset( $tabs['additional_information'] );  	// Remove the additional information tab

 	return $tabs;
 
}
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tab', 98);

16 – Hapus remah roti

 /**
 * Remove WooCommerce BreadCrumb
 *
 */
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);

17 – Batasi daftar negara pengiriman

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Restrict shipping countries list
 *
 */
function woo_override_checkout_fields( $fields ) { 

	$fields['shipping']['shipping_country'] = array(
		'type'      => 'select',
		'label'     => __('My New Country List', 'woocommerce'),
		'options' 	=> array('AU' => 'Australia')
	);

	return $fields; 
} 
add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );

18 – Ganti “Gratis!” rangkaian produk

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Replace "Free!" by a custom string
 *
 */
function woo_my_custom_free_message() {
	return "This product is FREE!";
}

add_filter('woocommerce_free_price_html', 'woo_my_custom_free_message');

19 – Sembunyikan SEMUA metode pengiriman lainnya saat Pengiriman Gratis tersedia

// Hide ALL shipping options when free shipping is available
add_filter( 'woocommerce_available_shipping_methods', 'hide_all_shipping_when_free_is_available' , 10, 1 );
 
/**
* Hide ALL Shipping option when free shipping is available
*
* @param array $available_methods
*/
function hide_all_shipping_when_free_is_available( $available_methods ) {
 
  	if( isset( $available_methods['free_shipping'] ) ) :
		
		// Get Free Shipping array into a new array
		$freeshipping = array();
		$freeshipping = $available_methods['free_shipping'];
 
		// Empty the $available_methods array
		unset( $available_methods );
 
		// Add Free Shipping back into $avaialble_methods
		$available_methods = array();
		$available_methods[] = $freeshipping;
 
	endif;
 
	return $available_methods;
}

20 – Jadikan kolom “status” checkout tidak wajib diisi

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Make "state" field not required on checkout
 *
 */
 
add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing', 10, 1 );
add_filter( 'woocommerce_shipping_fields', 'woo_filter_state_shipping', 10, 1 );

function woo_filter_state_billing( $address_fields ) { 
	$address_fields['billing_state']['required'] = false;
	return $address_fields;
}

function woo_filter_state_shipping( $address_fields ) { 
	$address_fields['shipping_state']['required'] = false;
	return $address_fields;
}

21 – Buat kupon secara terprogram

$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
					
$coupon = array(
	'post_title' => $coupon_code,
	'post_content' => '',
	'post_status' => 'publish',
	'post_author' => 1,
	'post_type'		=> 'shop_coupon'
);
					
$new_coupon_id = wp_insert_post( $coupon );
					
// Add meta
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'no' );
update_post_meta( $new_coupon_id, 'product_ids', '' );
update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
update_post_meta( $new_coupon_id, 'usage_limit', '' );
update_post_meta( $new_coupon_id, 'expiry_date', '' );
update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

22 – Mengubah baris subjek email

/*
 * Subject filters: 
 *   woocommerce_email_subject_new_order
 *   woocommerce_email_subject_customer_procesing_order
 *   woocommerce_email_subject_customer_completed_order
 *   woocommerce_email_subject_customer_invoice
 *   woocommerce_email_subject_customer_note
 *   woocommerce_email_subject_low_stock
 *   woocommerce_email_subject_no_stock
 *   woocommerce_email_subject_backorder
 *   woocommerce_email_subject_customer_new_account
 *   woocommerce_email_subject_customer_invoice_paid
 **/
add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
 
function change_admin_email_subject( $subject, $order ) {
	global $woocommerce;
 
	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
 
	$subject = sprintf( '[%s] New Customer Order (# %s) from Name %s %s', $blogname, $order->id, $order->billing_first_name, $order->billing_last_name );
 
	return $subject;
}

23 – Tambahkan biaya khusus ke keranjang

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Add custom fee to cart automatically
 *
 */
function woo_add_cart_fee() {

	global $woocommerce;
	
	if ( is_cart() ) {
		$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
	}
	
}
add_action( 'woocommerce_before_cart_table', 'woo_add_cart_fee' );

24 – Pesan khusus yang ditambahkan ke keranjang

/**
 * Custom Add To Cart Messages
 * Add this to your theme functions.php file
 **/
add_filter( 'woocommerce_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
	global $woocommerce;
 
	// Output success messages
	if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
 
		$return_to 	= get_permalink(woocommerce_get_page_id('shop'));
 
		$message 	= sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
 
	else :
 
		$message 	= sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
 
	endif;
 
		return $message;
}

25 – Tambahkan metode pembayaran ke email admin

/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Add payment method to admin new order email
 *
 */
add_action( 'woocommerce_email_after_order_table', 'woo_add_payment_method_to_admin_new_order', 15, 2 ); 

function woo_add_payment_method_to_admin_new_order( $order, $is_admin_email ) { 
	if ( $is_admin_email ) { 
	echo '<p><strong>Payment Method:</strong> ' . $order->payment_method_title . '</p>'; 
	} 
}

Dan booming! Itu dia! Saya harap Anda menyukai cuplikan ini. Jika Anda punya saran, jangan ragu untuk meninggalkan komentar di bawah!


Belum memiliki tema WooCommerce siap pakai, atau ingin yang baru? Pastikan untuk memeriksa tema Total WordPress kami!