[英]Display woocommerce cart total outside Wordpress
so I followed this to try and get data from Wordpress to display outside the wordpress directory (nb I also needed to install the root cookie plugin): 因此,我按照此方法尝试从Wordpress中获取数据以在wordpress目录外显示(nb,我还需要安装根cookie插件):
How to use woocommerce functions outside of wordpress 如何在Wordpress之外使用woocommerce功能
https://wordpress.org/plugins/root-cookie/ https://wordpress.org/plugins/root-cookie/
Anyway, I'm trying to display the cart count, but keep getting "0" even though the cart has items: 无论如何,我试图显示购物车数量,但是即使购物车中有物品,也要保持“ 0”:
<?php include ($_SERVER['DOCUMENT_ROOT']."/store/wp-load.php")?>
<?php if (is_user_logged_in()){
echo "Is logged in";
}
else{
echo "Is not";
} ?> <Br />
<?php
echo WC()->cart->get_cart_contents_count();
?>
Output: 输出:
Is logged in
0
Any ideas what I'm doing wrong? 有什么想法我做错了吗?
NB the Wordpress API is not suitable for use here. 注意,Wordpress API不适合在此处使用。 As for cookies, I am using root cookie to change the path from /store to / which works fine for session data but the Woocommerce cookies are still stuck with the path /. 至于cookie,我正在使用根cookie来将路径从/ store更改为/,这对于会话数据来说效果很好,但是Woocommerce cookie仍然停留在/路径上。
Okay so I managed to do this. 好吧,我设法做到了。
In /wp-content/plugins/woocommerce/includes/wc-core-functions.php, line 730, remove the COOKIEPATH ? COOKIEPATH :
在/wp-content/plugins/woocommerce/includes/wc-core-functions.php的第730行中,删除COOKIEPATH ? COOKIEPATH :
COOKIEPATH ? COOKIEPATH :
, so that you are left with: COOKIEPATH ? COOKIEPATH :
,这样您就剩下了:
/**
* Set a cookie - wrapper for setcookie using WP constants.
*
* @param string $name Name of the cookie being set.
* @param string $value Value of the cookie.
* @param integer $expire Expiry of the cookie.
* @param string $secure Whether the cookie should be served only over https.
*/
function wc_setcookie( $name, $value, $expire = 0, $secure = false ) {
if ( ! headers_sent() ) {
setcookie( $name, $value, $expire, '/', COOKIE_DOMAIN, $secure );
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
headers_sent( $file, $line );
trigger_error( "{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.