简体   繁体   English

如何在 woocommerce 中为购物车创建 API?

[英]How can I create API for cart in woocommerce?

I am aware of the fact that Woocommerce provides APIs for most of its functionalities.我知道 Woocommerce 为其大部分功能提供 API。 I have gone through the below documentation我已经阅读了以下文档

Woocommerce REST API Woocommerce REST API

Now cart is one of the most important features of any e-commerce application.现在,购物车是任何电子商务应用程序最重要的功能之一。 I did some research and found that the cart works with sessions on the web.我做了一些研究,发现购物车适用于网络上的会话。 Now I am not understanding how to create API for cart in Woocommerce.现在我不明白如何在 Woocommerce 中为购物车创建 API。 I have done a lot of research but in vain.我做了很多研究,但徒劳无功。 Nothing seems to work.似乎没有任何效果。 There must be a way to do this.必须有办法做到这一点。 Any help is appreciated.任何帮助表示赞赏。

To synchronize the cart with your backend, you would need to listen for the woocommerce_add_to_cart and send information about the cart to your backend whenever an item gets added to the cart.要将购物车与您的后端同步,您需要监听woocommerce_add_to_cart并将有关购物车的信息发送到您的后端,每当有商品添加到购物woocommerce_add_to_cart

Within your functions.php add the code below to add a hook to listen for the woocommerce_add_to_cart function and run the track_add_to_cart method.在您的functions.php添加以下代码以添加一个挂钩以侦听woocommerce_add_to_cart函数并运行track_add_to_cart方法。

add_action('woocommerce_add_to_cart', 'track_add_to_cart');
function track_add_to_cart() {
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();

    foreach($items as $item => $values) { 
        $_product =  wc_get_product( $values['data']->get_id()); 
        // build a POST request and send product info to your server
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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