簡體   English   中英

添加動作woocommerce_add_to_cart打破網站原創添加到購物車功能

[英]add action woocommerce_add_to_cart breaks websites original add to cart functionality

我構建了一個node express API並讓它監聽來自woocommerce_add_to_cart webhook的帖子請求,但是從那里獲得的有效載荷基本上沒有價值。

body:{ action: 'woocommerce_add_to_cart', arg:'098uoijo098920sa489983jk' }

有沒有辦法將產品數據添加到這些webhooks? 如果不想從functions.php函數發送php curl請求,就像這樣

我的functions.php函數:

function custom_add_to_cart($cart_item_key, $product_id, $quantity, 
    $variation_id, $variation, $cart_item_data){
        //added this just to see if my event function even gets called, doesn't appear to 
        echo'<pre>';var_dump($product_id);echo'</pre>';die();
        $product_info = json_encode(array($product_id => wc_get_product($product_id)));
        $request_args = array(
           'httpversion' => '1.1',
           'headers' => 'Content-Type:application/json',
           'body' => $product_info,
    );
    $response = wp_safe_remote_post("myapp.com/endpoint", $request_args);
}

add_action('woocommerce_add_to_cart', 'custom_add_to_cart', 10, 6);

這導致網站添加到購物車功能中斷,所以我的假設是主題巴塞爾使用此操作掛鈎並再次將其添加到functions.php導致錯誤。 查看舊的段插件,他們也將此事件用於此目的,因此必須可以在多個位置使用此掛鈎。

發生錯誤的代碼段:

   a.ajax({
        url: basel_settings.ajaxurl,
        data: e,
        method: "POST",
        success: function(b) {
            if (b) {
                var c = window.location.toString();
                if (c = c.replace("add-to-cart", "added-to-cart"),
                b.error && b.product_url)
                    return void (window.location = b.product_url);
                if ("yes" === wc_add_to_cart_params.cart_redirect_after_add)
                    return void (window.location = wc_add_to_cart_params.cart_url);
                d.removeClass("loading");
                var e = b.fragments
                  , f = b.cart_hash;
                e && a.each(e, function(b) {
                    a(b).addClass("updating")
                }),
                e && a.each(e, function(b, c) {
                    a(b).replaceWith(c)
                }),
                //line where error gets reported
                b.notices.indexOf("error") > 0 ? (a(".woocommerce-error").length > 0 && a(".woocommerce-error").remove(),
                a(".single-product-content").prepend(b.notices),
                d.addClass("not-added")) : ("widget" == basel_settings.add_to_cart_action && a.magnificPopup.close(),
                d.addClass("added"),
                a(document.body).trigger("added_to_cart", [e, f, d]))
            }
        },
        error: function() {
            console.log("ajax adding to cart error")
        },
        complete: function() {}
    })

錯誤:

Uncaught TypeError: Cannot read property 'indexOf' of undefined
    at Object.success (theme.min.js?ver=4.0.0:formatted:17037)
    at i (jquery.js?ver=1.12.4:2)
    at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
    at y (jquery.js?ver=1.12.4:4)
    at XMLHttpRequest.c (jquery.js?ver=1.12.4:4)

當我從functions.php中刪除我的功能時,產品會正確地添加到購物車中並且沒有錯誤。

我是否需要在插件中而不是在functions.php中添加此功能?

我想只是注意不在那里,所以你需要保護它你可以試試,

           //line where error gets reported
         if(b.notices){
            b.notices.indexOf("error") > 0 ? (a(".woocommerce-error").length > 0 && a(".woocommerce-error").remove(),
            a(".single-product-content").prepend(b.notices),
            d.addClass("not-added")) : ("widget" == basel_settings.add_to_cart_action && a.magnificPopup.close(),
            d.addClass("added"),
            a(document.body).trigger("added_to_cart", [e, f, d]))
         }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM