简体   繁体   中英

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):

How to use woocommerce functions outside of wordpress

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:

<?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. 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 /.

Okay so I managed to do this.

In /wp-content/plugins/woocommerce/includes/wc-core-functions.php, line 730, remove the COOKIEPATH ? COOKIEPATH : COOKIEPATH ? COOKIEPATH : , so that you are left with:

/**
 * 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 );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM