{"id":2420,"date":"2025-06-11T02:08:19","date_gmt":"2025-06-10T18:08:19","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/wpcms\/wpdt\/2420.html"},"modified":"2025-06-11T02:08:19","modified_gmt":"2025-06-10T18:08:19","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8woocommerce%e4%b8%ad%e6%b7%bb%e5%8a%a0%e4%ba%a7%e5%93%81%e6%a0%87%e7%ad%be%e3%80%81%e5%b1%9e%e6%80%a7%e5%92%8c%e5%88%86%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.zhaozhao123.cn\/wpcms\/post\/2420.html","title":{"rendered":"\u5982\u4f55\u5728WooCommerce\u4e2d\u6dfb\u52a0\u4ea7\u54c1\u6807\u7b7e\u3001\u5c5e\u6027\u548c\u5206\u7c7b"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"Bug&#32534;&#35793;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<p class=\"my-0\">Bug&#32534;&#35793;&#29422;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312;WooCommerce&#20013;&#31649;&#29702;&#20135;&#21697;&#26631;&#31614;&#12289;&#23646;&#24615;&#21644;&#20998;&#31867;&#26159;&#38750;&#24120;&#37325;&#35201;&#30340;&#65292;&#36825;&#19981;&#20165;&#26377;&#21161;&#20110;&#23458;&#25143;&#26356;&#22909;&#22320;&#20102;&#35299;&#21644;&#36873;&#25321;&#20135;&#21697;&#65292;&#36824;&#21487;&#20197;&#36890;&#36807;SEO&#20248;&#21270;&#25552;&#39640;&#32593;&#31449;&#30340;&#25628;&#32034;&#24341;&#25806;&#25490;&#21517;&#12290;&#19979;&#38754;&#25105;&#23558;&#35814;&#32454;&#20171;&#32461;&#22914;&#20309;&#22312;WooCommerce&#20013;&#28155;&#21152;&#36825;&#20123;&#21151;&#33021;&#12290;<\/p>\n<h3>1. &#28155;&#21152;&#20135;&#21697;&#26631;&#31614;<\/h3>\n<p><strong>&#27493;&#39588;&#65306;<\/strong><\/p>\n<ol>\n<li>&#30331;&#24405;&#21040;&#24744;&#30340;WooCommerce&#21518;&#21488;&#12290;<\/li>\n<li>&#36716;&#21040;&ldquo;&#35774;&#32622;&rdquo; -&gt; &ldquo;&#20135;&#21697;&#26631;&#31614;&rdquo;&#12290;<\/li>\n<li>&#22312;&#26631;&#31614;&#21015;&#34920;&#20013;&#28857;&#20987;&ldquo;+&rdquo;&#21495;&#26469;&#21019;&#24314;&#26032;&#30340;&#26631;&#31614;&#12290;<\/li>\n<li>&#36755;&#20837;&#26631;&#31614;&#21517;&#31216;&#24182;&#20445;&#23384;&#12290;<\/li>\n<\/ol>\n<p><strong>&#31034;&#20363;&#20195;&#30721;&#65306;<\/strong><\/p>\n<pre><code class=\"language-php\">\/\/ &#22686;&#21152;&#26032;&#26631;&#31614;\nadd_action('init', 'my_add_custom_product_tag');\nfunction my_add_custom_product_tag() {\n    register_taxonomy_for_object_type('product_tag', 'product'); \/\/ 'product' &#26159;&#20135;&#21697;&#31867;&#22411;\n}<\/code><\/pre>\n<p><strong>&#25928;&#26524;&#65306;<\/strong>\n&#24403;&#20320;&#19978;&#20256;&#24102;&#26377;&#26032;&#26631;&#31614;&#30340;&#20135;&#21697;&#26102;&#65292;&#31995;&#32479;&#20250;&#33258;&#21160;&#26174;&#31034;&#36825;&#20010;&#26032;&#26631;&#31614;&#12290;<\/p>\n<h3>2. &#28155;&#21152;&#20135;&#21697;&#23646;&#24615;<\/h3>\n<p><strong>&#27493;&#39588;&#65306;<\/strong><\/p>\n<ol>\n<li>&#30331;&#24405;&#21040;&#24744;&#30340;WooCommerce&#21518;&#21488;&#12290;<\/li>\n<li>&#36716;&#21040;&ldquo;&#35774;&#32622;&rdquo; -&gt; &ldquo;&#20135;&#21697;&#23646;&#24615;&rdquo;&#12290;<\/li>\n<li>&#28857;&#20987;&ldquo;+&rdquo;&#21495;&#26469;&#28155;&#21152;&#19968;&#20010;&#26032;&#30340;&#23646;&#24615;&#12290;<\/li>\n<li>&#35774;&#32622;&#23646;&#24615;&#21517;&#31216;&#12289;&#36873;&#39033;&#65288;&#22914;&#39068;&#33394;&#12289;&#22823;&#23567;&#31561;&#65289;&#24182;&#20445;&#23384;&#12290;<\/li>\n<\/ol>\n<p><strong>&#31034;&#20363;&#20195;&#30721;&#65306;<\/strong><\/p>\n<pre><code class=\"language-php\">\/\/ &#22686;&#21152;&#26032;&#23646;&#24615;\nadd_action('init', 'my_add_custom_product_attribute');\nfunction my_add_custom_product_attribute() {\n    add_rewrite_tag('%color%', '([a-zA-Z0-9s]+)'); \/\/ &#28155;&#21152;&#39068;&#33394;&#23646;&#24615;\n    add_filter('woocommerce_shortcode_products_attributes', 'custom_woocommerce_shortcode_products_attributes', 10, 2);\n}\n\nfunction custom_woocommerce_shortcode_products_attributes($atts, $id) {\n    global $post;\n\n    if (isset($atts['color'])) {\n        $color = sanitize_text_field($atts['color']);\n        return '&lt;div class=\"color\"&gt;' . esc_html($color) . '&lt;\/div&gt;';\n    }\n    return '';\n}<\/code><\/pre>\n<p><strong>&#25928;&#26524;&#65306;<\/strong>\n&#24403;&#29992;&#25143;&#27983;&#35272;&#20135;&#21697;&#39029;&#38754;&#26102;&#65292;&#20182;&#20204;&#21487;&#20197;&#22312;&#25628;&#32034;&#26639;&#36755;&#20837;&#39068;&#33394;&#26469;&#25214;&#21040;&#30456;&#20851;&#20135;&#21697;&#12290;<\/p>\n<h3>3. &#28155;&#21152;&#20135;&#21697;&#20998;&#31867;<\/h3>\n<p><strong>&#27493;&#39588;&#65306;<\/strong><\/p>\n<ol>\n<li>&#30331;&#24405;&#21040;&#24744;&#30340;WooCommerce&#21518;&#21488;&#12290;<\/li>\n<li>&#36716;&#21040;&ldquo;&#35774;&#32622;&rdquo; -&gt; &ldquo;&#20135;&#21697;&#20998;&#31867;&rdquo;&#12290;<\/li>\n<li>&#28857;&#20987;&ldquo;+&rdquo;&#21495;&#26469;&#28155;&#21152;&#26032;&#30340;&#20998;&#31867;&#12290;<\/li>\n<li>&#35774;&#32622;&#20998;&#31867;&#21517;&#31216;&#24182;&#20445;&#23384;&#12290;<\/li>\n<\/ol>\n<p><strong>&#31034;&#20363;&#20195;&#30721;&#65306;<\/strong><\/p>\n<pre><code class=\"language-php\">\/\/ &#22686;&#21152;&#26032;&#20998;&#31867;\nadd_action('init', 'my_add_custom_product_category');\nfunction my_add_custom_product_category() {\n    register_taxonomy_for_object_type('product_cat', 'product'); \/\/ 'product' &#26159;&#20135;&#21697;&#31867;&#22411;\n}<\/code><\/pre>\n<p><strong>&#25928;&#26524;&#65306;<\/strong>\n&#24403;&#20320;&#19978;&#20256;&#24102;&#26377;&#26032;&#20998;&#31867;&#30340;&#20135;&#21697;&#26102;&#65292;&#31995;&#32479;&#20250;&#33258;&#21160;&#26174;&#31034;&#36825;&#20010;&#26032;&#20998;&#31867;&#12290;<\/p>\n<h3>&#24635;&#32467;<\/h3>\n<p>&#20197;&#19978;&#23601;&#26159;&#20851;&#20110;&#22312;WooCommerce&#20013;&#28155;&#21152;&#20135;&#21697;&#26631;&#31614;&#12289;&#23646;&#24615;&#21644;&#20998;&#31867;&#30340;&#22522;&#26412;&#26041;&#27861;&#12290;&#36890;&#36807;&#19978;&#36848;&#27493;&#39588;&#65292;&#20320;&#21487;&#20197;&#36731;&#26494;&#22320;&#31649;&#29702;&#21644;&#20248;&#21270;&#20135;&#21697;&#30340;&#23637;&#31034;&#21644;&#25628;&#32034;&#20307;&#39564;&#12290;&#24076;&#26395;&#23545;&#20320;&#26377;&#25152;&#24110;&#21161;&#65281;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#40657;&#26495;&#21069;&#30340;Bug&#35821;&#27861;&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<p class=\"my-0\">&#40657;&#26495;&#21069;&#30340;Bug&#35821;&#27861;&#35762;&#24072;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312;WordPress&#19978;&#20351;&#29992;WooCommerce&#21019;&#24314;&#20135;&#21697;&#26102;&#65292;&#21487;&#20197;&#20026;&#20135;&#21697;&#28155;&#21152;&#22810;&#31181;&#23646;&#24615;&#21644;&#26631;&#31614;&#26469;&#24110;&#21161;&#29992;&#25143;&#26356;&#22909;&#22320;&#25628;&#32034;&#21644;&#21457;&#29616;&#20135;&#21697;&#12290;&#20197;&#19979;&#26159;&#22914;&#20309;&#22312;WooCommerce&#20013;&#28155;&#21152;&#20135;&#21697;&#26631;&#31614;&#12289;&#23646;&#24615;&#21644;&#20998;&#31867;&#30340;&#27493;&#39588;&#65306;<\/p>\n<h3>&#27493;&#39588; 1: &#21019;&#24314;&#20135;&#21697;&#20998;&#31867;<\/h3>\n<p>&#39318;&#20808;&#65292;&#22312;<code>wp-admin<\/code>&#30446;&#24405;&#19979;&#21019;&#24314;&#19968;&#20010;&#21517;&#20026;<code>woocommerce-product-categories.php<\/code>&#30340;&#26032;&#25991;&#20214;&#65292;&#29992;&#20110;&#31649;&#29702;&#20135;&#21697;&#30340;&#20998;&#31867;&#12290;<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\/\/ Add product categories to the WooCommerce product list.\nadd_action( 'woocommerce_product_list_categories', 'my_custom_product_category' );\nfunction my_custom_product_category() {\n    global $product;\n    if ( ! is_admin() ) { \/\/ Check if this is not a admin area\n        if ( isset( $_GET['cat'] ) &amp;&amp; ! empty( $_GET['cat'] ) ) {\n            $category = wc_get_term_by_id( $_GET['cat'], 'product_cat' );\n            if ( $category ) {\n                $product-&gt;set_attribute( 'product_cat', $category-&gt;slug );\n            }\n        }\n    }\n}<\/code><\/pre>\n<h3>&#27493;&#39588; 2: &#28155;&#21152;&#20135;&#21697;&#26631;&#31614;<\/h3>\n<p>&#25509;&#19979;&#26469;&#65292;&#20320;&#38656;&#35201;&#20026;&#27599;&#20010;&#20135;&#21697;&#35774;&#32622;&#19968;&#20010;&#25110;&#22810;&#20010;&#26631;&#31614;&#12290;&#22312;<code>wp-content\/plugins\/woocommerce\/includes\/product.php<\/code>&#25991;&#20214;&#20013;&#25214;&#21040;<code>add_tag()<\/code>&#20989;&#25968;&#65292;&#35813;&#20989;&#25968;&#20801;&#35768;&#20320;&#22312;&#21333;&#20010;&#20135;&#21697;&#20013;&#28155;&#21152;&#22810;&#20010;&#26631;&#31614;&#12290;<\/p>\n<pre><code class=\"language-php\">\/\/ add tags to the product's attributes\nfunction woocommerce_product_add_tags_to_attributes($attributes) {\n    if (!isset($attributes['tags'])) {\n        return $attributes;\n    }\n\n    $tags = explode(',', $attributes['tags']);\n    foreach ($tags as $tag) {\n        $attribute = array(\n            'name' =&gt; 'tags',\n            'value' =&gt; sanitize_text_field($tag),\n        );\n\n        $attributes['tags'][] = $attribute;\n\n        \/\/ Add tag attribute to the product\n        $product = wc_get_product(get_the_ID());\n        $product-&gt;add_attribute('tags', $attribute);\n    }\n\n    return $attributes;\n}\n\nadd_filter('woocommerce_product_attributes', 'woocommerce_product_add_tags_to_attributes');<\/code><\/pre>\n<h3>&#27493;&#39588; 3: &#35774;&#32622;&#20135;&#21697;&#23646;&#24615;<\/h3>\n<p>&#26368;&#21518;&#19968;&#27493;&#26159;&#20026;&#20135;&#21697;&#35774;&#32622;&#23646;&#24615;&#12290;&#36825;&#21487;&#20197;&#36890;&#36807;&#23558;&#23646;&#24615;&#20540;&#38468;&#21152;&#21040;&#20135;&#21697;&#23545;&#35937;&#19978;&#23454;&#29616;&#12290;&#20363;&#22914;&#65292;&#20320;&#21487;&#20197;&#20026;&#19968;&#20010;&#29305;&#23450;&#30340;&#20135;&#21697;&#35774;&#32622;&ldquo;&#39068;&#33394;&rdquo;&#23646;&#24615;&#12290;<\/p>\n<pre><code class=\"language-php\">\/\/ Set product color attribute\n$product = wc_get_product(1);\n$product-&gt;set_attribute('color', 'blue');\n\n\/\/ Get the current product and set its color attribute\n$products = wc_get_products(['status' =&gt; 'publish']);\nforeach ($products as $product) {\n    $product-&gt;set_attribute('color', 'blue'); \/\/ Set the color attribute for all products with the same ID\n}<\/code><\/pre>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<h4>&#31034;&#20363; 1: &#28155;&#21152;&#20135;&#21697;&#20998;&#31867;<\/h4>\n<pre><code class=\"language-php\">add_action( 'woocommerce_product_list_categories', 'my_custom_product_category' );\nfunction my_custom_product_category() {\n    global $product;\n    if ( ! is_admin() ) { \/\/ Check if this is not an admin area\n        if ( isset( $_GET['cat'] ) &amp;&amp; ! empty( $_GET['cat'] ) ) {\n            $category = wc_get_term_by_id( $_GET['cat'], 'product_cat' );\n            if ( $category ) {\n                $product-&gt;set_attribute( 'product_cat', $category-&gt;slug );\n            }\n        }\n    }\n}<\/code><\/pre>\n<h4>&#31034;&#20363; 2: &#28155;&#21152;&#20135;&#21697;&#26631;&#31614;<\/h4>\n<pre><code class=\"language-php\">add_filter('woocommerce_product_attributes', 'woocommerce_product_add_tags_to_attributes');\nfunction woocommerce_product_add_tags_to_attributes($attributes) {\n    if (!isset($attributes['tags'])) {\n        return $attributes;\n    }\n\n    $tags = explode(',', $attributes['tags']);\n    foreach ($tags as $tag) {\n        $attribute = array(\n            'name' =&gt; 'tags',\n            'value' =&gt; sanitize_text_field($tag),\n        );\n\n        $attributes['tags'][] = $attribute;\n\n        \/\/ Add tag attribute to the product\n        $product = wc_get_product(get_the_ID());\n        $product-&gt;add_attribute('tags', $attribute);\n    }\n\n    return $attributes;\n}<\/code><\/pre>\n<h4>&#31034;&#20363; 3: &#35774;&#32622;&#20135;&#21697;&#23646;&#24615;<\/h4>\n<pre><code class=\"language-php\">$product = wc_get_product(1);\n$product-&gt;set_attribute('color', 'blue');<\/code><\/pre>\n<p>&#36825;&#20123;&#20195;&#30721;&#23637;&#31034;&#20102;&#22914;&#20309;&#22312;WooCommerce&#20013;&#28155;&#21152;&#20135;&#21697;&#26631;&#31614;&#12289;&#23646;&#24615;&#21644;&#20998;&#31867;&#30340;&#22522;&#26412;&#25805;&#20316;&#12290;&#26681;&#25454;&#20320;&#30340;&#38656;&#27714;&#65292;&#20320;&#21487;&#20197;&#36827;&#19968;&#27493;&#25193;&#23637;&#21644;&#23450;&#21046;&#36825;&#20123;&#21151;&#33021;&#20197;&#28385;&#36275;&#19981;&#21516;&#30340;&#24212;&#29992;&#22330;&#26223;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#22312;WooCommerce&#20013;&#31649;&#29702;&#20135;&#21697;&#26631;&#31614;&#12289;&#23646;&#24615;&#21644;&#20998;&#31867;&#26159;&#038;..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[59],"tuisongtax":[],"class_list":["post-2420","post","type-post","status-publish","format-standard","hentry","category-wpdt","tag-pzjc"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/posts\/2420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/comments?post=2420"}],"version-history":[{"count":0,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/posts\/2420\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/media?parent=2420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/categories?post=2420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tags?post=2420"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tuisongtax?post=2420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}