在自定义按钮上运行功能,在woocommerce管理订单页面中单击 [英] Run a function on custom button click in woocommerce admin order page
问题描述
基于",我能够在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
}
}
现在,当单击此自定义按钮时,我需要运行以下功能:
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 );
}
在实践中,该函数根据特定的订单ID更新"_shipping_couriers"自定义字段.这两个值存在于一个csv文件中.
我已经对其进行了测试,并且可以正常工作.当我单击使用上面的函数创建的按钮时,我就运行"它.
单击按钮后如何运行我的功能?
您的代码中有一些遗漏的东西,而上一个函数中有一个错误,需要替换count($couriers);
.
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 );
}
.
// 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);
$id = $rows[0];
$id = int($id);
$couriers = $rows[1];
update_post_meta( $id, '_shipping_couriers', $couriers );
}
$i++;
}
}
}
while ( $i <= $count );
}
代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
基于:在woocommerce中的管理订单列表顶部添加一个按钮
Based on "Add a button on top of admin orders list in woocommerce" answer code, I was able to add a custom button on woocommerce admin orders list.
Here is that code (lightly customized):
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
}
}
Now I need to run a the following function when this custom button is clicked:
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 );
}
In practice, the function updates a "_shipping_couriers" custom field based on a specific order ID. The two values are present in a csv file.
I've already tested it and it's working. I "just" have it run when I click on the button I created with the function above.
How can I run my function when the button is clicked?
There are some missing things in your code and an error in your last function where count(couriers);
need to be instead 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);
$id = $rows[0];
$id = int($id);
$couriers = $rows[1];
update_post_meta( $id, '_shipping_couriers', $couriers );
}
$i++;
}
}
}
while ( $i <= $count );
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Based from: Add a button on top of admin orders list in woocommerce
这篇关于在自定义按钮上运行功能,在woocommerce管理订单页面中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!