WordPress WooCommerce开发常见代码连载(三)

2017-04-06 点击:1300

上次写了WooCommerce开发常见代码连载二,今天继续WooCommerce主题项目开发中遇到常见代码及问题解决,这里记录下来,方便以后查找。

订单提交页面获取商品属性信息

$cart_item是一个数组类型的数据,包含了我们在后台给商品添加的各种信息,假设我们在后台给商品添加了颜色和尺寸等属性,那么可以通过下面的方式获取属性

 echo $cart_item['variation']['attribute_pa_size'];  
 echo $cart_item['variation']['attribute_pa_color']; 

自定义的属性包含在$cart_item['variation']这个数组中,属性名字是一个拼接的字符串


CheckOut页面检查支付方式

woocommerce定义了函数来检查当前是否拥有有效的支付方式,如果后台设置了有效的支付方式,这里函数返回ture

WC()->cart->needs_payment()

移除下单页面select2下拉选择框

在你的functions.php中添加下面的代码

     add_action( 'wp_enqueue_scripts', 'mgt_dequeue_stylesandscripts', 100 );
function mgt_dequeue_stylesandscripts() {
    if ( class_exists( 'woocommerce' ) ) {
        wp_dequeue_style( 'select2' );
        wp_deregister_style( 'select2' );
        wp_dequeue_script( 'select2');
        wp_deregister_script('select2');

    } 
} 

修改可变商品价格区间变成起价

add_filter( 'woocommerce_variable_sale_price_html', 'bbloomer_variation_price_format', 10, 2 ); 
add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format', 10, 2 ); 
function bbloomer_variation_price_format( $price, $product ) { 
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( '%1$s 起', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] ); 
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] ); 
if ( $price !== $saleprice ) {
$price = '' . $saleprice . ' ' . $price . '';
}
return $price;
}

修改产品页面和列表页面加入购物车按钮的文字

/**
 * 产品页面的按钮文字
 */
function woo_custom_cart_button_text() {
return __('按钮文字', 'woocommerce');
}
add_filter('single_add_to_cart_text', 'woo_custom_cart_button_text');



/**
 * 产品列表页面的按钮文字
 */
function woo_archive_custom_cart_button_text() {
return __( '按钮文字', 'woocommerce' );
}
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' );


隐藏checkout页面的字段

通过unset来移除checkout页面相关的字段

function custom_override_checkout_fields( $fields ) {
if(IsChinaMarket())
{
unset($fields['billing']['billing_first_name']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_address_1']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_city']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_state']);
    unset($fields['billing']['billing_phone']);
    unset($fields['order']['order_comments']);
    unset($fields['billing']['billing_address_2']);
    unset($fields['billing']['billing_postcode']);
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_last_name']);
    unset($fields['billing']['billing_email']);
    unset($fields['billing']['billing_city']);
}	

    return $fields;
}

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

结账页面的账单详情-添加购物者的年龄

在结帐页面加入其他自定义内容,比如购物者年龄

//个性化结账页面的账单详情-添加购物者的年龄
add_filter( 'woocommerce_checkout_fields' , 'custom_add_checkout_fields_age' );  
function custom_add_checkout_fields_age( $fields ) { 

$fields['billing']['billing_Shoppers_age '] = array(  
        'label'         => __('年龄', 'woocommerce'),//名称  
        'placeholder'   => _x('购物者年龄', 'placeholder', 'woocommerce'),//占位文字  
        'required'      => true,//是否必填项  
        'class'         => array('form-row-wide'),//类  
        'clear'         => true//是否清除浮动  
     );  
  
  $fields['billing']['billing_Shoppers_weixin'] = array(  
        'label'         => __('微信号', 'woocommerce'),//名称  
        'placeholder'   => _x('购物者微信号', 'placeholder', 'woocommerce'),//占位文字  
        'required'      => false,//是否必填项  
        'class'         => array('form-row-wide'),//类  
        'clear'         => true//是否清除浮动  
     ); 
return $fields;  
} 

woocommerce修改logout redirect,比如退出登录到首页

function cj_auto_redirect_after_logout(){  
  wp_redirect( home_url() );  
  exit();  
}  
add_action('wp_logout','cj_auto_redirect_after_logout');

立即加【QQ:6347007】试用WordPress支付插件

相关产品

  • WordPress WooCommerce微信支付插件红包版
    WordPress WooCommerce微信支付插件红包版

相关博客

  • WordPress WooCommerce开发常见代码连载(二)

  • WordPress WooCommerce主题开发常见代码连载(一)