为订单添加附加费用/手续费
以下代码演示收取每单商品费用加运费总和的1%作为附加费用。


add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
 
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
 
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, false, '' );
 
}

付款成功后立刻发送invoice


/**
* send invoice straight away if payment is successful
* @param string $order_id valid payment order id
* @return null
*/
function send_invoice_upon_payment_successful($order_id) {
global $woocommerce;
$order = new WC_Order($order_id);
$mailer = $woocommerce->mailer();
$mailer->customer_invoice( $order );
}
add_action('woocommerce_payment_complete', 'send_invoice_upon_payment_successful');

产品列表页:加入购物车按钮移动到标题之前


remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_add_to_cart', 10 );

产品列表页:添加链接
下面的代码演示如何在标题之前添加链接。


add_action( 'woocommerce_before_shop_loop_item_title', 'wc_template_loop_additional_links', 10 );
function wc_template_loop_additional_links(){
?>
<a href="#" class="button1-link button product_type_simple">Button 1 </a>
<a href="#" class="button2-link button product_type_simple">Button 2 </a>
<?php
}

修改产品列表页按钮文字

产品列表页的按钮文字一般是:add to cart、select options, view options和read more。下面代码演示如何更改这些按钮文字,使用代码时,只选择需要的即可,比如要修改view options,只需add_filter( ‘grouped_add_to_cart_text’, ‘wc_add_to_cart_text’ ),其它的删掉。


add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
 
/**
* custom_woocommerce_template_loop_add_to_cart
*/
function custom_woocommerce_product_add_to_cart_text() {
global $product;
 
$product_type = $product->product_type;
 
switch ( $product_type ) {
case 'external':
return __( 'Buy product', 'woocommerce' );
break;
case 'grouped':
return __( 'View products', 'woocommerce' );
break;
case 'simple':
return __( 'Add to cart', 'woocommerce' );
break;
case 'variable':
return __( 'Select options', 'woocommerce' );
break;
default:
return __( 'Read more', 'woocommerce' );
}
 
}

无论产品是否有属性,添加到购物车的按钮名称都是Purchase.


add_filter( 'woocommerce_product_tabs', 'wc_remove_reviews_tab' );
function wc_remove_reviews_tab( $tabs ){
unset($tabs['reviews']);
return $tabs;
}

产品页添加自定义选项卡