自动设置特定的运费类别在Woocommerce中添加产品时

huangapple go评论90阅读模式
英文:

Auto set specific shipping class in Woocommerce when adding a product

问题

在Woocommerce中添加产品时,在“配送”选项卡下,有人知道如何或是否可以更改默认选择的运输类别吗?

目前,在添加新产品时,运输类别设置为“无运输类别”,但我希望默认设置为“皇家邮政”,这是我设置的自定义运输类别。

自动设置特定的运费类别在Woocommerce中添加产品时

我已经查看了所有选项,但没有找到可以更改选择的内容。

英文:

When adding a product in Woocommerce, under the Shipping Tab, does anyone know how or if the dfefault selected shipping class choice can be changed.

Currently, when adding a new product, the shipping class is set to No Shipping Class but I would prefer for the default to be be set to Royal Mail which is a custom shipping class I have setup.

自动设置特定的运费类别在Woocommerce中添加产品时

I have loked under all the options but nothing is there to move the choices.

答案1

得分: 2

你可以通过在仅针对“新”的产品页面添加一些JavaScript代码来使用一个小技巧。

首先,你需要找到你的“Royal”运输类别的数字术语ID。为此,请检查运输类别下拉菜单以获取相关的选项值:

现在,你可以在以下代码中设置你的运输类别ID:

add_action('admin_footer', 'custom_admin_product_js');
function custom_admin_product_js() {
    global $pagenow, $post_type;

    if ($pagenow === 'post-new.php' && $post_type === 'product'):
    ?><script type='text/javascript'>
    jQuery(function($) {
        var shippingClassID = '33'; // <= 在这里设置你的运输类别ID
        $('select#product_shipping_class').val(shippingClassID).change();
    });
    </script><?php
    endif;
}

将代码放入你的活动子主题(或活动主题)的functions.php文件中。已经测试并且有效。

英文:

You can use a trick by adding some JavaScript code in the Admin targeting "new" product pages only.

First you need to find out the numerical term Id for your "Royal" shipping class. For that, inspect the shipping class dropdown to get the related option value:

自动设置特定的运费类别在Woocommerce中添加产品时

Now you can set your shipping class ID in the code below:

add_action( &#39;admin_footer&#39;, &#39;custom_admin_product_js&#39; );
function custom_admin_product_js() {
    global $pagenow, $post_type;

    if( $pagenow === &#39;post-new.php&#39; &amp;&amp; $post_type === &#39;product&#39; ):
    ?&gt;&lt;script type=&#39;text/javascript&#39;&gt;
    jQuery( function($) {
        var shippingClassID = &#39;33&#39;; // &lt;= Here set you shipping class ID
        $(&#39;select#product_shipping_class&#39;).val(shippingClassID).change();
    });
    &lt;/script&gt;&lt;?php
    endif;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

huangapple
  • 本文由 发表于 2023年7月3日 04:39:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定