{"id":2211,"date":"2024-07-04T19:35:52","date_gmt":"2024-07-04T11:35:52","guid":{"rendered":"https:\/\/qishiya.com\/?p=2211"},"modified":"2024-07-04T19:35:53","modified_gmt":"2024-07-04T11:35:53","slug":"woocommerce-%e5%af%bc%e5%85%a5%e5%95%86%e5%93%81%e6%97%b6%e5%9b%be%e7%89%87%e5%a4%84%e7%90%86%e9%80%bb%e8%be%91","status":"publish","type":"post","link":"https:\/\/qishiya.com\/?p=2211","title":{"rendered":"woocommerce \u5bfc\u5165\u5546\u54c1\u65f6\u56fe\u7247\u5904\u7406\u903b\u8f91"},"content":{"rendered":"\n<p>\u5728 woocommerce \u5bfc\u5165\u5546\u54c1\u65f6\uff0c\u5982\u679c\u4f60\u7684 csv \u4e2d\u7684\u5546\u54c1\u6709 images \u5b57\u6bb5\uff0c\u5982\u679c\u56fe\u7247\u662f\u5916\u90e8\u8fde\u63a5\uff0c\u4ed6\u4f1a\u4e00\u4e2a\u4e00\u4e2a\u4e0b\u8f7d\u5230\u672c\u5730\uff0c\u7136\u540e\u518d\u91cd\u65b0\u4e0a\u4f20\uff0c\u56e0\u4e3a\u6211\u5bfc\u5165\u7684\u5546\u54c1\u4e00\u6b21\u4f1a\u5bfc\u5165\u51e0\u767e\u4e2a\uff0c\u9020\u6210 cdn \u8ba4\u4e3a\u6d41\u91cf\u5f02\u5e38\uff0c\u76f4\u63a5\u7ed9\u62d2\u7edd\u4e86\uff0c\u7279\u610f\u770b\u4e86 woocommerce \u5904\u7406\u56fe\u7247\u7684\u903b\u8f91\uff0c\u603b\u7ed3\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u53ea\u8981\u662f images \u5b57\u6bb5\u91cc\u9762\u7684\u56fe\u7247\uff0c\u5982\u679c\u662f\u5916\u90e8\u56fe\u7247\uff08\u5728 media libaray \u4e2d\u4e0d\u5b58\u5728\uff09\uff0c\u5c31\u4f1a\u5148\u901a\u8fc7 curl \u4e0b\u8f7d\u5230\u672c\u5730\uff0c\u7136\u540e\u518d\u4e0a\u4f20\uff0c\u6700\u540e\u5728 gallery \u4e2d\u5c55\u793a\u7528\u7684\u662f\u8fd9\u4e2a media id.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code> \/**\n         * Get attachment ID.\n         *\n         * @param  string $url        Attachment URL.\n         * @param  int    $product_id Product ID.\n         * @return int\n         * @throws Exception If attachment cannot be loaded.\n         *\/\n        public function get_attachment_id_from_url( $url, $product_id ) {\n                if ( empty( $url ) ) {\n                        return 0;\n                }\n\n                $id         = 0;\n                $upload_dir = wp_upload_dir( null, false );\n                $base_url   = $upload_dir&#91;'baseurl'] . '\/';\n\n                \/\/ Check first if attachment is inside the WordPress uploads directory, or we're given a filename only.\n                if ( false !== strpos( $url, $base_url ) || false === strpos( $url, ':\/\/' ) ) {\n                        \/\/ Search for yyyy\/mm\/slug.extension or slug.extension - remove the base URL.\n                        $file = str_replace( $base_url, '', $url );\n                        $args = array(\n                                'post_type'   => 'attachment',\n                                'post_status' => 'any',\n                                'fields'      => 'ids',\n                                'meta_query'  => array( \/\/ @codingStandardsIgnoreLine.\n                                        'relation' => 'OR',\n                                        array(\n                                                'key'     => '_wp_attached_file',\n                                                'value'   => '^' . $file,\n                                                'compare' => 'REGEXP',\n                                        ),\n                                        array(\n                                                'key'     => '_wp_attached_file',\n                                                'value'   => '\/' . $file,\n                                                'compare' => 'LIKE',\n                                        ),\n                                        array(\n                                                'key'     => '_wc_attachment_source',\n                                                'value'   => '\/' . $file,\n                                                'compare' => 'LIKE',\n                                        ),\n                                ),\n                        );\n                } else {\n                        \/\/ This is an external URL, so compare to source.\n                        $args = array(\n                                'post_type'   => 'attachment',\n                                'post_status' => 'any',\n                                'fields'      => 'ids',\n                                'meta_query'  => array( \/\/ @codingStandardsIgnoreLine.\n                                        array(\n                                                'value' => $url,\n                                                'key'   => '_wc_attachment_source',\n                                        ),\n                                ),\n                        );\n                }\n\n                $ids = get_posts( $args ); \/\/ @codingStandardsIgnoreLine.\n\n                if ( $ids ) {\n                        $id = current( $ids );\n                }\n\n                \/\/ Upload if attachment does not exists.\n                if ( ! $id &amp;&amp; stristr( $url, ':\/\/' ) ) {\n                        $upload = wc_rest_upload_image_from_url( $url );\n\n                        if ( is_wp_error( $upload ) ) {\n                                throw new Exception( $upload->get_error_message(), 400 );\n                        }\n\n                        $id = wc_rest_set_uploaded_image_as_attachment( $upload, $product_id );\n\n                        if ( ! wp_attachment_is_image( $id ) ) {\n                          \/* translators: %s: image URL *\/\n                                throw new Exception( sprintf( __( 'Not able to attach \"%s\".', 'woocommerce' ), $url ), 400 );\n                        }\n\n                        \/\/ Save attachment source for future reference.\n                        update_post_meta( $id, '_wc_attachment_source', $url );\n                }\n\n                if ( ! $id ) {\n                        \/* translators: %s: image URL *\/\n                        throw new Exception( sprintf( __( 'Unable to use image \"%s\".', 'woocommerce' ), $url ), 400 );\n                }\n\n                return $id;\n        }<\/code><\/pre>\n\n\n\n<p>\u4ee3\u7801\u7684\u5b9e\u73b0\u5728 wp-content\/plugins\/woocommerce\/includes\/import\/abstract-wc-product-importer.php \u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 woocommerce \u5bfc\u5165\u5546\u54c1\u65f6\uff0c\u5982\u679c\u4f60\u7684 csv \u4e2d\u7684\u5546\u54c1\u6709 images \u5b57\u6bb5\uff0c\u5982\u679c\u56fe\u7247\u662f\u5916\u90e8\u8fde\u63a5\uff0c\u4ed6\u4f1a\u4e00\u4e2a\u4e00\u4e2a\u4e0b\u8f7d\u5230\u672c\u5730\uff0c\u7136\u540e\u518d\u91cd\u65b0\u4e0a\u4f20\uff0c\u56e0\u4e3a\u6211\u5bfc\u5165\u7684\u5546\u54c1\u4e00\u6b21\u4f1a\u5bfc\u5165\u51e0\u767e\u4e2a\uff0c\u9020\u6210 cdn \u8ba4\u4e3a\u6d41\u91cf\u5f02\u5e38\uff0c\u76f4\u63a5\u7ed9\u62d2\u7edd\u4e86\uff0c\u7279\u610f\u770b\u4e86 wo &#8230; <a title=\"woocommerce \u5bfc\u5165\u5546\u54c1\u65f6\u56fe\u7247\u5904\u7406\u903b\u8f91\" class=\"read-more\" href=\"https:\/\/qishiya.com\/?p=2211\" aria-label=\"\u66f4\u591a woocommerce \u5bfc\u5165\u5546\u54c1\u65f6\u56fe\u7247\u5904\u7406\u903b\u8f91\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[639,597],"tags":[],"class_list":["post-2211","post","type-post","status-publish","format-standard","hentry","category-woocommerece","category-wordpress"],"_links":{"self":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/2211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2211"}],"version-history":[{"count":1,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/2211\/revisions"}],"predecessor-version":[{"id":2212,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/2211\/revisions\/2212"}],"wp:attachment":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}