【问题标题】:Woocommerce add_to_cart not working if cart is emptyWoocommerce add_to_cart not working if cart is empty
【发布时间】:2022-12-26 19:52:01
【问题描述】:

I have made a simple script in PHP to add a product to the WooCommerce cart:

WC()->cart->add_to_cart(43, 2);

The code is made in a custom plugin.

However, if the product is added as the first pproduct in the cart, the function is not working. If I add another product to the cart, and then run the script, is works just fine.

I think it is because the cart is somehow not initialized the right way/session is not created?

What am I missing?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    Try following:

    session_start();
    WC()->cart->set_cart_cookies();
    WC()->cart->add_to_cart(43,2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-03
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 2022-11-20
      相关资源
      最近更新 更多