采集数据产品描述有超链接///设置免运费后,达到免送标准,其他运费不显示///给产品详情页面的图片点击放大是个模态窗///在shop页面有重复的产品展示,去重

//产品描述有超链接,去掉
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\">&times;</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' ); // 升序或降序
}



//去重1.20版本
add_action('woocommerce_product_query', 'filter_and_randomize_products');
function filter_and_randomize_products($query) {
if (is_shop() || is_product_category() || is_product_tag()) {
global $wpdb;

// 获取所有唯一的产品标题
$product_titles = $wpdb->get_col("
SELECT DISTINCT post_title
FROM {$wpdb->posts}
WHERE post_type = 'product'
AND post_status = 'publish'
");

// 获取每个标题对应的最小ID
$product_ids = [];
foreach ($product_titles as $title) {
$product_id = $wpdb->get_var($wpdb->prepare("
SELECT MIN(ID)
FROM {$wpdb->posts}
WHERE post_title = %s
AND post_type = 'product'
AND post_status = 'publish'
", $title));
$product_ids[] = $product_id;
}

// 打乱产品ID顺序
shuffle($product_ids);

// 修改查询参数
$query->set('post__in', $product_ids);
$query->set('orderby', 'post__in');
}
}



 

posted @ 2024-05-24 17:08  还好阿卡  Views(3)  Comments(0Edit  收藏  举报