脚本 – 蓝月网络 https://lanyueer.com 专注于WordPress外贸建站,主题插件汉化、配置 Sat, 13 Dec 2025 15:40:57 +0000 zh-CN hourly 1 实用的25个WooCommerce片段2 https://lanyueer.com/25-best-woocommerce-snippets-2/ Tue, 20 Jun 2017 11:32:50 +0000 https://lanyueer.com/?p=958 WooCommerce是非常强大的电商工具,并且易于扩展。 它有许多钩子可以用于修改几乎所有的东西,这就是使W…

实用的25个WooCommerce片段2,首发于蓝月网络

]]>
WooCommerce是非常强大的电商工具,并且易于扩展。 它有许多钩子可以用于修改几乎所有的东西,这就是使WooCommerce如此受欢迎。 以下一些实用的Woocommerce片段列表 所有这些片段必须粘贴在您的主题文件夹中的functions.php文件中才能起作用:

1 – 替换WooCommerce默认PayPal logo

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

2 – 替换默认产品占位符图片

  1. /*
  2. * goes in theme functions.php or a custom plugin. Replace the image filename/path with your own ?
  3. *
  4. **/
  5. add_action( 'init', 'custom_fix_thumbnail' );
  6.  
  7. function custom_fix_thumbnail() {
  8.   add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
  9.  
  10. 	function custom_woocommerce_placeholder_img_src( $src ) {
  11. 	$upload_dir = wp_upload_dir();
  12. 	$uploads = untrailingslashit( $upload_dir['baseurl'] );
  13. 	$src = $uploads . '/2012/07/thumb1.jpg';
  14.  
  15. 	return $src;
  16. 	}
  17. }

3 – 从面包屑中移除“Products”

  1. /*
  2.  * Hide "Products" in WooCommerce breadcrumb
  3.  */
  4. function woo_custom_filter_breadcrumbs_trail ( $trail ) {
  5.   foreach ( $trail as $k => $v ) {
  6.     if ( strtolower( strip_tags( $v ) ) == 'products' ) {
  7.       unset( $trail[$k] );
  8.       break;
  9.     }
  10.   }
  11.  
  12.   return $trail;
  13. }
  14.  
  15. add_filter( 'woo_breadcrumbs_trail', 'woo_custom_filter_breadcrumbs_trail', 10 );

4 – 清空购物车

  1. /*
  2.  * Empty WooCommerce cart
  3.  */
  4. function my_empty_cart(){
  5. 	global $woocommerce;
  6. 	$woocommerce->cart->empty_cart(); 
  7. }
  8. add_action('init', 'my_empty_cart');

5 – 访问时自动添加产品到购物车

  1. /*
  2.  * Add item to cart on visit
  3.  */
  4. function add_product_to_cart() {
  5. 	if ( ! is_admin() ) {
  6. 		global $woocommerce;
  7. 		$product_id = 64;
  8. 		$found = false;
  9. 		//check if product already in cart
  10. 		if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
  11. 			foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  12. 				$_product = $values['data'];
  13. 				if ( $_product->id == $product_id )
  14. 					$found = true;
  15. 			}
  16. 			// if product not found, add it
  17. 			if ( ! $found )
  18. 				$woocommerce->cart->add_to_cart( $product_id );
  19. 		} else {
  20. 			// if no products in cart, add it
  21. 			$woocommerce->cart->add_to_cart( $product_id );
  22. 		}
  23. 	}
  24. }
  25. add_action( 'init', 'add_product_to_cart' );

6 – 添加自定义货币/符号

  1. add_filter( 'woocommerce_currencies', 'add_my_currency' );
  2.  
  3. function add_my_currency( $currencies ) {
  4.      $currencies['ABC'] = __( 'Currency name', 'woocommerce' );
  5.      return $currencies;
  6. }
  7.  
  8. add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2);
  9.  
  10. function add_my_currency_symbol( $currency_symbol, $currency ) {
  11.      switch( $currency ) {
  12.           case 'ABC': $currency_symbol = '$'; break;
  13.      }
  14.      return $currency_symbol;
  15. }

