根据管理产品列表中的库存数量显示自定义库存状态 table
Display custom stock status based on stock quantity in admin products list table
我为 WooCommerce 产品添加了自定义库存状态“即将上市”。我想在产品库存数量等于 1 时更改此状态。
我在感谢页面上尝试了一个钩子,它使用查询更新了数据库中的状态
update_post_meta( $product_id, '_stock_status', 'availablesoon' );
但是当我在 WooCommmerce 产品仪表板中看到该特定产品仍然处于“有货”状态时。
我的完整代码:
add_action( 'woocommerce_thankyou', 'change_stock_status_cstm', 10, 1);
function change_stock_status_cstm( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
$product = $item->get_product();
$product_id = $item->get_variation_id() ? $item->get_variation_id() :
$item->get_product_id();
if ( $product->get_stock_quantity() == 1 ) {
update_post_meta( $product_id, '_stock_status', 'availablesoon' );
}
}
}
任何人都可以告诉我如何在仪表板中更新库存状态吗?
你可以试试这个。
$product = wc_get_product( $variant_post_id );
wc_update_product_stock( $product, $qty_new , 'set' );
wc_delete_product_transients( $variant_post_id );
如果您想在管理产品列表中显示自定义库存状态table,根据当前库存数量,您可以使用:
// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
if ( $product->get_stock_quantity() == 1 ) {
$stock_html = '<mark class="my-class" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Available soon', 'woocommerce' ) . '</mark>';
}
return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
相关:
我为 WooCommerce 产品添加了自定义库存状态“即将上市”。我想在产品库存数量等于 1 时更改此状态。
我在感谢页面上尝试了一个钩子,它使用查询更新了数据库中的状态
update_post_meta( $product_id, '_stock_status', 'availablesoon' );
但是当我在 WooCommmerce 产品仪表板中看到该特定产品仍然处于“有货”状态时。
我的完整代码:
add_action( 'woocommerce_thankyou', 'change_stock_status_cstm', 10, 1);
function change_stock_status_cstm( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
$product = $item->get_product();
$product_id = $item->get_variation_id() ? $item->get_variation_id() :
$item->get_product_id();
if ( $product->get_stock_quantity() == 1 ) {
update_post_meta( $product_id, '_stock_status', 'availablesoon' );
}
}
}
任何人都可以告诉我如何在仪表板中更新库存状态吗?
你可以试试这个。
$product = wc_get_product( $variant_post_id );
wc_update_product_stock( $product, $qty_new , 'set' );
wc_delete_product_transients( $variant_post_id );
如果您想在管理产品列表中显示自定义库存状态table,根据当前库存数量,您可以使用:
// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
if ( $product->get_stock_quantity() == 1 ) {
$stock_html = '<mark class="my-class" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Available soon', 'woocommerce' ) . '</mark>';
}
return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
相关: