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.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器