【问题标题】:WooCommerce 502 Bad gateway when using add_to_cart functionWooCommerce 502 使用 add_to_cart 功能时网关错误
【发布时间】:2017-11-01 18:04:50
【问题描述】:

正如标题所说,当我尝试使用来自 WooCommerce 的标准函数 add_to_cart() 时,我收到错误 502 Bad Gateway

WC()->cart->add_to_cart( 8622 );

有什么想法吗?我还尝试向函数添加更多参数,例如数量等,但似乎没有任何改变......

WooCommerce 文档:https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html

【问题讨论】:

  • 第 1 步)检查您的 PHP 和 Web 服务器日志。
  • @AlexHowansky error.log: 2017/11/01 18:11:35 [error] 46200#46200: *2480780 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 92.53.xx.xxx, server: www.xxx.com.au, request: "GET /cart/ HTTP/1.0", upstream: "fastcgi://unix:/var/run/php7-fpm-xxx.sock:", host: "xxx.com"

标签: php wordpress woocommerce


【解决方案1】:

好的,我已经设法通过在 function.php 文件中调用另一个函数来解决这个问题。我不知道为什么它现在有效,因为它是相同的概念,除了现在从主题的 functions.php 文件中调用该函数。

这里是任何需要此代码的人的代码:

// Add item to cart
function add_id_to_cart( $product_id ) {
    $flag = true;
    //check if product already in cart
    foreach(WC()->cart->get_cart() as $key => $val ) {
        $_product = $val['data'];

        if($product_id == $_product->id ) {
            $flag = false;
        }
    }
    // if product not in cart, add it
    if ( $flag ) {
        WC()->cart->add_to_cart( $product_id );
    }
}

【讨论】:

    猜你喜欢
    • 2015-05-20
    • 1970-01-01
    • 2020-08-04
    • 2019-09-20
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多