久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使 WC_Cart add_to_cart 方法為 Woocommerce 中的客人工作

Make WC_Cart add_to_cart method working for guests in Woocommerce(使 WC_Cart add_to_cart 方法為 Woocommerce 中的客人工作)
本文介紹了使 WC_Cart add_to_cart 方法為 Woocommerce 中的客人工作的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我正在嘗試讓我的代碼工作.現(xiàn)在搜索小時(shí).我發(fā)現(xiàn)了類(lèi)似的問(wèn)題,例如 這個(gè).但不幸的是,似乎沒(méi)有人找到解決辦法.

Im trying to get my code to work. Searching for hours now. I found similar questions like this one. But unfortunately noone seems to find a solution.

在我的自定義插件中,我想將特定項(xiàng)目添加到 WC 購(gòu)物車(chē)并將用戶(hù)直接重定向到結(jié)帳.作為登錄用戶(hù),它就像一個(gè)魅力,但對(duì)于客人來(lái)說(shuō),它在結(jié)賬時(shí)顯示一個(gè)空白頁(yè)面(woocommerce_checkout 簡(jiǎn)碼在這種情況下似乎沒(méi)有返回任何內(nèi)容).所以我想出了一個(gè)檢查,如果購(gòu)物車(chē)是空的.顯然是因?yàn)橘?gòu)物車(chē)頁(yè)面"顯示購(gòu)物車(chē)中還沒(méi)有商品".

In my custom plugin I want to add a specific item to the WC cart and redirect the user directly to the checkout. As a logged in user it works like a charm but for guests it shows a blank page on checkout (woocommerce_checkout shortcode seems to return nothing in this case). So I came up with a check if the cart iss till empty. Apparently it is because the "cart-page" shows "There are no items in the cart yet".

在代碼中,我檢查購(gòu)物車(chē)是否仍然是空的,但它告訴我不是!

In code i check if the cart is still empty but it tells me its not!

這是我的代碼:

if(!$wooID = $wpdb->get_var("SELECT wooID FROM ".$wpdb->prefix."ceb_events WHERE id = $event")) die("ERROR GETTING WOOID");

WC()->cart->empty_cart();

if(!WC()->cart->add_to_cart( $wooID, 1 )) die("CART GOT NOT UPDATED. THERE IS AN ERROR 1.");

if(WC()->cart->get_cart_contents_count() == 0) die("CART GOT NOT UPDATED. THERE IS AN ERROR 2."); 

//Here follows the redirect to checkout page

代碼運(yùn)行沒(méi)有錯(cuò)誤.它以登錄用戶(hù)/管理員身份 100% 工作.只是不是作為客人,即使我允許在 woocommerce 設(shè)置中進(jìn)行客人結(jié)帳.

The code runs without errors. And it works 100% as a logged in user / admin. Just not as guest, even tho I allowed the guest checkout in the woocommerce settings.

推薦答案

您需要在未登錄的情況下啟動(dòng) Woocommerce User 會(huì)話(huà).因此您將使用以下內(nèi)容:

You need to initiate Woocommerce User session when it's not logged in. So you will use the following:

add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' );
function force_non_logged_user_wc_session(){ 
    if( is_user_logged_in() || is_admin() )
       return;

    if ( isset(WC()->session) && ! WC()->session->has_session() ) 
       WC()->session->set_customer_session_cookie( true ); 
}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的 functions.php 文件中.它應(yīng)該有效.

Code goes in functions.php file of your active child theme (or active theme). It should works.

這篇關(guān)于使 WC_Cart add_to_cart 方法為 Woocommerce 中的客人工作的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話(huà)字段驗(yàn)證問(wèn)題中添加自定義注冊(cè)字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡(jiǎn)單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 日韩视频在线观看免费 | 亚洲一区免费观看 | 香蕉视频导航 | 三级视频在线 | 免费看黄色网址 | 蜜桃91丨九色丨蝌蚪91桃色 | 欧美日韩免费一区二区三区 | 精品久久一区二区 | 求av网址 | 国产精品视屏 | 一级免费片| 亚洲天堂一区 | 影音先锋中文字幕在线 | 日韩欧美在线观看视频 | 国产一区二区自拍 | 免费毛片在线 | 国产精品自拍av | 蜜桃色一区二区三区 | 久久精品毛片 | 中文字幕免费观看 | 在线免费看黄色 | 亚洲精品美女 | 一级片黄色 | 日韩一区二区在线播放 | 欧美激情在线观看 | 国产精品久久久久久久久久 | 亚洲黄色大片 | 免费福利在线观看 | 女人久久久 | 成人91看片 | 亚洲精品久久久久久久久久久 | 免费一级a毛片 | 亚洲精品91 | 97久久超碰 | 手机看片1024日韩 | 少妇xxxx69 | 日韩精品不卡 | 黄色片免费看 | 天天看片天天爽 | 国产第五页 | 国产精品麻豆免费版 |