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 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线
  • ¥20 无法创建新的堆栈防护界面