The Most Active and Friendliest
Affiliate Marketing Community Online!

“Adavice”/  “CPA

I can't access cart from PHP

Ms Montana

New Member
affiliate
I am new to developing with Woocommerce and have successfully added products and coupons using PHP but cannot work out why I cannot access the cart from PHP? I have trawled google for ideas but nothing works - all the woocommerce calls I try return null. For example:

$cart_url = WC()->cart->get_cart_url();

I'm trying to remove products from the cart from within one of my functions called when processing form data. Am I missing a step to get the cart available to PHP? I tried a simple hook as a test and this worked:x
add_action('the_content', 'dump_woocommerce_cart');
function dump_woocommerce_cart() {
global $woocommerce;
print_r(WC()->cart->cart_contents);


}
but I don't understand why it works in that hook but not within my PHP function (which processes the response from a form using the admin_post_ action hook.

Any help gratefully received as I am now pulling my hair out.
 
MI
Back