{"id":2170,"date":"2024-03-25T00:11:00","date_gmt":"2024-03-24T16:11:00","guid":{"rendered":"https:\/\/qishiya.com\/?p=2170"},"modified":"2024-03-25T00:11:50","modified_gmt":"2024-03-24T16:11:50","slug":"woocommerce%e6%a0%b9%e6%8d%ae%e4%bd%93%e7%a7%af%e5%92%8c%e9%87%8d%e9%87%8f%e7%ae%97%e8%bf%90%e8%b4%b9","status":"publish","type":"post","link":"https:\/\/qishiya.com\/?p=2170","title":{"rendered":"Woocommerce\u6839\u636e\u4f53\u79ef\u548c\u91cd\u91cf\u7b97\u8fd0\u8d39"},"content":{"rendered":"\n<p>\u770b\u4e86\u5f88\u591a\u63d2\u4ef6\uff0c\u5305\u62ec\u6536\u8d39\u548c\u514d\u8d39\u7684\uff0c\u90fd\u5f88\u80fd\u6ee1\u8db3\u6211\u7684\u9700\u6c42\uff0c\u6211\u7684\u9700\u6c42\u662f\u91cd\u91cf\u548c\u6ce1\u91cd\u8c01\u6700\u5927\uff0c\u5c31\u7528\u8c01\u6765\u505a\u8fd0\u8d39\u8ba1\u7b97\uff0c\u5e76\u4e14\u8fd8\u8981\u652f\u6301\u9636\u68af\u8fd0\u8d39\uff0c\u6bd4\u5982 2 \u516c\u65a4\u4e4b\u5185\uff0c2 \u516c\u65a4\u4ee5\u4e0a\uff0c\u6bcf\u589e\u52a0\u4e00\u516c\u65a4\uff0c\u589e\u52a0\u591a\u5c11\u94b1\u4e4b\u7c7b\u7684\u8ba1\u7b97\u903b\u8f91\u3002<\/p>\n\n\n\n<p>\u6211\u81ea\u5df1\u4ece\u6765\u6ca1\u6709\u5b8c\u6574\u7684\u5199\u4e2a wordpress \u63d2\u4ef6\uff0c\u7136\u540e\u6211\u5c31\u6c42\u52a9 chatgpt\uff0c\u7ec8\u4e8e\u628a\u8fd9\u4e2a\u80fd\u529b\u7ed9\u5b9e\u73b0\u4e86\u4e00\u4e2a\u521d\u7248\u672c\uff0c\u8fd8\u4e0d\u80fd\u76f4\u63a5\u5728\u540e\u53f0\u914d\u7f6e\uff0c\u4f46\u662f\u901a\u8fc7\u4ee3\u7801\u5df2\u7ecf\u5b9e\u73b0\uff0c\u5148\u8ba9\u5ba2\u6237\u53ef\u4ee5\u6b63\u5e38\u8ba1\u7b97\u8fd0\u8d39\uff0c\u9897\u7c92\u5ea6\u4e0d\u662f\u7279\u522b\u7ec6\uff0c\u6709\u6bd4\u6ca1\u6709\u8981\u597d\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_61 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ff6200;color:#ff6200\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ff6200;color:#ff6200\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/qishiya.com\/?p=2170\/#%E6%80%8E%E4%B9%88%E6%A0%B7%E5%AE%9E%E7%8E%B0\" title=\"\u600e\u4e48\u6837\u5b9e\u73b0\">\u600e\u4e48\u6837\u5b9e\u73b0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/qishiya.com\/?p=2170\/#%E5%8F%82%E8%80%83\" title=\"\u53c2\u8003\">\u53c2\u8003<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%80%8E%E4%B9%88%E6%A0%B7%E5%AE%9E%E7%8E%B0\"><\/span>\u600e\u4e48\u6837\u5b9e\u73b0<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5b9e\u73b0\u4e00\u4e2a\u7ee7\u627f\u81ea WC_Shipping_Method \u7684\u7c7b\u5c31\u53ef\u4ee5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u7269\u6d41\u8ba1\u8d39\u65b9\u5f0f\uff0c\u4ee5\u4e0b\u4ee3\u7801\u6765\u81ea\u4e8e chatgpt.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### \u4f7f\u7528\u8bf4\u660e\n\n\u4f60\u53ef\u4ee5\u5c06\u4ee5\u4e0b\u4ee3\u7801\u590d\u5236\u5e76\u7c98\u8d34\u5230\u4f60\u7684 WooCommerce \u63d2\u4ef6\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63d2\u4ef6\u6587\u4ef6\u3002\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a `WC_Tesl_Shipping_Method` \u7684\u65b0\u7684\u7269\u6d41\u65b9\u5f0f\uff0c\u5e76\u4e14\u6309\u7167\u7ed9\u5b9a\u7684\u89c4\u5219\u8ba1\u7b97\u8fd0\u8d39\u3002\n\n\u786e\u4fdd\u5728\u6dfb\u52a0\u6b64\u4ee3\u7801\u4e4b\u524d\uff0c\u5728 WordPress \u7ad9\u70b9\u4e0a\u5b89\u88c5\u5e76\u6fc0\u6d3b\u4e86 WooCommerce \u63d2\u4ef6\u3002\n\n### \u4ee3\u7801\n\n```php\n&lt;?php\n\/**\n * Plugin Name: Tesl Shipping Method for WooCommerce\n * Description: Custom shipping method for WooCommerce.\n * Version: 1.0.0\n * Author: Your Name\n *\/\n\nif ( ! defined( 'WPINC' ) ) {\n    die;\n}\n\nfunction tesl_shipping_init() {\n    if ( ! class_exists( 'WC_Tesl_Shipping_Method' ) ) {\n        class WC_Tesl_Shipping_Method extends WC_Shipping_Method {\n            public function __construct() {\n                $this->id                 = 'tesl'; \n                $this->method_title       = __( 'Tesl Shipping', 'woocommerce' );\n                $this->method_description = __( 'Custom Shipping Method for Tesl', 'woocommerce' );\n\n                \/\/ Availability &amp; Countries\n                $this->availability = 'including';\n                $this->countries = array(\n                    'US',\n                    \/\/ Add other countries as needed\n                );\n\n                $this->init();\n\n                $this->enabled = isset( $this->settings&#91;'enabled'] ) ? $this->settings&#91;'enabled'] : 'yes';\n                $this->title   = isset( $this->settings&#91;'title'] ) ? $this->settings&#91;'title'] : __( 'Tesl Shipping', 'woocommerce' );\n            }\n\n            function init() {\n                \/\/ Load the settings API\n                $this->init_form_fields();\n                $this->init_settings();\n\n                \/\/ Save settings in admin if you have any defined\n                add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );\n            }\n\n            \/**\n             * Calculate shipping cost.\n             *\n             * @param array $package Package information.\n             *\/\n            public function calculate_shipping( $package = array() ) {\n                $weight = 0;\n                $length = 0;\n                $width  = 0;\n                $height = 0;\n                $cost   = 0;\n                $longest_side = 0;\n\n                foreach ( $package&#91;'contents'] as $item_id => $values ) {\n                    $_product = $values&#91;'data'];\n                    $weight += $_product->get_weight();\n                    $dimensions = $_product->get_dimensions(false);\n                    $length = max($length, $dimensions&#91;'length']);\n                    $width  = max($width, $dimensions&#91;'width']);\n                    $height = max($height, $dimensions&#91;'height']);\n                    $longest_side = max($dimensions&#91;'length'], $dimensions&#91;'width'], $dimensions&#91;'height']);\n                    $volumetric_weight = ($dimensions&#91;'length'] * $dimensions&#91;'width'] * $dimensions&#91;'height']) \/ 6000;\n                    \n                    if ($weight &lt; 1 &amp;&amp; $volumetric_weight &lt; 1 &amp;&amp; ($dimensions&#91;'length'] + $dimensions&#91;'width'] + $dimensions&#91;'height']) &lt; 90) {\n                        $cost = 10;\n                    } elseif ($weight >= 1 &amp;&amp; $weight &lt; 2 &amp;&amp; $volumetric_weight >= 1 &amp;&amp; $volumetric_weight &lt; 2 &amp;&amp; ($dimensions&#91;'length'] + $dimensions&#91;'width'] + $dimensions&#91;'height']) &lt; 90) {\n                        $cost = 20;\n                    } elseif ($weight > 2 || $volumetric_weight > 2) {\n                        $base_cost = 20;\n                        $additional_weight = max($weight, $volumetric_weight) - 2;\n                        $additional_cost = $additional_weight * 8;\n                        $cost = $base_cost + $additional_cost;\n\n                        if ($longest_side > 120) {\n                            $cost += 20; \/\/ Surcharge for long items\n                        }\n                    }\n                }\n\n                $rate = array(\n                    'id'    => $this->id,\n                    'label' => $this->title,\n                    'cost'  => $cost,\n                );\n\n                $this->add_rate( $rate );\n            }\n        }\n    }\n}\n\nadd_action( 'woocommerce_shipping_init', 'tesl_shipping_init' );\n\nfunction add_tesl_shipping_method( $methods ) {\n    $methods&#91;'tesl'] = 'WC_Tesl_Shipping_Method';\n    return $methods;\n}\n\n\n\nadd_filter( 'woocommerce_shipping_methods', 'add_tesl_shipping_method' );\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6700\u4e0d\u53cb\u597d\u7684\u5730\u65b9\u662f\u652f\u6301\u8fd0\u9001\u7684\u56fd\u5bb6\u5199\u6b7b\u5728\u4ee3\u7801\u91cc\u9762\uff0c\u6700\u597d\u662f\u53ef\u4ee5\u5728 zone \u533a\u57df\u8fdb\u884c\u914d\u7f6e\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u82b1\u70b9\u65f6\u95f4\u7406\u89e3\u600e\u4e48\u6837\u5728 zone \u91cc\u9762\u914d\u7f6e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%8F%82%E8%80%83\"><\/span>\u53c2\u8003<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.cloudways.com\/blog\/create-woocommerce-custom-shipping-method-plugin\/\">https:\/\/www.cloudways.com\/blog\/create-woocommerce-custom-shipping-method-plugin\/<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/code.tutsplus.com\/create-a-custom-shipping-method-for-woocommerce--cms-26098t\">https:\/\/code.tutsplus.com\/create-a-custom-shipping-method-for-woocommerce&#8211;cms-26098t<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/webkul.com\/blog\/creating-new-shipping-method-woocommerce\/\">How to create new shipping method in WooCommerce (webkul.com)<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u770b\u4e86\u5f88\u591a\u63d2\u4ef6\uff0c\u5305\u62ec\u6536\u8d39\u548c\u514d\u8d39\u7684\uff0c\u90fd\u5f88\u80fd\u6ee1\u8db3\u6211\u7684\u9700\u6c42\uff0c\u6211\u7684\u9700\u6c42\u662f\u91cd\u91cf\u548c\u6ce1\u91cd\u8c01\u6700\u5927\uff0c\u5c31\u7528\u8c01\u6765\u505a\u8fd0\u8d39\u8ba1\u7b97\uff0c\u5e76\u4e14\u8fd8\u8981\u652f\u6301\u9636\u68af\u8fd0\u8d39\uff0c\u6bd4\u5982 2 \u516c\u65a4\u4e4b\u5185\uff0c2 \u516c\u65a4\u4ee5\u4e0a\uff0c\u6bcf\u589e\u52a0\u4e00\u516c\u65a4\uff0c\u589e\u52a0\u591a\u5c11\u94b1\u4e4b\u7c7b\u7684\u8ba1\u7b97\u903b\u8f91\u3002 \u6211\u81ea\u5df1\u4ece\u6765\u6ca1\u6709\u5b8c\u6574\u7684\u5199\u4e2a wordpres &#8230; <a title=\"Woocommerce\u6839\u636e\u4f53\u79ef\u548c\u91cd\u91cf\u7b97\u8fd0\u8d39\" class=\"read-more\" href=\"https:\/\/qishiya.com\/?p=2170\" aria-label=\"\u66f4\u591a Woocommerce\u6839\u636e\u4f53\u79ef\u548c\u91cd\u91cf\u7b97\u8fd0\u8d39\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":2172,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[639,597],"tags":[678,679,680],"class_list":["post-2170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-woocommerece","category-wordpress","tag-woocommerce","tag-679","tag-680","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/2170","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=2170"}],"version-history":[{"count":1,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/2170\/revisions"}],"predecessor-version":[{"id":2171,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/posts\/2170\/revisions\/2171"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=\/wp\/v2\/media\/2172"}],"wp:attachment":[{"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qishiya.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}