2017-02-25 51 views
0

我在函數function.php中的這個簡單函數上已經足夠了,讓我添加一個複選框給優惠券。但是,一旦我保存/更新優惠券,我的複選框值(選中/未選中)不會被提交(因此複選框始終未選中)。換句話說,當我更新/保存時,我無法在postmetas的meta_value列中將值更新爲yes。複選框在那裏,我只是不能使用它......非常令人沮喪!在我做錯了任何sugestions,請:)Woocommerce優惠券添加自定義複選框

function add_coupon_revenue_dropdown_checkbox() { 
$post_id = $_GET['post']; 

woocommerce_wp_checkbox(array('id' => 'include_stats', 'label' => __('Coupon check list', 'woocommerce'), 'description' => sprintf(__('Includes the coupon in coupon check drop-down list', 'woocommerce')))); 

$include_stats = isset($_POST['include_stats']) ? 'yes' : 'no'; 

update_post_meta($post_id, 'include_stats', $include_stats); 

do_action('woocommerce_coupon_options_save', $post_id); 

}add_action('woocommerce_coupon_options', 'add_coupon_revenue_dropdown_checkbox', 10, 0); 

我想影響的部分是:

的wp-content /插件/ woocommerce /包括/管理/元盒/ class-wc-meta-box-coupon-data.php

回答

2

你的代碼存在的問題是,你試圖將複選框的值保存在爲其生成html的相同函數中。這不起作用。您需要將當前的函數分成兩部分,這兩部分運行在兩個不同的WooCommerce掛鉤上。

首先是顯示實際複選框:

function add_coupon_revenue_dropdown_checkbox() { 
    woocommerce_wp_checkbox(array('id' => 'include_stats', 'label' => __('Coupon check list', 'woocommerce'), 'description' => sprintf(__('Includes the coupon in coupon check drop-down list', 'woocommerce')))); 
} 
add_action('woocommerce_coupon_options', 'add_coupon_revenue_dropdown_checkbox', 10, 0); 

第二是保存複選框的值正在處理所提交的形式時。

function save_coupon_revenue_dropdown_checkbox($post_id) { 
    $include_stats = isset($_POST['include_stats']) ? 'yes' : 'no'; 
    update_post_meta($post_id, 'include_stats', $include_stats); 
} 
add_action('woocommerce_coupon_options_save', 'save_coupon_revenue_dropdown_checkbox'); 
+0

哈哈,在我看到你的之前張貼了我的回答!是的,我現在瞭解這個過程。謝謝您的回答 :) – axelra82