我正在尝试让init函数仅在post_type为shop_order且使用以下代码的情况下发生。我的意图是在编辑模式下将元框显示为管理订单/单击创建新订单时,但是在get_post_type中的以下代码为shop_order时,该元框未出现在管理订单编辑视图中。
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
class DM_Apply_Order_Discount {
public function __construct()
{
$this->init();
}
public function init() {
global $post;
$postid = $post->ID;
if( get_post_type($postid) === 'shop_order' ) {
add_action('add_meta_boxes', array($this, 'add_apply_discount_container'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
}}
public function enqueue_scripts() {
wp_enqueue_script('hellodev-discount-manager-js', plugins_url('js/hellodev-discount-manager-apply-discount-min.js', DM_PLUGIN_FILE), array(), '1.0', false);
wp_localize_script('hellodev-discount-manager-js',
'hellodev_discount_manager_locales',
array(
'confirm_apply_discount' => __('Applying the discount will overwrite existing discounts. Do you want to continue?', 'hellodev-discount-manager-apply-discount'),
'apply_success_message' => __('Saved with success.', 'hellodev-discount-manager-apply-discount'))
);
}
public function add_apply_discount_container() {
global $post_id;
$order = new WC_Order($post_id);
if (!$order->is_editable()) {
return;
}
add_meta_box('hellodev-discount-manager-apply-discount-container', __('Apply discount to all items', 'hellodev-discount-manager-apply-discount'),
array($this, 'create_apply_discount_container'), 'shop_order', 'side');
}
public function create_apply_discount_container() {
?>
<ul id="hellodev-discount-manager-apply-discount" class="hellodev-discount-manager-apply-discount">
<li>
<input id="hellodev-discount-manager-apply-discount-percentage" type="number" name="discount" min="0" max="100" step="1"
placeholder="<?php _e("Discount in percentage (%)", "hellodev-discount-manager-apply-discount") ?>" style="width: 100%"/>
</li>
</ul>
<?php
}
}
new DM_Apply_Order_Discount();
出了什么问题?
答案 0 :(得分:0)
请使用全局$ post与$ post_id进行核对;
public function init() {
global $post;
$postid = $post->ID;
if( get_post_type($postid) === 'shop_order' ) {
add_action('add_meta_boxes', array($this, 'add_apply_discount_container'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
}}
希望对您有帮助。