downloadbooks_2014 2017-04-05 08:13
浏览 52
已采纳

在Cookie中保存产品ID不适用于两种语言wordpress

I have a wishlist page which shows all saved products. Products are saved in a cookie and that can be done through a click of a button when viewing a single product.

On that single product, there is an icon with two states, selected or unselected. The purpose is "Save for later". If the specific product is already in the cookie list then i add "selected" class, so the icon changes properly in order to let the user know that he already had "save for later" this product.

An example of cookie with 3 products : 185-589-382

That means we have three products with the ID's 185, 589, 382.

Every product has different ID for each language. That means a product in "EN"(english) has ID 100, but in the other language the ID may be 252.

Example :

Let's suppose i have only one product which has the ID 100 in "EN". That product, in the other language has the ID 200.

When visiting mysite.com/wishlist it shows the product in "EN" (defualt language)

When visiting mysite.com/el/wishlist it shows the product in "GR".

It works just fine even though they have different ID's. (probably because there is a connection between them through the plugin i am using)

My problem

When viewing a single product in "EN", which has the id 100, supposing that id is already in the cookie list - the icon has the selected state. When i press the language switcher, it switches to the other language showing the product but the icon is not in the selected state. I know this happens because in the cookie list i have saved the product with the id 100 (which is in "EN" only) and not 200 which is the other's language ID.

Is there any workaround?

Using WPML Plugin.

  • Didn't post any code because i don't have a problem with the coding but if that's going to help you then feel free to ask.
  • Currently searching a way to find how these posts are connected so maybe i change what i save into my cookie list. One way would be to save all the id's of each language but i would like to keep it clean - find the best way.
  • 写回答

1条回答 默认 最新

  • dongtao4890 2017-04-05 08:54
    关注

    Found a solution using the below WPML function :

    icl_object_id(get_the_ID(), 'post', false, ICL_LANGUAGE_CODE);
    

    This function returns the post id of "ICL_LANGUAGE_CODE" - which means current language.

    So, i stored in an array $mLanguages all the available languages. Then, used a for loop to store in $mIDArray all the ID's in every language of current product :

    foreach($mLanguages as $language) {
        $mIDArray[] = icl_object_id($mCurrentPostID, 'post', false, $language);
    }
    

    Below is the code block which was checking if post id was in the cookie list before :

    if(isset($_COOKIE['mSavedList'])) {
        $mListOfSavedItems = $_COOKIE['mSavedList'];
        $mListOfSavedItems = explode('-', $mListOfSavedItems);
    
        if(in_array(get_the_ID(), $mListOfSavedItems))
            $isCurrentProductSaved = true;
    }
    

    $isCurrentProductSaved is the "selected" statement i was talking about. So i changed it to :

    if(isset($_COOKIE['mSavedList'])) {
        $mListOfSavedItems = $_COOKIE['mSavedList'];
        $mListOfSavedItems = explode('-', $mListOfSavedItems);
    
        foreach($mListOfSavedItems as $item){
    
            if($mCurrentPostID == $item)
                $isCurrentProductSaved = true;
            else if(in_array($item, $mIDArray))
                $isCurrentProductSaved = true;
    
        }
    }
    

    In other words :

    Before i was checking if current post id exists in my cookie list. Now i am checking if current post id ( or all translatables ID's of this post) exists in my cookie list.

    Found a bug though :

    I was saving current post's id into the cookie list no matter the language. To make it work, i always save the posts id of "EN" which is the default language.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题