{"id":49222,"date":"2024-12-02T09:14:12","date_gmt":"2024-12-02T01:14:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49222\/"},"modified":"2024-12-02T09:14:12","modified_gmt":"2024-12-02T01:14:12","slug":"%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0-woocommerce-ajax-%e7%9b%b4%e6%8e%a5%e7%ab%8b%e5%8d%b3%e8%b4%ad%e4%b9%b0%e6%8c%89%e9%92%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49222\/","title":{"rendered":"\u5982\u4f55\u5b9e\u73b0 WooCommerce AJAX \u76f4\u63a5\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5b9e\u73b0 WooCommerce AJAX \u76f4\u63a5\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5b9e\u73b0 WooCommerce AJAX \u76f4\u63a5\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241118\/1731906033673ac9f150495.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5b9e\u73b0 WooCommerce AJAX \u76f4\u63a5\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u63d2\u56fe\" alt=\"\u5982\u4f55\u5b9e\u73b0 WooCommerce AJAX \u76f4\u63a5\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u63d2\u56fe\" \/><\/p>\n<p>\u5f53\u60a8\u4f7f\u7528 woocommerce \u8fd0\u8425\u5728\u7ebf\u5546\u5e97\u65f6\uff0c\u4f7f\u8d2d\u4e70\u6d41\u7a0b\u5c3d\u53ef\u80fd\u65e0\u7f1d\u81f3\u5173\u91cd\u8981\u3002\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u662f\u6dfb\u52a0\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\uff0c\u4f7f\u5ba2\u6237\u65e0\u9700\u6d4f\u89c8\u591a\u4e2a\u9875\u9762\u5373\u53ef\u76f4\u63a5\u8d2d\u4e70\u4ea7\u54c1\u3002\u672c\u535a\u5ba2\u5c06\u5f15\u5bfc\u60a8\u4f7f\u7528\u63d0\u4f9b\u7684\u4ee3\u7801\u7247\u6bb5\u521b\u5efa woocommerce ajax\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u3002<\/p>\n<h2> \u7b2c 1 \u6b65\uff1a\u6dfb\u52a0\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae <\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728 woocommerce \u4ea7\u54c1\u9875\u9762\u4e0a\u6dfb\u52a0\u81ea\u5b9a\u4e49\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u6302\u94a9 woocommerce_after_add_to_cart_button \u64cd\u4f5c\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\uff0c\u8be5\u64cd\u4f5c\u5c06\u6211\u4eec\u7684\u6309\u94ae\u653e\u5728\u6807\u51c6\u201c\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u201d\u6309\u94ae\u4e4b\u540e\u3002<\/p>\n<p>\u8fd9\u662f php \u4ee3\u7801\u7247\u6bb5\uff1a<\/p>\n<pre>add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );\nfunction add_content_after_addtocart() {\n    $current_product_id = get_the_id();\n    $product = wc_get_product( $current_product_id );\n\n    if( $product-&gt;is_type( 'simple' ) ){\n        echo '&lt;button data-id=\"'.$current_product_id.'\" class=\"buy-now button\"&gt;&lt;i class=\"matico-icon-toys\"&gt;&lt;\/i&gt;'.__('buy now', 'woocommerce').'&lt;\/button&gt;';\n    }\n}\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u6211\u4eec\u4f7f\u7528 woocommerce_after_add_to_cart_button \u6302\u94a9\u5728\u201c\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u201d\u6309\u94ae\u540e\u9762\u63d2\u5165\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u3002<\/li>\n<li>get_the_id() \u51fd\u6570\u68c0\u7d22\u5f53\u524d\u4ea7\u54c1 id\uff0cwc_get_product() \u51fd\u6570\u83b7\u53d6\u4ea7\u54c1\u8be6\u7ec6\u4fe1\u606f\u3002<\/li>\n<li>\u6211\u4eec\u68c0\u67e5\u4ea7\u54c1\u662f\u5426\u4e3a\u7b80\u5355\u7c7b\u578b\uff0c\u7136\u540e\u4f7f\u7528\u9002\u5f53\u7684 data-id \u5c5e\u6027\u548c\u81ea\u5b9a\u4e49\u56fe\u6807\u6e32\u67d3\u6309\u94ae\u3002<\/li>\n<\/ul>\n<h2> \u7b2c 3 \u6b65\uff1a\u5c06\u811a\u672c\u6392\u5165\u961f\u5217 <\/h2>\n<p>\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u5c06\u811a\u672c\u6392\u5165\u4e3b\u9898\u4e2d\uff0c\u4ee5\u786e\u4fdd\u5176\u6b63\u786e\u52a0\u8f7d\u5230\u60a8\u7684 woocommerce \u9875\u9762\u4e0a\u3002\u64cd\u4f5c\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>wp_enqueue_script('matico-child-script', get_stylesheet_directory_uri() . '\/assets\/js\/script.js', array( 'jquery', 'scrollfix-script' ),  $matico_version, true);\n\nwp_localize_script( 'matico-child-script', 'matico_child_script_obj',\n    array( \n        'checkout_page_url' =&gt; wc_get_checkout_url(),\n    )\n);\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li> wp_enqueue_script() \u7528\u4e8e\u52a0\u8f7d\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u811a\u672c\u6587\u4ef6\uff08script.js\uff09\uff0c\u5176\u4e2d\u5305\u542b jquery \u4ee3\u7801\u3002<\/li>\n<li> wp_localize_script() \u5c06 php \u6570\u636e\u4f20\u9012\u7ed9\u811a\u672c\uff0c\u4f8b\u5982\u7ed3\u5e10\u9875\u9762 url\uff0c\u5141\u8bb8\u6211\u4eec\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u5b83\u3002<\/li>\n<\/ul>\n<h2> \u7b2c 2 \u6b65\uff1a\u5904\u7406 ajax \u8bf7\u6c42 <\/h2>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 jquery \u5904\u7406\u6309\u94ae\u5355\u51fb\u4e8b\u4ef6\u3002 jquery \u811a\u672c\u5411 woocommerce \u53d1\u9001 ajax \u8bf7\u6c42\uff0c\u540e\u8005\u5c06\u4ea7\u54c1\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\uff0c\u7136\u540e\u5c06\u7528\u6237\u76f4\u63a5\u91cd\u5b9a\u5411\u5230\u7ed3\u5e10\u9875\u9762\u3002<\/p>\n<p>\u8fd9\u662f jquery \u4ee3\u7801\u7247\u6bb5\uff1a<\/p>\n<pre>(function ($) {\n    var MaticoChildThemeConfig = {\n        init: function () {\n            this.bindEvents();\n        },\n        bindEvents: function () {\n            $(document).on('click', '.buy-now', this.handleBuyNowClick);\n        },\n        handleBuyNowClick: function (event) {\n            event.preventDefault();\n\n            var $button = $(this),\n                quantity = parseFloat($button.closest('.quantity').find('.qty').val()) || 1,\n                productID = $button.data('id');\n\n            var data = {\n                product_id: productID,\n                quantity: quantity,\n            };\n\n            $.ajax({\n                type: 'POST',\n                url: wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'add_to_cart'),\n                data: data,\n                dataType: 'json',\n                beforeSend: function () {\n                    $button.addClass('loading');\n                },\n                success: function (res) {\n                    if (res.error &amp;&amp; res.product_url) {\n                        window.location.href = res.product_url;\n                    } else {\n                        window.location.href = matico_child_script_obj.checkout_page_url;\n                    }\n                }\n            });\n        }\n    };\n\n    MaticoChildThemeConfig.init();\n})(jQuery);\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5355\u51fb\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u65f6\uff0c\u6211\u4eec\u4f1a\u963b\u6b62\u9ed8\u8ba4\u64cd\u4f5c\u4ee5\u907f\u514d\u9875\u9762\u91cd\u65b0\u52a0\u8f7d\u3002<\/li>\n<li>\u6211\u4eec\u4ece\u5f53\u524d\u4ea7\u54c1\u9875\u9762\u6536\u96c6\u4ea7\u54c1 id \u548c\u6570\u91cf\u3002<\/li>\n<li>ajax \u8bf7\u6c42\u53d1\u9001\u5230 woocommerce \u7684 add_to_cart \u7aef\u70b9\uff0c\u8be5\u7aef\u70b9\u5c06\u4ea7\u54c1\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u3002<\/li>\n<li>\u5982\u679c\u4ea7\u54c1\u6dfb\u52a0\u6210\u529f\uff0c\u6211\u4eec\u4f1a\u5c06\u7528\u6237\u91cd\u5b9a\u5411\u5230\u7ed3\u5e10\u9875\u9762\u3002\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff08\u4f8b\u5982\uff0c\u4ea7\u54c1\u4e0d\u518d\u53ef\u7528\uff09\uff0c\u7528\u6237\u5c06\u88ab\u91cd\u5b9a\u5411\u5230\u4ea7\u54c1\u9875\u9762\u3002<\/li>\n<\/ul>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u901a\u8fc7\u5b9e\u65bd\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\uff0c\u4ee5\u7b80\u5316\u5ba2\u6237\u7684\u8d2d\u4e70\u6d41\u7a0b\u3002\u6b64\u529f\u80fd\u5728\u901a\u8fc7\u51cf\u5c11\u5ba2\u6237\u5728\u5b8c\u6210\u8d2d\u4e70\u4e4b\u524d\u9700\u8981\u5bfc\u822a\u7684\u70b9\u51fb\u6b21\u6570\u548c\u9875\u9762\u6570\u91cf\u6765\u63d0\u9ad8\u8f6c\u5316\u7387\u65b9\u9762\u7279\u522b\u6709\u7528\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5b9e\u73b0 WooCommerce AJAX \u76f4\u63a5\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5b9e\u73b0 WooCommerce AJAX \u76f4\u63a5\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae \u6536\u85cf \u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5b9e\u73b0 WooCommerce AJAX \u76f4\u63a5\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u300b\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01 \u5f53\u60a8\u4f7f\u7528 woocommerce \u8fd0\u8425\u5728\u7ebf\u5546\u5e97\u65f6\uff0c\u4f7f\u8d2d\u4e70\u6d41\u7a0b\u5c3d\u53ef\u80fd\u65e0\u7f1d\u81f3\u5173\u91cd\u8981\u3002\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u662f\u6dfb\u52a0\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\uff0c\u4f7f\u5ba2\u6237\u65e0\u9700\u6d4f\u89c8\u591a\u4e2a\u9875\u9762\u5373\u53ef\u76f4\u63a5\u8d2d\u4e70\u4ea7\u54c1\u3002\u672c\u535a\u5ba2\u5c06\u5f15\u5bfc\u60a8\u4f7f\u7528\u63d0\u4f9b\u7684\u4ee3\u7801\u7247\u6bb5\u521b\u5efa woocommerce ajax\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u3002 \u7b2c 1 \u6b65\uff1a\u6dfb\u52a0\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae \u9996\u5148\uff0c\u60a8\u9700\u8981\u5728 woocommerce \u4ea7\u54c1\u9875\u9762\u4e0a\u6dfb\u52a0\u81ea\u5b9a\u4e49\u201c\u7acb\u5373\u8d2d\u4e70\u201d\u6309\u94ae\u3002\u6211\u4eec\u5c06\u901a\u8fc7\u6302\u94a9 woocommerce_after_add_to_cart_button \u64cd\u4f5c\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\uff0c\u8be5\u64cd\u4f5c\u5c06\u6211\u4eec\u7684\u6309\u94ae\u653e\u5728\u6807\u51c6\u201c\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\u201d\u6309\u94ae\u4e4b\u540e\u3002 \u8fd9\u662f php \u4ee3\u7801\u7247\u6bb5\uff1a add_action( &#8216;woocommerce_after_add_to_cart_button&#8217;, &#8216;add_content_after_addtocart&#8217; ); function add_content_after_addtocart() { $current_product_id = get_the_id(); $product = wc_get_product( $current_product_id ); if( $product-&gt;is_type( &#8216;simple&#8217; ) ){ echo &#8216;&lt;button data-id=&#8221;&#8216;.$current_product_id.'&#8221; class=&#8221;buy-now button&#8221;&gt;&lt;i class=&#8221;matico-icon-toys&#8221;&gt;&lt;\/i&gt;&#8217;.__(&#8216;buy now&#8217;, &#8216;woocommerce&#8217;).'&lt;\/button&gt;&#8217;; } } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-49222","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=49222"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49222\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}