使用Wordpress插件保存数据时出现问题

时间:2018-12-16 03:33:22

标签: wordpress plugins save

我正在使用 Woo预计的发货日期插件在我的产品页面中添加自定义文本(字段)。 问题是:

示例:如果我在以下自定义字段中使用此文本:“有货”,然后保存更改,则插件在产品页面上显示“有货” ... 返回编辑产品页面上的其他内容,插件将还原默认句子“预计交货日期”,如果我保存更改而未再次修改该句子,则产品页面上将显示“预计交货日期”,而不是新句子。就像一个讨厌的循环,保存更改后总是返回默认文本,如果我不看该字段,我的自定义文本就消失了。

这是插件代码:

<?php 
/**
* No cheating please
*/
if ( ! defined( 'WPINC' ) ) exit;

/**
* WCESD_Product_Settings Class
*/
class WCESD_Product_Settings {
/**
 * Hold the instance
 * 
 * @var string
 */
private static $instance;

use helperMethods;

/**
 * Constructor method
 *
 * @return void
 */
public function __construct() {
    if ( ! $this->enabled() ) {
        return;
    }

    $this->init_hooks();
}

/**
 * Init all the hooks
 * 
 * @return void
 */
protected function init_hooks() {
    add_action( 'woocommerce_product_options_shipping', array( $this, 'wc_esd_add_estimated_shipping_date' ) );
    add_action( 'woocommerce_process_product_meta', array( $this, 'wc_esd_save_shipping_date') );       
}

/**
 * Add wcesd form
 * 
 * @return void
 */
public function wc_esd_add_estimated_shipping_date() {
    woocommerce_wp_checkbox( array(
        'id'            => 'wc_esd_date_enable',
        'label'         => __( 'Habilitar fecha estimada entrega', 'wcesd' ),
        'description'   => __( 'Enable or Disable woocommerce estimated shipping date', 'wcesd' ),
        'desc_tip'      => true,
    ) );
    woocommerce_wp_text_input( array(
        'id'            => 'wc_esd_date',
        'label'         => __( 'Fecha estimada en días', 'wcesd' ),
        'description'   => __( 'Días del posible arribo del producto', 'wcesd' ),
        'desc_tip'      => true,
        'type'          => 'number',
        'placeholder'   => 5,
        'value'         => 5,
    ) );
    woocommerce_wp_text_input( array(
        'id'            => 'wc_esd_date_message',
        'label'         => __( 'Frase', 'wcesd' ),
        'description'   => __( 'Agregue su mensaje', 'wcesd' ),
        'desc_tip'      => true,
        'placeholder'   => 'Estimated Delivery Date',
        'value'         => 'Estimated Delivery Date',
    ) );

    do_action( 'wc_esd_add_estimated_shipping_date' );
}

/**
 * Save wcesd form data
 * 
 * @param  int $product_id
 * 
 * @return void
 */
public function wc_esd_save_shipping_date( $product_id ) {
    if ( ! is_admin() || get_post_type() !== 'product' ) {
        return;
    }

    $wc_esd_date_enable  = isset( $_POST['wc_esd_date_enable'] ) ? sanitize_text_field( $_POST['wc_esd_date_enable'] ) : '';
    $wc_esd_date         = isset( $_POST['wc_esd_date'] ) ? sanitize_text_field( $_POST['wc_esd_date'] ) : '';
    $wc_esd_date_message = isset( $_POST['wc_esd_date_message'] ) ? sanitize_text_field( $_POST['wc_esd_date_message'] ) : '';

    update_post_meta( $product_id, 'wc_esd_date_enable', $wc_esd_date_enable );
    update_post_meta( $product_id, 'wc_esd_date', $wc_esd_date );
    update_post_meta( $product_id, 'wc_esd_date_message', $wc_esd_date_message );

    do_action( 'wc_esd_save_shipping_date', $product_id );
}

/**
 * Get instance
 * 
 * @return object
 */
public static function init() {
    if ( null === self::$instance ) {
        self::$instance = new self();
    }

    return self::$instance;
}

/**
 * Disable cloning this class
 * 
 * @return void
 */
private function __clone() {
    //
}

private function __wakeup() {
    //
}
}

WCESD_Product_Settings::init();

任何想法如何解决此问题? 谢谢:)

1 个答案:

答案 0 :(得分:1)

请删除值字段

woocommerce_wp_text_input( array(
    'id'            => 'wc_esd_date_message',
    'label'         => __( 'Frase', 'wcesd' ),
    'description'   => __( 'Agregue su mensaje', 'wcesd' ),
    'desc_tip'      => true,
    'placeholder'   => 'Estimated Delivery Date',
    /*'value'         => 'Estimated Delivery Date',*/
) );

然后它应该工作。

相关问题