【问题标题】:Setting Cookie for Custom URL Parameter - wordpress/woocommerce为自定义 URL 参数设置 Cookie - wordpress/woocommerce
【发布时间】:2021-07-14 00:46:04
【问题描述】:

我创建了一个简码,我总是使用 URL 参数来动态交换主页上的特色产品,效果很好:

// Shortcode to display specific product page via URL - ?myppid=46
// =============================================================================
function ppid_from_query( $atts ) {
    $atts = shortcode_atts( array(
        'default' => 46
    ), $atts, 'myppid' );
    $pp_id = $atts['default']; 
    if ( isset( $_REQUEST['ppid'] ) && $_REQUEST['ppid'] != '' ) {
        $pp_id = intval( $_REQUEST['ppid'] );
    }
    return do_shortcode( "[product_page id='".$pp_id."']" );
    
}
add_shortcode( 'myppid', 'ppid_from_query' );

我面临的挑战是,如果用户转到我网站上的任何其他页面并返回,URL 参数就会消失,并且会显示默认产品。

我已经尝试了几种传递参数的方法,但都没有成功……即使我成功了,它也只适用于那个会话。换句话说,如果用户离开站点并在没有 ?ppid=X 的情况下返回主域,那么他们将看不到该产品。所以为此,我尝试为我找到的这个线程设置一个 cookie(当然它有点过时了):https://wordpress.stackexchange.com/questions/188749/i-am-looking-to-append-url-parameter-to-all-urls

所以在我的标题中,我有:

<script>
if(!isset($_SESSION['ppid']) and $_GET['ppid']){
$cookie_expire = time()+60*60*24*30;
$_SESSION['ppid'] = $_GET['ppid'];
setcookie('ppid', $_SESSION['ppid'], $cookie_expire, '/', '.'.$_SERVER['HTTP_HOST']);
</script>

在函数中:

function wprdcv_param_redirect(){
    if(isset($_COOKIE['ppid']) and !$_GET['ppid']){
        $location = esc_url(add_query_arg('ppid', $_COOKIE['ppid']));
        wp_redirect($location);
    }
}
add_action('template_redirect', 'wprdcv_param_redirect');

仍然没有运气。我做错了什么?

【问题讨论】:

    标签: wordpress cookies woocommerce url-parameters


    【解决方案1】:

    PHP 代码由 Web 服务器而不是浏览器处理。您不必在&lt;script&gt; 标记之间设置cookie。相反,请通过 Wordpress init 挂钩运行它。

    根据@MattMelton 的评论,我在wprdcv_param_redirect 函数中添加了结帐页面排除项。

    您可以像这样优化您的功能:

    add_shortcode( 'myppid', 'ppid_from_query' );
    function ppid_from_query( $atts ) {
        $atts = shortcode_atts( array(
            'default' => 46
        ), $atts, 'myppid' );
        $pp_id = $atts['default']; 
        if ( isset( $_REQUEST['ppid'] ) && $_REQUEST['ppid'] != '' ) {
            $pp_id = intval( $_REQUEST['ppid'] );
        }
        return do_shortcode( "[product_page id='".$pp_id."']" );
        
    }
    
    // set the cookie based on the url parameter "ppid"
    add_action( 'init', 'set_cookie_based_on_url_parameter_ppid' );
    function set_cookie_based_on_url_parameter_ppid() {
        if ( ! isset($_SESSION['ppid'] ) && $_GET['ppid'] ) {
            $cookie_expire = time()+60*60*24*30;
            $_SESSION['ppid'] = $_GET['ppid'];
            setcookie('ppid', $_SESSION['ppid'], $cookie_expire, '/', '.'.$_SERVER['HTTP_HOST']);
        }    
    }
    
    add_action('template_redirect', 'wprdcv_param_redirect');
    function wprdcv_param_redirect(){
    
        // if it's the checkout page, don't redirect
        if ( is_checkout() ) {
            return;
        }
    
        /*
        // if it is the order confirmation page in checkout, please do not redirect
        if ( is_checkout() && is_wc_endpoint_url( 'order-received' ) ) {
            return;
        }
        */
    
        if ( isset($_COOKIE['ppid']) && ! $_GET['ppid'] ) {
            $location = esc_url( add_query_arg('ppid', $_COOKIE['ppid']) );
            wp_redirect($location);
        }
    }
    

    代码已经过测试并且对我有用。将其添加到活动主题的 functions.php 中。

    【讨论】:

    • 谢谢!我现在看到它正确设置了一个 cookie ppid,但是 url 参数并没有传递到我的 WP 网站上的其他页面......知道为什么会这样吗?也许是缓存插件?
    • @ 检查 - 它似乎正在工作。也就是说,当函数“wprdcv_param_redirect”被添加到函数时,我得到一个结帐错误。该订单似乎正在执行,但页面重定向到错误:无法处理您的请求。会不会跟 url 的结尾有关系?:?key=wc_order_8WwRgMsfZkAEZ&order_id=728&ppid=46 注释掉就可以了。
    • @MattMelton 我已经更新了我的答案。您可以添加任何排除项以阻止代码运行。
    • 太棒了!喝杯咖啡吧!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    相关资源
    最近更新 更多