在Wordpress外部显示woocommerce购物车

时间:2017-05-23 10:32:35

标签: php wordpress cookies

所以我按照这个来尝试从Wordpress获取数据以显示在wordpress目录之外(n.b.我还需要安装根cookie插件):

如何在wordpress之外使用woocommerce功能

https://wordpress.org/plugins/root-cookie/

无论如何,我试图显示购物车数量,但继续得到" 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();
?>

输出:

Is logged in 
0

任何想法我做错了什么?

N.B。 Wordpress API不适合在这里使用。至于cookie,我使用根cookie来改变从/ store到/的路径,这对于会话数据很好,但是Woocommerce cookie仍然坚持使用路径/.

1 个答案:

答案 0 :(得分:0)

好的,所以我设法做到了。

在/wp-content/plugins/woocommerce/includes/wc-core-functions.php,第730行,删除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 );
    }
}