最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - How to call WooCommerce update cart function programatically

programmeradmin1浏览0评论

I am rendering WooCommerce Cart items in a custom html table wrapper by a form that points to a custom PHP file.

<form class="woocommerce-cart-form" action="/update-cart.php" method="POST">
    ...
    <input type="submit" name="update_cart" value="Update cart">
</form>

When I click on update cart, I get redirected via POST to update-cart.php where I do some business related actions with the PHP $_REQUEST.

After doing those actions, I want to call to the WooCommerce original update action, but I am stucked.

This is what I have tried:

// Non of this has worked for me

do_action('woocommerce_update_cart_action_cart_updated'); // 1

WC()->cart->persistent_cart_update(); // 2

do_action('update_cart_action'); // 3

How should I call WooCommerce's update action?

Thank you.

I am rendering WooCommerce Cart items in a custom html table wrapper by a form that points to a custom PHP file.

<form class="woocommerce-cart-form" action="/update-cart.php" method="POST">
    ...
    <input type="submit" name="update_cart" value="Update cart">
</form>

When I click on update cart, I get redirected via POST to update-cart.php where I do some business related actions with the PHP $_REQUEST.

After doing those actions, I want to call to the WooCommerce original update action, but I am stucked.

This is what I have tried:

// Non of this has worked for me

do_action('woocommerce_update_cart_action_cart_updated'); // 1

WC()->cart->persistent_cart_update(); // 2

do_action('update_cart_action'); // 3

How should I call WooCommerce's update action?

Thank you.

Share Improve this question asked Feb 11, 2021 at 18:41 rogervilarogervila 1131 silver badge5 bronze badges 5
  • 1 What exactly are you trying to update in the cart? The update_cart form handler is WC_Form_Handler::update_cart_action(); you could read that and see how it does whatever you want to do. – Rup Commented Feb 12, 2021 at 15:07
  • I cannot instantiate WC_Form_Handler class, I am getting a 'class not found'. How can I get a WooCommerce class instance? – rogervila Commented Feb 12, 2021 at 16:15
  • 1 I don't think you should be, unless you want to process updates from a posted form? And it's a static method anyway: I'd guess you'll have to require_once wp-content/plugins/woocommerce/includes/class-wp-form-handler.php if it really isn't found, and then you can call WC_Form_Handler::update_cart_action() statically. – Rup Commented Feb 12, 2021 at 16:36
  • 1 Actually looking at class-woocommerce.php, WC_Form_Handler is loaded automatically for all frontend requests, so you shouldn't have to load it yourself. But if you really do have to, try include_once WC_ABSPATH . 'includes/class-wc-form-handler.php'; – Rup Commented Feb 12, 2021 at 16:52
  • You are right, the class is already loaded. If you answer the question I will mark it as the solution. Thank you very much. – rogervila Commented Feb 12, 2021 at 17:17
Add a comment  | 

1 Answer 1

Reset to default 1

The default form handler is a static method which you can call with

WC_Form_Handler::update_cart_action();
发布评论

评论列表(0)

  1. 暂无评论