//產品描述有超連結,去掉 function remove_product_hyperlinks($content) { if (is_product()) { // 確保只在產品頁面上應用 $content = preg_replace('/<a href=".*?">(.*?)<\/a>/', '$1', $content); } return $content; } add_filter('the_content', 'remove_product_hyperlinks');
// 設定免運費後,達到免送標準,其他運費不顯示 function js_hide_all_shipping_when_free_is_available( $shipping_rates ) { foreach ( $shipping_rates as $key => $rate ) { // Check if current rate is 'free_shipping' if ( $rate->get_method_id() == 'free_shipping' ) { $shipping_rates = array( $key => $rate ); } } return $shipping_rates; } add_filter( 'woocommerce_package_rates', 'js_hide_all_shipping_when_free_is_available' );
//給產品詳情頁面的圖片點選放大是個模態窗 function theme_enqueue_scripts_and_styles() { wp_enqueue_style('custom-style', get_stylesheet_directory_uri() . '/css/custom-style.css'); wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), null, true); $inline_script = " jQuery(document).ready(function($) { $('body').append('<div id=\"myModal\" class=\"modal\"><span class=\"close\">×</span><img class=\"modal-content\" id=\"img01\"></div>'); // CSS for the modal var modal_css = '<style>\ .modal {display: none; position: fixed; z-index: 1000; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.9);}\ .modal-content {margin: auto; display: block; max-width: 40%;}\ .close {position: absolute; top: 15px; right: 35px; color: white; font-size: 40px; font-weight: bold; cursor: pointer;}\ </style>'; $('head').append(modal_css); $('.woocommerce-product-gallery__image a').click(function(event) { event.preventDefault(); var imgSrc = $(this).find('img').attr('src'); $('#img01').attr('src', imgSrc); $('#myModal').css('display', 'block'); }); $('.close').on('click', function() { $('#myModal').css('display', 'none'); }); }); "; wp_add_inline_script('custom-script', $inline_script); } add_action('wp_enqueue_scripts', 'theme_enqueue_scripts_and_styles');
//在shop頁面有重複的產品展示,去重 add_filter( 'posts_groupby', 'custom_posts_groupby', 10, 2 ); function custom_posts_groupby( $groupby, $query ) { global $wpdb; // 只在前端的WooCommerce商品頁面上應用 if ( ! is_admin() && $query->is_main_query() && ( is_shop() || is_product_category() || is_product_tag() ) ) { // 新增我們自己的groupby邏輯,基於商品標題 $groupby = "{$wpdb->posts}.post_title"; } return $groupby; } add_action( 'woocommerce_product_query', 'custom_product_query' ); function custom_product_query( $q ) { $q->set( 'posts_per_page', 12 ); // 設定每頁顯示的產品數量,根據需要更改 $q->set( 'orderby', 'title' ); // 基於產品標題排序 $q->set( 'order', 'ASC' ); // 升序或降序 }