Vin*_*ano 4 php wordpress backend woocommerce hook-wordpress
基于“在woocommerce的管理订单列表顶部添加一个按钮”答案代码,我能够在woocommerce管理订单列表上添加一个自定义按钮。
这是代码(稍微定制):
add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
global $typenow;
if ( 'shop_order' === $typenow && 'top' === $which ) {
?>
<div class="alignleft actions custom">
<button type="submit" name="custom_" style="height:32px;" class="button" value=""><?php
echo __( 'Import Couriers', 'woocommerce' ); ?></button>
</div>
<?php
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当单击此自定义按钮时,我需要运行以下函数:
function update_shipping_couriers_meta_field() {
$dir = __DIR__;
$couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$count = count(couriers);
$i = 1;
do {
if ( !empty( $couriers ) ) {
foreach ( $couriers as $a ) {
if ( !empty( $a ) ) {
$rows = explode(';', $a);
$id = $rows[0];
$id = int($id);
$couriers = $rows[1];
update_post_meta( $id, '_shipping_couriers', $couriers );
}
$i++;
}
}
}
while ( $i <= $count );
}
Run Code Online (Sandbox Code Playgroud)
实际上,该函数会根据特定订单 ID 更新“_shipping_couriers”自定义字段。这两个值存在于一个 csv 文件中。
我已经测试过它并且它正在工作。当我点击我用上面的函数创建的按钮时,我“只是”让它运行。
单击按钮时如何运行我的函数?
您的代码中缺少一些内容,而您的最后一个函数中存在错误,count(couriers);需要改为count($couriers);。
// Display an action button in admin order list header
add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
function admin_order_list_top_bar_button( $which ) {
global $pagenow, $typenow;
if ( 'shop_order' === $typenow && 'edit.php' === $pagenow && 'top' === $which ) {
?>
<div class="alignleft actions custom">
<button type="submit" name="import_courier" style="height:32px;" class="button" value="yes"><?php
echo __( 'Import Couriers', 'woocommerce' ); ?></button>
</div>
<?php
}
}
// Trigger an action (or run some code) when the button is pressed
add_action( 'restrict_manage_posts', 'display_admin_shop_order_language_filter' );
function display_admin_shop_order_language_filter() {
global $pagenow, $typenow;
if ( 'shop_order' === $typenow && 'edit.php' === $pagenow &&
isset($_GET['import_courier']) && $_GET['import_courier'] === 'yes' ) {
## -------- The code to be trigered -------- ##
update_shipping_couriers_meta_field();
## -------------- End of code -------------- ##
}
}
// Your function that will be triggered on button press
function update_shipping_couriers_meta_field() {
$dir = __DIR__;
$couriers = file( $dir . '/import-couriers.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$count = count($couriers);
$i = 1;
do {
if ( ! empty( $couriers ) ) {
foreach ( $couriers as $a ) {
if ( ! empty( $a ) ) {
$rows = explode(';', $a);
update_post_meta( intval($rows[0]), '_shipping_couriers', $rows[1] );
}
$i++;
}
}
}
while ( $i <= $count );
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。
基于:在woocommerce的管理订单列表顶部添加一个按钮
| 归档时间: |
|
| 查看次数: |
1990 次 |
| 最近记录: |