英文:
Auto set specific shipping class in Woocommerce when adding a product
问题
在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.
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:
Now you can set your shipping class ID in the code below:
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'; // <= Here set you shipping class ID
$('select#product_shipping_class').val(shippingClassID).change();
});
</script><?php
endif;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论