7 – 更改添加到购物车按钮文字

  1. /**
  2.  * Change the add to cart text on single product pages
  3.  */
  4. function woo_custom_cart_button_text() {
  5. 	return __('My Button Text', 'woocommerce');
  6. }
  7. add_filter('single_add_to_cart_text', 'woo_custom_cart_button_text');
  8.  
  9.  
  10.  
  11. /**
  12.  * Change the add to cart text on product archives
  13.  */
  14. function woo_archive_custom_cart_button_text() {
  15. 	return __( 'My Button Text', 'woocommerce' );
  16. }
  17. add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );

8 – 重定向订阅添加到购物车到结帐页面

  1. /**
  2.  * Redirect subscription add to cart to checkout page
  3.  *
  4.  * @param string $url
  5.  */
  6. function custom_add_to_cart_redirect( $url ) {
  7.  
  8.   $product_id	= (int) $_REQUEST['add-to-cart'];
  9. 	if ( class_exists( 'WC_Subscriptions_Product' ) ) {
  10. 		if ( WC_Subscriptions_Product::is_subscription( $product_id ) ) {
  11. 			return get_permalink(get_option( 'woocommerce_checkout_page_id' ) );
  12. 		} else return $url;
  13. 	} else return $url;
  14.  
  15. }
  16. add_filter('add_to_cart_redirect', 'custom_add_to_cart_redirect');

此片段需要订阅插件。

9 – 加入购物车后,重定向到结帐页面

  1. /**
  2.  * Redirect subscription add to cart to checkout page
  3.  *
  4.  * @param none
  5.  */
  6. function add_to_cart_checkout_redirect() {
  7. 	wp_safe_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
  8. 	die();
  9. }
  10. add_action( 'woocommerce_add_to_cart',  'add_to_cart_checkout_redirect', 11 );

10 – CC所有电子邮件

  1.  /**
  2.  * WooCommerce Extra Feature
  3.  * --------------------------
  4.  *
  5.  * Add another email recipient to all WooCommerce emails
  6.  *
  7.  */
  8. function woo_cc_all_emails() {
  9.   return 'Bcc: youremail@yourdomain.com' . "\r\n";
  10. }
  11. add_filter('woocommerce_email_headers', 'woo_cc_all_emails' );

11 – 当使用优惠券完成新订单时,发送电子邮件

  1. /**
  2.  * WooCommerce Extra Feature
  3.  * --------------------------
  4.  *
  5.  * Send an email each time an order with coupon(s) is completed
  6.  * The email contains coupon(s) used during checkout process
  7.  *
  8.  */ 
  9. function woo_email_order_coupons( $order_id ) {
  10.         $order = new WC_Order( $order_id );
  11.  
  12.         if( $order->get_used_coupons() ) {
  13.  
  14.           $to = 'youremail@yourcompany.com';
  15. 	        $subject = 'New Order Completed';
  16. 	        $headers = 'From: My Name ' . "\r\n";
  17.  
  18. 	        $message = 'A new order has been completed.\n';
  19. 	        $message .= 'Order ID: '.$order_id.'\n';
  20. 	        $message .= 'Coupons used:\n';
  21.  
  22. 	        foreach( $order->get_used_coupons() as $coupon) {
  23. 		        $message .= $coupon.'\n';
  24. 	        }
  25. 	        @wp_mail( $to, $subject, $message, $headers );
  26.         }
  27. }
  28. add_action( 'woocommerce_thankyou', 'woo_email_order_coupons' );

12 – 更改相关产品数量

  1. /**
  2.  * WooCommerce Extra Feature
  3.  * --------------------------
  4.  *
  5.  * Change number of related products on product page
  6.  * Set your own value for 'posts_per_page'
  7.  *
  8.  */ 
  9. function woo_related_products_limit() {
  10.   global $product;
  11.  
  12. 	$args = array(
  13. 		'post_type'        		=> 'product',
  14. 		'no_found_rows'    		=> 1,
  15. 		'posts_per_page'   		=> 6,
  16. 		'ignore_sticky_posts' 	=> 1,
  17. 		'orderby'             	=> $orderby,
  18. 		'post__in'            	=> $related,
  19. 		'post__not_in'        	=> array($product->id)
  20. 	);
  21. 	return $args;
  22. }
  23. add_filter( 'woocommerce_related_products_args', 'woo_related_products_limit' );

13 – 从商店页面中的特定类别中排除产品

  1.  /**
  2.  * Remove products from shop page by category
  3.  *
  4.  */
  5. function woo_custom_pre_get_posts_query( $q ) {
  6.  
  7. 	if ( ! $q->is_main_query() ) return;
  8. 	if ( ! $q->is_post_type_archive() ) return;
  9.  
  10. 	if ( ! is_admin() && is_shop() ) {
  11.  
  12. 		$q->set( 'tax_query', array(array(
  13. 			'taxonomy' => 'product_cat',
  14. 			'field' => 'slug',
  15. 			'terms' => array( 'shoes' ), // Don't display products in the shoes category on the shop page
  16. 			'operator' => 'NOT IN'
  17. 		)));
  18.  
  19. 	}
  20.  
  21. 	remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
  22.  
  23. }
  24. add_action( 'pre_get_posts', 'woo_custom_pre_get_posts_query' );

14 – 更改商城列数

  1. /**
  2.  * WooCommerce Extra Feature
  3.  * --------------------------
  4.  *
  5.  * Change product columns number on shop pages
  6.  *
  7.  */
  8. function woo_product_columns_frontend() {
  9.     global $woocommerce;
  10.  
  11.     // Default Value also used for categories and sub_categories
  12.     $columns = 4;
  13.  
  14.     // Product List
  15.     if ( is_product_category() ) :
  16.         $columns = 4;
  17.     endif;
  18.  
  19.     //Related Products
  20.     if ( is_product() ) :
  21.         $columns = 2;
  22.     endif;
  23.  
  24.     //Cross Sells
  25.     if ( is_checkout() ) :
  26.         $columns = 4;
  27.     endif;
  28.  
  29. 	return $columns;
  30. }
  31. add_filter('loop_shop_columns', 'woo_product_columns_frontend');

15 – 禁用WooCommerce选项卡

  1. /**
  2.  * Remove product tabs
  3.  *
  4.  */
  5. function woo_remove_product_tab($tabs) {
  6.  
  7.     unset( $tabs['description'] );      		// Remove the description tab
  8.     unset( $tabs['reviews'] ); 					// Remove the reviews tab
  9.     unset( $tabs['additional_information'] );  	// Remove the additional information tab
  10.  
  11.  	return $tabs;
  12.  
  13. }
  14. add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tab', 98);

16 – 移除面包屑

  1.  /**
  2.  * Remove WooCommerce BreadCrumb
  3.  *
  4.  */
  5. remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);

17 – 限制运输的国家列表

  1. /**
  2.  * WooCommerce Extra Feature
  3.  * --------------------------
  4.  *
  5.  * Restrict shipping countries list
  6.  *
  7.  */
  8. function woo_override_checkout_fields( $fields ) { 
  9.  
  10. 	$fields['shipping']['shipping_country'] = array(
  11. 		'type'      => 'select',
  12. 		'label'     => __('My New Country List', 'woocommerce'),
  13. 		'options' 	=> array('AU' => 'Australia')
  14. 	);
  15.  
  16. 	return $fields; 
  17. } 
  18. add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );

18 – 替换“Free!”产品字符串

  1. /**
  2.  * WooCommerce Extra Feature
  3.  * --------------------------
  4.  *
  5.  * Replace "Free!" by a custom string
  6.  *
  7.  */
  8. function woo_my_custom_free_message() {
  9. 	return "This product is FREE!";
  10. }
  11.  
  12. add_filter('woocommerce_free_price_html', 'woo_my_custom_free_message');

19 – 当免费送货可用时隐藏所有其他运送方式

  1. // Hide ALL shipping options when free shipping is available
  2. add_filter( 'woocommerce_available_shipping_methods', 'hide_all_shipping_when_free_is_available' , 10, 1 );
  3.  
  4. /**
  5. * Hide ALL Shipping option when free shipping is available
  6. *
  7. * @param array $available_methods
  8. */
  9. function hide_all_shipping_when_free_is_available( $available_methods ) {
  10.  
  11.   	if( isset( $available_methods['free_shipping'] ) ) :
  12.  
  13. 		// Get Free Shipping array into a new array
  14. 		$freeshipping = array();
  15. 		$freeshipping = $available_methods['free_shipping'];
  16.  
  17. 		// Empty the $available_methods array
  18. 		unset( $available_methods );
  19.  
  20. 		// Add Free Shipping back into $avaialble_methods
  21. 		$available_methods = array();
  22. 		$available_methods[] = $freeshipping;
  23.  
  24. 	endif;
  25.  
  26. 	return $available_methods;
  27. }

20 – 设置结账时“state”字段非必填

  1. /**
  2.  * WooCommerce Extra Feature
  3.  * --------------------------
  4.  *
  5.  * Make "state" field not required on checkout
  6.  *
  7.  */
  8.  
  9. add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing', 10, 1 );
  10. add_filter( 'woocommerce_shipping_fields', 'woo_filter_state_shipping', 10, 1 );
  11.  
  12. function woo_filter_state_billing( $address_fields ) { 
  13. 	$address_fields['billing_state']['required'] = false;
  14. 	return $address_fields;
  15. }
  16.  
  17. function woo_filter_state_shipping( $address_fields ) { 
  18. 	$address_fields['shipping_state']['required'] = false;
  19. 	return $address_fields;
  20. }

21 – 创建优惠券程序

  1. $coupon_code = 'UNIQUECODE'; // Code
  2. $amount = '10'; // Amount
  3. $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
  4.  
  5. $coupon = array(
  6. 	'post_title' => $coupon_code,
  7. 	'post_content' => '',
  8. 	'post_status' => 'publish',
  9. 	'post_author' => 1,
  10. 	'post_type'		=> 'shop_coupon'
  11. );
  12.  
  13. $new_coupon_id = wp_insert_post( $coupon );
  14.  
  15. // Add meta
  16. update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
  17. update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
  18. update_post_meta( $new_coupon_id, 'individual_use', 'no' );
  19. update_post_meta( $new_coupon_id, 'product_ids', '' );
  20. update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
  21. update_post_meta( $new_coupon_id, 'usage_limit', '' );
  22. update_post_meta( $new_coupon_id, 'expiry_date', '' );
  23. update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
  24. update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

22 – 更改电子邮件主题行

  1. /*
  2.  * Subject filters: 
  3.  *   woocommerce_email_subject_new_order
  4.  *   woocommerce_email_subject_customer_procesing_order
  5.  *   woocommerce_email_subject_customer_completed_order
  6.  *   woocommerce_email_subject_customer_invoice
  7.  *   woocommerce_email_subject_customer_note
  8.  *   woocommerce_email_subject_low_stock
  9.  *   woocommerce_email_subject_no_stock
  10.  *   woocommerce_email_subject_backorder
  11.  *   woocommerce_email_subject_customer_new_account
  12.  *   woocommerce_email_subject_customer_invoice_paid
  13.  **/
  14. add_filter('woocommerce_email_subject_new_order', 'change_admin_email_subject', 1, 2);
  15.  
  16. function change_admin_email_subject( $subject, $order ) {
  17. 	global $woocommerce;
  18.  
  19. 	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  20.  
  21. 	$subject = sprintf( '[%s] New Customer Order (# %s) from Name %s %s', $blogname, $order->id, $order->billing_first_name, $order->billing_last_name );
  22.  
  23. 	return $subject;
  24. }

23 – 添加自定义费用到购物车

  1. /**
  2.  * WooCommerce Extra Feature
  3.  * --------------------------
  4.  *
  5.  * Add custom fee to cart automatically
  6.  *
  7.  */
  8. function woo_add_cart_fee() {
  9.  
  10. 	global $woocommerce;
  11.  
  12. 	if ( is_cart() ) {
  13. 		$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
  14. 	}
  15.  
  16. }
  17. add_action( 'woocommerce_before_cart_table', 'woo_add_cart_fee' );

24 – 自定义添加到购物车消息

  1. /**
  2.  * Custom Add To Cart Messages
  3.  * Add this to your theme functions.php file
  4.  **/
  5. add_filter( 'woocommerce_add_to_cart_message', 'custom_add_to_cart_message' );
  6. function custom_add_to_cart_message() {
  7. 	global $woocommerce;
  8.  
  9. 	// Output success messages
  10. 	if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
  11.  
  12. 		$return_to 	= get_permalink(woocommerce_get_page_id('shop'));
  13.  
  14. 		$message 	= sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
  15.  
  16. 	else :
  17.  
  18. 		$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') );
  19.  
  20. 	endif;
  21.  
  22. 		return $message;
  23. }

25 – 向管理员电子邮件添加付款方式

  1. /**
  2.  * WooCommerce Extra Feature
  3.  * --------------------------
  4.  *
  5.  * Add payment method to admin new order email
  6.  *
  7.  */
  8. add_action( 'woocommerce_email_after_order_table', 'woo_add_payment_method_to_admin_new_order', 15, 2 ); 
  9.  
  10. function woo_add_payment_method_to_admin_new_order( $order, $is_admin_email ) { 
  11. 	if ( $is_admin_email ) { 
  12. 	echo '<p><strong>Payment Method:</strong> ' . $order->payment_method_title . '</p>'; 
  13. 	} 
  14. }

原文链接:My 25 Best WooCommerce Snippets For WordPress Part 2

实用的25个WooCommerce片段2,首发于蓝月网络

]]>
实用的25个WooCommerce片段1 https://lanyueer.com/25-best-woocommerce-snippets-1/ Tue, 20 Jun 2017 11:30:56 +0000 https://lanyueer.com/?p=957 WooCommerce是非常强大的电商工具,并且易于扩展。 它有许多钩子可以用于修改几乎所有的东西,这就是使W…

实用的25个WooCommerce片段1,首发于蓝月网络

]]>
WooCommerce是非常强大的电商工具,并且易于扩展。 它有许多钩子可以用于修改几乎所有的东西,这就是使WooCommerce如此受欢迎。 以下一些实用的Woocommerce片段列表 所有这些片段必须粘贴在您的主题文件夹中的functions.php文件中才能起作用:
实用的25个WooCommerce片段2

WooCommerce是非常强大的电商工具,并且易于扩展。 它有许多钩子可以用于修改几乎所有的东西,这就是使WooCommerce如此受欢迎。 以下一些实用的Woocommerce片段列表 所有这些片段必须粘贴在您的主题文件夹中的functions.php文件中才能起作用: 1 – 替换WooCommerce默认PayPal logo /* * Replace WooCommerce default P...

1 – 将付款类型添加到WooCommerce管理电子邮件

  1. add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );
  2.  
  3. function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {
  4.   if ( $is_admin_email ) {
  5.     echo '<p><strong>Payment Method:</strong> ' . $order->payment_method_title . '</p>';
  6.   }
  7. }

2 – 每页/每行向上销售产品数

  1. remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
  2. add_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_upsells', 15 );
  3.  
  4. if ( ! function_exists( 'woocommerce_output_upsells' ) ) {
  5. 	function woocommerce_output_upsells() {
  6. 	    woocommerce_upsell_display( 3,3 ); // Display 3 products in rows of 3
  7. 	}
  8. }

3 – 从商店页面中删除某些产品类别

  1. add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
  2.  
  3. function custom_pre_get_posts_query( $q ) {
  4.  
  5. 	if ( ! $q->is_main_query() ) return;
  6. 	if ( ! $q->is_post_type_archive() ) return;
  7.  
  8. 	if ( ! is_admin() && is_shop() && ! is_user_logged_in() ) {
  9.  
  10. 		$q->set( 'tax_query', array(array(
  11. 			'taxonomy' => 'product_cat',
  12. 			'field' => 'slug',
  13. 			'terms' => array( 'color', 'flavor', 'spices', 'vanilla' ), // Don't display products in these categories on the shop page
  14. 			'operator' => 'NOT IN'
  15. 		)));
  16.  
  17. 	}
  18.  
  19. 	remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
  20.  
  21. }

4 – 快速翻译字段

  1. add_filter('gettext',  'translate_text');
  2. add_filter('ngettext',  'translate_text');
  3.  
  4. function translate_text($translated) {
  5.      $translated = str_ireplace('Choose and option',  'Select',  $translated);
  6.      return $translated;
  7. }

5 – 从WooCommerce类别小工具中排除一个类别

  1. add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
  2.  
  3. function woo_product_cat_widget_args( $cat_args ) {
  4.  
  5. 	$cat_args['exclude'] = array('16');
  6.  
  7. 	return $cat_args;
  8. }

6 – 向产品可变添加自定义字段

  1. //Display Fields
  2. add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
  3. //JS to add fields for new variations
  4. add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
  5. //Save variation fields
  6. add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );
  7.  
  8. function variable_fields( $loop, $variation_data ) { ?>	
  9. 	<tr>
  10. 		<td>
  11. 			<div>
  12. 					<label></label>
  13. 					<input type="text" size="5" name="my_custom_field[]" value=""/>
  14. 			</div>
  15. 		</td>
  16. 	</tr>
  17.  
  18. <tr>
  19. 		<td>
  20. 			<div>
  21. 					<label></label>
  22.  
  23. 			</div>
  24. 		</td>
  25. 	</tr>
  26. <?php }
  27.  
  28. function variable_fields_process( $post_id ) {
  29. 	if (isset( $_POST['variable_sku'] ) ) :
  30. 		$variable_sku = $_POST['variable_sku'];
  31. 		$variable_post_id = $_POST['variable_post_id'];
  32. 		$variable_custom_field = $_POST['my_custom_field'];
  33. 		for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  34. 			$variation_id = (int) $variable_post_id[$i];
  35. 			if ( isset( $variable_custom_field[$i] ) ) {
  36. 				update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
  37. 			}
  38. 		endfor;
  39. 	endif;
  40. }

7 – 用“sold”替换“Out of stock”

  1. add_filter('woocommerce_get_availability', 'availability_filter_func');
  2.  
  3. function availability_filter_func($availability)
  4. {
  5. 	$availability['availability'] = str_ireplace('Out of stock', 'Sold', $availability['availability']);
  6. 	return $availability;
  7. }

8 – 使用“产品已在购物车”替代“加入购物车”按钮

  1. /**
  2.  * Change the add to cart text on single product pages
  3.  */
  4. add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
  5.  
  6. function woo_custom_cart_button_text() {
  7.  
  8. 	global $woocommerce;
  9.  
  10. 	foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  11. 		$_product = $values['data'];
  12.  
  13. 		if( get_the_ID() == $_product->id ) {
  14. 			return __('Already in cart - Add Again?', 'woocommerce');
  15. 		}
  16. 	}
  17.  
  18. 	return __('Add to cart', 'woocommerce');
  19. }
  20.  
  21. /**
  22.  * Change the add to cart text on product archives
  23.  */
  24. add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );
  25.  
  26. function woo_archive_custom_cart_button_text() {
  27.  
  28. 	global $woocommerce;
  29.  
  30. 	foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  31. 		$_product = $values['data'];
  32.  
  33. 		if( get_the_ID() == $_product->id ) {
  34. 			return __('Already in cart', 'woocommerce');
  35. 		}
  36. 	}
  37.  
  38. 	return __('Add to cart', 'woocommerce');
  39. }

9 – 在类别视图中隐藏产品数量

  1. add_filter( 'woocommerce_subcategory_count_html', 'woo_remove_category_products_count' );
  2.  
  3. function woo_remove_category_products_count() {
  4. 	return;
  5. }

10 – 设置结账字段必填

  1. add_filter( 'woocommerce_checkout_fields', 'woo_filter_account_checkout_fields' );
  2.  
  3. function woo_filter_account_checkout_fields( $fields ) {
  4. 	$fields['account']['account_username']['required'] = true;
  5. 	$fields['account']['account_password']['required'] = true;
  6. 	$fields['account']['account_password-2']['required'] = true;
  7.  
  8. 	return $fields;
  9. }

11 – 重命名产品选项卡

  1. add_filter( 'woocommerce_product_tabs', 'woo_rename_tab', 98);
  2. function woo_rename_tab($tabs) {
  3.  
  4.  $tabs['description']['title'] = 'More info';
  5.  
  6.  return $tabs;
  7. }

12 – 列出WooCommerce产品分类

  1. $args = array(
  2.     'number'     => $number,
  3.     'orderby'    => $orderby,
  4.     'order'      => $order,
  5.     'hide_empty' => $hide_empty,
  6.     'include'    => $ids
  7. );
  8.  
  9. $product_categories = get_terms( 'product_cat', $args );
  10.  
  11. $count = count($product_categories);
  12.  if ( $count > 0 ){
  13.      echo "<ul>";
  14.      foreach ( $product_categories as $product_category ) {
  15.        echo '<li><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</li>';
  16.  
  17.      }
  18.      echo "</ul>";
  19.  }

13 – 更换商店页面标题

  1. add_filter( 'woocommerce_page_title', 'woo_shop_page_title');
  2.  
  3. function woo_shop_page_title( $page_title ) {
  4.  
  5. 	if( 'Shop' == $page_title) {
  6. 		return "My new title";
  7. 	}
  8. }

14 – 更改小工具标题

  1. /*
  2.  * Change widget title
  3.  */
  4. add_filter( 'widget_title', 'woo_widget_title', 10, 3);
  5.  
  6. function woo_widget_title( $title, $instance, $id_base ) {
  7.  
  8. 	if( 'onsale' == $id_base) {
  9. 		return "My new title";
  10. 	}
  11. }

15 – 删除WooCommerce默认设置

  1. add_filter( 'woocommerce_catalog_settings', 'woo_remove_catalog_options' );
  2.  
  3. function woo_remove_catalog_options( $catalog ) {
  4.  
  5. 	unset($catalog[23]); //Trim zeros (no) 
  6. 	unset($catalog[22]); //2 decimals 
  7. 	unset($catalog[21]); //decimal sep (.) 
  8. 	unset($catalog[20]); //thousand sep (,) 
  9. 	unset($catalog[19]); //currency position (left)	
  10. 	unset($catalog[18]); //currency position (left)	
  11. 	unset($catalog[5]); // ajax add to cart (no)	
  12.  
  13. 	return $catalog; 
  14. }

16 – 更改发件人电子邮件地址

  1. function woo_custom_wp_mail_from() {
  2.         global $woocommerce;
  3.         return html_entity_decode( 'your@email.com' );
  4. }
  5. add_filter( 'wp_mail_from', 'woo_custom_wp_mail_from', 99 );

17 – 从WooCommerce电子邮件名称解码

  1. function woo_custom_wp_mail_from_name() {
  2.         global $woocommerce;
  3.         return html_entity_decode( get_option( 'woocommerce_email_from_name' ) );
  4. }
  5. add_filter( 'wp_mail_from_name', 'woo_custom_wp_mail_from_name', 99 );
  6.  
  7. function woo_custom_wp_mail_from() {
  8.         global $woocommerce;
  9.         return html_entity_decode( get_option( 'woocommerce_email_from' ) );
  10. }
  11. add_filter( 'wp_mail_from_name', 'woo_custom_wp_mail_from_name', 99 );

18 – 返回特色产品ID

  1. function woo_get_featured_product_ids() {
  2. 	// Load from cache
  3. 	$featured_product_ids = get_transient( 'wc_featured_products' );
  4.  
  5. 	// Valid cache found
  6. 	if ( false !== $featured_product_ids )
  7. 		return $featured_product_ids;
  8.  
  9. 	$featured = get_posts( array(
  10. 		'post_type'      => array( 'product', 'product_variation' ),
  11. 		'posts_per_page' => -1,
  12. 		'post_status'    => 'publish',
  13. 		'meta_query'     => array(
  14. 			array(
  15. 				'key' 		=> '_visibility',
  16. 				'value' 	=> array('catalog', 'visible'),
  17. 				'compare' 	=> 'IN'
  18. 			),
  19. 			array(
  20. 				'key' 	=> '_featured',
  21. 				'value' => 'yes'
  22. 			)
  23. 		),
  24. 		'fields' => 'id=>parent'
  25. 	) );
  26.  
  27. 	$product_ids = array_keys( $featured );
  28. 	$parent_ids  = array_values( $featured );
  29. 	$featured_product_ids = array_unique( array_merge( $product_ids, $parent_ids ) );
  30.  
  31. 	set_transient( 'wc_featured_products', $featured_product_ids );
  32.  
  33. 	return $featured_product_ids;
  34. }

19 – 在编辑地址页面添加自定义字段

  1. // add fields to edit address page
  2. function woo_add_edit_address_fields( $fields ) {
  3.  
  4. 	$new_fields = array(
  5. 				'date_of_birth'     => array(
  6. 				'label'             => __( 'Date of birth', 'woocommerce' ),
  7. 				'required'          => false,
  8. 				'class'             => array( 'form-row' ),
  9. 			),
  10. 		);
  11.  
  12. 	$fields = array_merge( $fields, $new_fields );
  13.  
  14.     return $fields;
  15.  
  16. }
  17.  
  18. add_filter( 'woocommerce_default_address_fields', 'woo_add_edit_address_fields' );

