Reputation: 13
I am trying to access the cart data of a WooCommerce cart containing bundled subscription products. I am using both the WooCommerce Product Bundles and Subscriptions plug ins.
Ultimately I need the cart items' permalinks including the URL parameters but at this stage I can't even make WC var_dump()
the cart contents.
There is probably an obvious error in my PHP - what am I doing wrong?
Thanks!
function playboi_carti() {
/* none of these three things works, trying each independently */
/*
if (WC()->cart->get_cart() != NULL) {
var_dump(WC()->cart->get_cart());
} else {
echo('No cart item!');
}
*/
/*
foreach ( WC()->cart->get_cart() as $cart_item ) {
var_dump($cart_item);
}
*/
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_permalink = $_product->get_permalink( $cart_item );
var_dump($product_permalink);
}
}
add_action( 'init', 'playboi_carti' );
The stack trace throws essentially the same error whenever I try the above in any combination:
Uncaught Error: Call to a member function get_cart() on null in wp-content/themes/canine-catering-theme/functions.php:71
Stack trace:
#0 wp-includes/class-wp-hook.php(307): playboi_carti()
#1 wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters()
#2 wp-includes/plugin.php(476): WP_Hook->do_action()
#3 wp-settings.php(598): do_action()
#4 wp-config.php(105): require_once('wp-...')
#5 wp-load.php(50): require_once('wp-...')
#6 wp-admin/admin.php(34): require_once('wp-...')
#7 wp-admin/theme-editor.php(10): require_once('wp-...')
#8 {main}
thrown
When I try the following (from this SO answer):
add_action('init', 'dump_woocommerce_cart');
function dump_woocommerce_cart() {
global $woocommerce;
var_dump($woocommerce->cart);
}
I do get a var_dump of an empty cart even though there are (bundled subscription) products in the cart. Also apparently it's bad to use the $woocommerce global??
Is this lack of cart to do with the Product Bundles or Subscriptions plug ins?
Upvotes: 0
Views: 245