File: /www/wwwroot/1.scey.cn/wp-content/themes/Leading/woocommerce/woocommerce-functions.php
<?php
//禁止加载https://stats.wp.com/w.js
if ( is_admin() ) {
function remove_woo_tracks() {
wp_deregister_script( 'woo-tracks' );
}
add_action('init', 'remove_woo_tracks');
}
// js css
function dahuzi_enqueue_scripts() {
if (!is_admin()) {
wp_enqueue_style( 'dashicons');
wp_enqueue_style('woocommerce_css', get_stylesheet_directory_uri().'/woocommerce/static/css/woocommerce.css');
wp_enqueue_script('woocommerce_js', get_stylesheet_directory_uri() . '/woocommerce/static/js/woocommerce.js', ['jquery'], '', true);
}
}
add_action( 'wp_enqueue_scripts', 'dahuzi_enqueue_scripts' );
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
add_action( 'woocommerce_product_thumbnails', 'dahuzi_show_product_thumbnails', 20 );
add_action( 'woocommerce_before_single_product_summary', 'dahuzi_show_product_image', 10 );
// Single Product Image
function dahuzi_show_product_image() {
// Woocmmerce 3.0+ Slider Fix
require_once 'single-product/product-image.php';
}
// Single Product Thumbnails
function dahuzi_show_product_thumbnails() {
// Woocmmerce 3.0+ Slider Fix
require_once 'single-product/product-thumbnails.php';
}
// 为购买数量添加加号和减号
add_action( 'woocommerce_after_add_to_cart_quantity', 'bbloomer_display_quantity_plus' );
function bbloomer_display_quantity_plus() {
echo '<button type="button" class="plus" >+</button>';
}
add_action( 'woocommerce_before_add_to_cart_quantity', 'bbloomer_display_quantity_minus' );
function bbloomer_display_quantity_minus() {
echo '<span class="label">数量</span><button type="button" class="minus" >-</button>';
}
add_action( 'wp_footer', 'bbloomer_add_cart_quantity_plus_minus' );
function bbloomer_add_cart_quantity_plus_minus() {
// Only run this on the single product page
if ( ! is_product() ) return;
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('form.cart').on( 'click', 'button.plus, button.minus', function() {
// Get current quantity values
var qty = $( this ).closest( 'form.cart' ).find( '.qty' );
var val = parseFloat(qty.val());
var max = parseFloat(qty.attr( 'max' ));
var min = parseFloat(qty.attr( 'min' ));
var step = parseFloat(qty.attr( 'step' ));
// Change the value if plus or minus
if ( $( this ).is( '.plus' ) ) {
if ( max && ( max <= val ) ) {
qty.val( max );
} else {
qty.val( val + step );
}
} else {
if ( min && ( min >= val ) ) {
qty.val( min );
} else if ( val > 1 ) {
qty.val( val - step );
}
}
});
});
</script>
<?php
}
// 移除产品描述 TAB菜单栏下的描述
add_filter( 'woocommerce_product_description_heading', 'remove_product_description_heading' );
function remove_product_description_heading() {
return '';
}
add_filter( 'woocommerce_product_additional_information_heading', '__return_false' );
// 移除SKU选项,一般情况下无用
add_filter( 'wc_product_sku_enabled', 'dahuzi_remove_product_page_sku' );
function dahuzi_remove_product_page_sku( $enabled ) {
//if ( ! is_admin() && is_product() ) {
//return false; //只在前台移除
//}
return false; //前后台全部移除
return $enabled;
}
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
// 将产品变量变为单选,需要配合js使用
function variation_radio_buttons($html, $args) {
$args = wp_parse_args(apply_filters('woocommerce_dropdown_variation_attribute_options_args', $args), array(
'options' => false,
'attribute' => false,
'product' => false,
'selected' => false,
'name' => '',
'id' => '',
'class' => '',
'show_option_none' => __('Choose an option', 'woocommerce'),
));
if(false === $args['selected'] && $args['attribute'] && $args['product'] instanceof WC_Product) {
$selected_key = 'attribute_'.sanitize_title($args['attribute']);
$args['selected'] = isset($_REQUEST[$selected_key]) ? wc_clean(wp_unslash($_REQUEST[$selected_key])) : $args['product']->get_variation_default_attribute($args['attribute']);
}
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_'.sanitize_title($attribute);
$id = $args['id'] ? $args['id'] : sanitize_title($attribute);
$class = $args['class'];
$show_option_none = (bool)$args['show_option_none'];
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __('Choose an option', 'woocommerce');
if(empty($options) && !empty($product) && !empty($attribute)) {
$attributes = $product->get_variation_attributes();
$options = $attributes[$attribute];
}
$radios = '<div class="variation-radios">';
if(!empty($options)) {
if($product && taxonomy_exists($attribute)) {
$terms = wc_get_product_terms($product->get_id(), $attribute, array(
'fields' => 'all',
));
foreach($terms as $term) {
if(in_array($term->slug, $options, true)) {
$radios .= '<span><input type="radio" name="'.esc_attr($name).'" value="'.esc_attr($term->slug).'" id="'.esc_attr($term->slug).'" '.checked(sanitize_title($args['selected']), $term->slug, false).'><label for="'.esc_attr($term->slug).'">'.esc_html(apply_filters('woocommerce_variation_option_name', $term->name)).'</label></span>';
}
}
} else {
foreach($options as $option) {
$checked = sanitize_title($args['selected']) === $args['selected'] ? checked($args['selected'], sanitize_title($option), false) : checked($args['selected'], $option, false);
$radios .= '<input type="radio" name="'.esc_attr($name).'" value="'.esc_attr($option).'" id="'.sanitize_title($option).'" '.$checked.'><label for="'.sanitize_title($option).'">'.esc_html(apply_filters('woocommerce_variation_option_name', $option)).'</label>';
}
}
}
$radios .= '</div>';
return $html.$radios;
}
add_filter('woocommerce_dropdown_variation_attribute_options_html', 'variation_radio_buttons', 20, 2);
// 立即购买
function buy_now_submit_form() {
?>
<script>
jQuery(document).ready(function(){
// listen if someone clicks 'Buy Now' button
jQuery('#buy_now_button').click(function(){
// set value to 1
jQuery('#is_buy_now').val('1');
//submit the form
jQuery('form.cart').submit();
});
});
</script>
<?php
}
add_action('woocommerce_after_add_to_cart_form', 'buy_now_submit_form');
// 重定向到结账页面
add_filter('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout($redirect_url) {
if (isset($_REQUEST['is_buy_now']) && $_REQUEST['is_buy_now']) {
global $woocommerce;
$redirect_url = wc_get_checkout_url();
}
return $redirect_url;
}
// 取消“相关产品”展示
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
add_action( 'get_header', 'remove_storefront_sidebar' );
function remove_storefront_sidebar() {
if ( is_shop() ) {
remove_action( 'storefront_sidebar', 'storefront_get_sidebar', 10 );
}
}
// 更改文章缩略图尺寸
if ( ! function_exists( 'WooCommerce_template_loop_product_thumbnail' ) ) {
function WooCommerce_template_loop_product_thumbnail() {
echo WooCommerce_get_product_thumbnail( 'full');
}
}
// 移除结账页面的某些字段
//add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
//function custom_override_checkout_fields( $fields ) {
//unset($fields['order']['order_comments']); //订单备注
//unset( $fields['billing']['billing_country'] ); //国家/地区
//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_state'] ); //省
//unset( $fields['billing']['billing_postcode'] ); //邮政编码
//unset($fields['billing']['billing_email']); //邮箱
//unset( $fields['billing']['billing_phone'] ); //电话
//return $fields;
//}
// 自定义结账页面字段显示顺序
add_filter( 'woocommerce_default_address_fields' , 'dahuzi_billing_fields_custom' );
function dahuzi_billing_fields_custom( $fields ) {
$fields['last_name']['priority'] = 10;
$fields['first_name']['priority'] = 20;
$fields['phone']['priority'] = 30;
$fields['country']['priority'] = 40;
$fields['state']['priority'] = 50;
$fields['city']['priority'] = 60;
$fields[ 'city' ][ 'label' ] = '城市';
$fields['address_1']['priority'] = 70;
$fields[ 'address_1' ][ 'label' ] = '县城/街道地址';
$fields['address_2']['priority'] = 80;
$fields[ 'country' ][ 'class' ][] = 'is-hidden'; //隐藏国家
return $fields;
}
// 重定义账户菜单文本
add_filter( 'woocommerce_account_menu_items', 'rename_menu_items' );
function rename_menu_items( $items ) {
$items['dashboard'] = '我的账户';
$items['edit-account'] = '个人资料';
$items['edit-address'] = '收货地址';
$items['customer-logout'] = '退出登录';
$items['downloads'] = '资源下载';
$items['orders'] = '订单中心';
return $items;
}
// 移除账单地址的某些字段
add_filter( 'woocommerce_billing_fields', 'dahuzi_remove_billing_fields' );
function dahuzi_remove_billing_fields( $fields ) {
//unset($fields['order_comments']); //订单备注
//unset( $fields['billing_country'] ); //国家/地区
//unset( $fields['billing_first_name'] ); //名字
//unset( $fields['billing_last_name'] ); //姓氏
unset( $fields['billing_company'] ); //公司
//unset( $fields['billing_address_1'] ); //街道地址
//unset( $fields['billing_address_2'] ); //小区,楼号,单元等
//unset( $fields['billing_city'] ); //市
//unset( $fields['billing_state'] ); //省
unset( $fields['billing_postcode'] ); //邮政编码
//unset($fields['billing_email']); //邮箱
//unset( $fields['billing_phone'] ); //电话
return $fields;
}