downloadbooks_2014 2017-04-05 08:13
浏览 52


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 it shows the product in "EN" (defualt language)

When visiting 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 关于网上一个easyx制作的见缝插针小游戏(c++)
    • ¥15 开地址法双散列函数处理碰撞
    • ¥15 想问一下这个是什么情况 虚拟机Linux打不开了
    • ¥15 联通光猫掉注册了怎么重新注册上去
    • ¥15 关于unity开发steamvr程序遇到的问题
    • ¥60 求tc downloader的下载方式
    • ¥15 华为 快捷方式 手电筒 接口
    • ¥15 Qt6.5支不支持Android13开发啊
    • ¥20 网络只能跑一半,应该如何设置
    • ¥20 Python调用百度开发者平台人脸识别接口