Отображение пользовательского хука на странице продукта WooCommerce - PullRequest
2 голосов
/ 30 марта 2020

Я думаю, что это очень легко исправить - но каждый раз, когда я вставляю хук - я получаю критическую ошибку WordPress, и я не знаю, что я делаю неправильно - новичок в Hooks, поэтому, пожалуйста, прости меня.

Я пытаюсь просто напечатать значение, которое я сохранил в бэкэнде, чтобы вывести его на передний конец.

Заранее спасибо!

(этот код в функции. php)

//Custom Colour Text Box

add_action( 'woocommerce_product_options_inventory_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
    // Print a custom text field
    woocommerce_wp_text_input( array(
        'id' => '_custom_text_field',
        'label' => 'Product Colour',
        'description' => 'This is where you put the colour of the product in.',
        'desc_tip' => 'true',
        'placeholder' => 'Custom Colour'
    ) );
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields( $post_id ) {
    if ( ! empty( $_POST['_custom_text_field'] ) ) {
        update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title', 2 );
function custom_field_display_below_title(){
    global $product;

    // Get the custom field value
    $custom_field = get_post_meta( $product->get_id(), '_custom_product_text_field', true );

    // Display
    if( ! empty($custom_field) ){
        echo '<p class="my-custom-field">'.$custom_field.'</p>';
    }
}

/**** Display on the Product Page ***/

add_action( 'woocommerce_single_product_summary', 'custom_text', 9 );
function custom_text() {
  print '<p class="my-custom-field"></p>';
}

1 Ответ

1 голос
/ 30 марта 2020

Я не получаю сообщение об ошибке, я внес небольшие изменения здесь и там.

  • if ( ! empty( $_POST[.. будет работать, только если поле НЕ пусто, если ничего не заполнено, оно не будет сохранено.
  • get_post_meta( $product->get_id(), '_custom_product_text_field',... _custom_product_text_field - это не то же самое, что _custom_text_field
  • при woocommerce_single_product_summary вы печатаете только текст, соответствующий код отсутствует
//Custom Colour Text Box
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
    // Print a custom text field
    woocommerce_wp_text_input( array(
        'id' => '_custom_text_field',
        'label' => 'Product Colour',
        'description' => 'This is where you put the colour of the product in.',
        'desc_tip' => 'true',
        'placeholder' => 'Custom Colour'
    ) );
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields', 10, 1 );
function wc_custom_save_custom_fields( $post_id ) {
    $product = wc_get_product( $post_id );

    $my_text_field = isset( $_POST['_custom_text_field'] ) ? $_POST['_custom_text_field'] : '';

    $product->update_meta_data( '_custom_text_field', sanitize_text_field( $my_text_field ) );
    $product->save();
}

add_action( 'woocommerce_after_shop_loop_item_title', 'custom_field_display_below_title' );
function custom_field_display_below_title() {
    global $post;

    // Get product
    $product = wc_get_product( $post->ID );

    // Check for the custom field value
    $my_text_field = $product->get_meta( '_custom_text_field' );

    // Display
    if( $my_text_field ) {
        echo '<p class="my-custom-field">' . $my_text_field . '</p>';
    }
}

/**** Display on the Product Page ***/
add_action( 'woocommerce_single_product_summary', 'custom_text' );
function custom_text() {
    global $post;

    // Get product
    $product = wc_get_product( $post->ID );

    // Check for the custom field value
    $my_text_field = $product->get_meta( '_custom_text_field' );

    // Display
    if( $my_text_field ) {
        echo '<p class="my-custom-field">' . $my_text_field . '</p>';
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...