添加动作woocommerce_add_to_cart打破网站原创添加到购物车功能 [英] add action woocommerce_add_to_cart breaks websites original add to cart functionality

查看:170
本文介绍了添加动作woocommerce_add_to_cart打破网站原创添加到购物车功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个node express API并让它监听来自woocommerce_add_to_cart webhook的帖子请求,但是从那里获得的有效载荷基本上没有价值。

I built a node express API and have it listening for post requests coming from the woocommerce_add_to_cart webhook, but the payload from that is essentially worthless.

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

有没有办法将产品数据添加到这些webhooks?如果不想从functions.php函数发送php curl请求,那么

Is there a way to add product data to these webhooks? If not looking to send a php curl request from a functions.php function like so

我的functions.php函数:

My functions.php function:

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会导致错误。查看旧的段插件,他们也将此事件用于此目的,因此必须可以在多个位置使用此挂钩。

This is causing the websites add to cart functionality to break so my assumption is that the theme, Basel, uses this action hook and adding it again to functions.php causes an error. Looking at the old segment plugin, they too use this event for this purpose, so it must be possible to use this hook in multiple places.

错误的代码片段发生:

   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中删除我的功能时,产品会正确地添加到购物车中没有错误。

When I remove my function from functions.php the product gets added to the cart properly and there are no errors.

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

Do I need to add this function in a plug-in rather than in functions.php?

推荐答案

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

I think just just notices not there, so you need to guard it You can try that,

           //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]))
         }

这篇关于添加动作woocommerce_add_to_cart打破网站原创添加到购物车功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