wordpress - 如何将类别名称中的数字插入 WooCommerce 产品库存号?
问题描述
是否可以仅从类别名称中获取编号(类别 = 衬衫 - 20)& 我希望将“20”插入产品库存编号。现在通过以下代码插入数字“999”
<script>
jQuery( function( $ ) {
$('.post-type-product #product_catchecklist input[type=checkbox]').click( function () {
//Check if post title is empty and if only one category is selected
if( $('.post-type-product #product_catchecklist').find('input[type=checkbox]:checked').length === 1 && $('#_stock').val().length <= 1 ) {
//If so, add the category to the product stock
$('.post-type-product #product_catchecklist input[type=checkbox]').each(function(){
if (this.checked) {
let $category = $(this).closest('label').text();
$('label').html('');
$('#_stock').val(999);
}
});
}
});
});
</script>
<?php
}
解决方案
库存经常分布在仓库和生产车间之间。问题是分布在两个位置的跟踪库存。
令人惊讶的是,没有用于跟踪分布式库存的内置解决方案。我决定创建一个涉及自定义字段的自定义解决方案。将有两个自定义的“工厂库存”字段和“总库存”。
总库存字段将包含工厂库存和 WooCommerce 内置库存控制的总和。这是一种跟踪总库存数量的简单方法,即使它们实际上分布在两个位置。
目前,此解决方案适用于简单产品,但也可以扩展到涵盖可变产品。为了使事情变得更好,我在产品列表列中添加了这些库存水平的两列。
将以下代码添加到 functions.php 文件中。
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action('admin_head', 'my_custom_fonts');
function my_custom_fonts() {
echo '<style>
._total_inventory_field {
color: red;
}
._total_inventory_field input {
background-color:red;
color: #fff;
}
</style>';
}
add_action('save_post', 'myWoo_savePost', 10, 2);
function myWoo_savePost($postID, $post) {
if (isset($post->post_type) && $post->post_type == 'product') {
$shopstock = get_post_meta($post->ID, '_stock', true);
$factorystock = get_post_meta($post->ID, '_factory_inventory', true);
$totalstock = $shopstock + $factorystock;
update_post_meta($post->ID, '_manage_stock', 'yes');
update_post_meta($post->ID, '_total_inventory', $totalstock);
}
}
add_action( 'woocommerce_product_options_inventory_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_factory_inventory',
'label' => __( 'Factory Inventory', 'woocommerce' ),
'description' => __( 'Please Enter Your Factory Count.', 'woocommerce' ),
'type' => 'number',
)
);
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_total_inventory',
'label' => __( 'Total Inventory', 'woocommerce' ),
'placeholder' => "",
'description' => __( 'Stock Must Match.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0',
)
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_number_field = $_POST['_factory_inventory'];
if( !empty( $woocommerce_number_field ) )
update_post_meta( $post_id, '_factory_inventory', esc_attr( $woocommerce_number_field ) );
}
function add_custom_stock_values( $column ) {
$column['factory_stock'] = 'Factory Stock';
$column['total_stock'] = 'Total Stock';
return $column;
}
add_filter( 'manage_product_posts_columns', 'add_custom_stock_values');
function add_value_to_custom_stock( $column_name, $post_id ) {
$custom_fields = get_post_custom( $post_id );
switch ($column_name) {
case 'factory_stock' :
echo $custom_fields['_factory_inventory'][0];
break;
case 'total_stock' :
echo $custom_fields['_total_inventory'][0];
break;
default:
}
}
add_action( 'manage_product_posts_custom_column', 'add_value_to_custom_stock', 10, 2 );
推荐阅读
- python - scipy.minimize ValueError:用户提供的目标函数必须返回一个标量值
- python - Python Numpy:如果满足 np.where 条件,如何分配两个结果?
- swift - CryptoSwift - 在 iOS 中对小于 16 字节的字符串使用 makeEncryptor() 时获得 nil
- javascript - 如何通过电子邮件发送多个附件
- r - 基于一列数据中提到的标题突出显示表格单元格
- java - JPA 存储库 - findLast 返回 NonUniqueResultException?
- sql - TSQL 两个不同的表,800GB vs 2GB 查找共同点
- regex - 字符串中间带有通配符的正则表达式
- java - 我所有的线程都在我的 WebScraper 项目中等待。有人能告诉我为什么吗?
- r - 如何添加对最后一个“n”值进行排序的列?