【问题标题】:Change FlexSlider options on mobile in Woocommerce在 Woocommerce 中更改移动设备上的 FlexSlider 选项
【发布时间】:2023-03-17 22:09:01
【问题描述】:

默认情况下,在单个产品页面上启用选项 'controlNav' = 'thumbnails'。台式机没问题。但在移动设备上,我想成为 'controlNav' = true(点)。我尝试使用 ajax 来实现,但我认为我需要以某种方式使用 flex 幻灯片刷新该片段以应用过滤器。看不懂。

在JS文件中:

if (window.matchMedia('(max-width: 800px)').matches) {
      
        $.ajax({
            url: wc_add_to_cart_params.ajax_url,
            data: { action: 'mobile_slider'},
            type: 'POST'
        })
        .then(res => console.log('works', res))
    }

在functions.php中:

function hellenik_change_slider_mobile()
{
    add_filter('woocommerce_single_product_carousel_options', 'hellenik_update_woo_flexslider_options');

    function hellenik_update_woo_flexslider_options($options)
    {

        $options['smoothHeight'] = true;
        $options['controlNav'] = true;
        $options['animation'] = "slide";
        $options['slideshow'] = false;



        return $options;
    }
    
    wp_die();
}
add_action('wp_ajax_mobile_slider', 'hellenik_change_slider_mobile');
add_action('wp_ajax_nopriv_mobile_slider', 'hellenik_change_slider_mobile');

【问题讨论】:

    标签: javascript php wordpress woocommerce flexslider


    【解决方案1】:

    以下使用 WooCommerce 专用的woocommerce_single_product_carousel_options 过滤器钩子和使用 WordPress 的wp_is_mobile() 条件标签:

    add_filter( 'woocommerce_single_product_carousel_options', 'filter_single_product_carousel_options' );
    function filter_single_product_carousel_options( $options ) {
        if ( wp_is_mobile() ) {
            $options['smoothHeight'] = true; // Already "true" by default
            $options['controlNav'] = true; // Option 'thumbnails' by default
            $options['animation'] = "slide"; // Already "slide" by default
            $options['slideshow'] = false; // Already "false" by default
        }
        return $options;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    见 WooCommerce related default settings for woocommerce_single_product_carousel_options hook:

    'flexslider' => apply_filters( 'woocommerce_single_product_carousel_options',
        array(
            'rtl'            => is_rtl(),
            'animation'      => 'slide',
            'smoothHeight'   => true,
            'directionNav'   => false,
            'controlNav'     => 'thumbnails',
            'slideshow'      => false,
            'animationSpeed' => 500,
            'animationLoop'  => false, // Breaks photoswipe pagination if true.
            'allowOneSlide'  => false,
        )
    ),
    

    文档:WordPress Developer Resources - wp_is_mobile() conditional function

    【讨论】:

    • 我注意到上述方法的一个问题,当您只使用数组中的几个选项时,它会忽略其他选项。就我而言,调用过滤器中的所有选项以使其正常工作很重要。此外,wp_is_mobile() 似乎不适用于 Apple 移动设备。为此我想参考:wp_is_mobile function not working.
    猜你喜欢
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    相关资源
    最近更新 更多