20 – 显示销售产品目录简码

  1. function woocommerce_sale_products( $atts ) {
  2.  
  3.     global $woocommerce_loop;
  4.  
  5.     extract(shortcode_atts(array(
  6.         'per_page'  => '12',
  7.         'columns'   => '4',
  8.         'orderby' => 'date',
  9.         'order' => 'desc'
  10.     ), $atts));
  11.  
  12.     $woocommerce_loop['columns'] = $columns;
  13.  
  14.     $args = array(
  15.         'post_type' => 'product',
  16.         'post_status' => 'publish',
  17.         'ignore_sticky_posts'   => 1,
  18.         'posts_per_page' => $per_page,
  19.         'orderby' => $orderby,
  20.         'order' => $order,
  21.         'meta_query' => array(
  22.             array(
  23.                 'key' => '_visibility',
  24.                 'value' => array('catalog', 'visible'),
  25.                 'compare' => 'IN'
  26.             ),
  27.             array(
  28.                 'key' => '_sale_price',
  29.                 'value' =>  0,
  30.                 'compare'   => '>',
  31.                 'type'      => 'NUMERIC'
  32.             )
  33.         )
  34.     );
  35.     query_posts($args);
  36.     ob_start();
  37.     woocommerce_get_template_part( 'loop', 'shop' );
  38.     wp_reset_query();
  39.  
  40.     return ob_get_clean();
  41. }
  42.  
  43. add_shortcode('sale_products', 'woocommerce_sale_products');

21 – 有促销商品

  1. function woo_have_onsale_products() {
  2.  
  3. 	global $woocommerce;
  4.  
  5. 	// Get products on sale
  6. 	$product_ids_on_sale = array_filter( woocommerce_get_product_ids_on_sale() );
  7.  
  8. 	if( !empty( $product_ids_on_sale ) ) {
  9. 		return true;
  10. 	} else {
  11. 		return false;
  12. 	}
  13.  
  14. }
  15.  
  16. // Example:
  17. if( woo_have_onsale_products() ) {
  18. 	echo 'have onsale products';
  19. } else {
  20. 	echo 'no onsale product';
  21. }

22 – 设定最低订单量

  1. add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
  2. function wc_minimum_order_amount() {
  3. 	global $woocommerce;
  4. 	$minimum = 50;
  5. 	if ( $woocommerce->cart->get_cart_total(); < $minimum ) {
  6.            $woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) );
  7. 	}
  8. }

23 – 在商城页面按价格,日期或标题排序

  1. add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');
  2.  
  3. function custom_default_catalog_orderby() {
  4.      return 'date'; // Can also use title and price
  5. }

24 – 重定向添加到购物车按钮到结帐页面

  1. add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
  2.  
  3. function redirect_to_checkout() {
  4.     global $woocommerce;
  5.     $checkout_url = $woocommerce->cart->get_checkout_url();
  6.     return $checkout_url;
  7. }

25 – 订单完成时添加电子邮件收件人

  1. function woo_extra_email_recipient($recipient, $object) {
  2.     $recipient = $recipient . ', your@email.com';
  3.     return $recipient;
  4. }
  5. add_filter( 'woocommerce_email_recipient_customer_completed_order', 'woo_extra_email_recipient', 10, 2);

原文链接:My 25 Best WooCommerce Snippets For WordPress

实用的25个WooCommerce片段1,首发于蓝月网络

]]>
清理HEAD头部多余脚本 https://lanyueer.com/head-head-extra-cleanup-script/ Sun, 08 Nov 2015 05:20:31 +0000 http://lanyueer.com/?p=452 remove_action( 'wp_head', 'feed_links_extra', 3 …

清理HEAD头部多余脚本,首发于蓝月网络

]]>
  1. remove_action( 'wp_head', 'feed_links_extra', 3 ); //去除评论feed
  2.  
  3. remove_action( 'wp_head', 'feed_links', 2 ); //去除文章feed
  4.  
  5. remove_action( 'wp_head', 'rsd_link' ); //针对Blog的远程离线编辑器接口
  6.  
  7. remove_action( 'wp_head', 'wlwmanifest_link' ); //Windows Live Writer接口
  8.  
  9. remove_action( 'wp_head', 'index_rel_link' ); //移除当前页面的索引
  10.  
  11. remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); //移除后面文章的url
  12.  
  13. remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); //移除最开始文章的url
  14.  
  15. remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );//自动生成的短链接
  16.  
  17. remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); ///移除相邻文章的url
  18.  
  19. remove_action( 'wp_head', 'wp_generator' ); // 移除版本号

根据上面的提示注释,我们可以把脚本添加到FUNCTION.PHP(当前主题下)文件中,这样我们网站源代码中就移除掉没有必要的代码行,进而提高代码执行率和网站速度

清理HEAD头部多余脚本,首发于蓝月网络

]]>