jjzjj

javascript - 添加操作 woocommerce_add_to_cart 破坏网站原始添加到购物车功能

coder 2024-04-08 原文

我构建了一个 node express API 并让它监听来自 woocommerce_add_to_cart webhook 的发布请求,但来自它的有效负载基本上毫无值(value)。

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

有没有办法将产品数据添加到这些 webhook 中?如果不想像这样从 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);

这导致网站添加到购物车功能中断,所以我假设主题 Basel 使用此操作 Hook 并将其再次添加到 functions.php 会导致错误。查看旧的段插件,他们也为此目的使用此事件,因此必须可以在多个地方使用此 Hook 。

发生错误的代码片段:

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

关于javascript - 添加操作 woocommerce_add_to_cart 破坏网站原始添加到购物车功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49581802/

有关javascript - 添加操作 woocommerce_add_to_cart 破坏网站原始添加到购物车功能的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby-on-rails - rails : save file from URL and save it to Amazon S3 - 2

    从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex

  4. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  5. ruby-on-rails - rails : How to make a form post to another controller action - 2

    我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak

  6. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  7. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  8. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  9. ruby-on-rails - Prawn PDF : I need to generate nested tables - 2

    我需要一个表,其中行实际上是2行表,一个嵌套表是..我怎样才能在Prawn中做到这一点?也许我需要延期..但哪一个? 最佳答案 现在支持子表:Prawn::Document.generate("subtable.pdf")do|pdf|subtable=pdf.make_table([["sub"],["table"]])pdf.table([[subtable,"original"]])end 关于ruby-on-rails-PrawnPDF:Ineedtogeneratenested

  10. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

随机推荐