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 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab