douzhantanju1849 2019-07-12 16:23
浏览 58

检查匿名客户是否已经在WooCommerce中购买了特定产品

I want to mark not registered customers that already bought a product using either a cookie, a session variable, or some other way.

I have attempted it with the code below, but this approach doesn't seem to be working. In the first approach, I try to save something. Then, I try to get the session cookie already in use by woocommerce to save it in the database as a unique identifier. After, I can read it out of the database to see if it's a new unique user or not.

add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
add_action( 'woocommerce_order_status_completed', 'so_payment_complete' );
function so_payment_complete( $order_id){
    setcookie('so_payment_complete',  1, time()+7200);
    WC()->session->set( 'so_payment_complete',true );

    $session = new WC_Session_Handler();
    $session_data = $session->get_session_data();
    file_put_contents(__DIR__ . '/return_products.txt','cookies:'.(print_r($_COOKIE,true)) . "
" ,FILE_APPEND );
    file_put_contents(__DIR__ . '/return_products.txt','get_session_data:'.(print_r($session_data,true)) . "
" ,FILE_APPEND );
}

It seems the cookie from setcookie never reaches the client. WC()->session->set never reaches the database. WC()->session->get cannot get it on other pages either.

$_COOKIE and $session_data arrays are empty.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。
    • ¥15 livecharts wpf piechart 属性
    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了
    • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题