Отображение пользовательских мета-значений товара в корзине, оформлении заказа, заказах, везде - PullRequest
0 голосов
/ 18 февраля 2019

Попытка добавить функцию «Дата на складе» в предварительный заказ YITH. Плагин Woocommerce. Бесплатный продукт для продуктов woocommerce.

В соответствии с кодом LoicTheAztec у меня есть следующий код, который добавляет метаданные даты ко всем продуктам.

Плагин YITH добавляет мету "_ywpo_preorder" (yith meta, text: Pre-order product).

Код ниже добавляет мету "_preorder_date" (meta meta, date: Day, Month Name,Год).

Проблема с кодом заключается в том, что для продуктов, отмеченных предзаказом и установлена ​​дата: (мета) заменяется на (мета кода) ТОЛЬКО в заказах на оформление заказа и администратора.

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

Предварительный заказ Название продукта Предварительный заказ продукта (yith meta) Доступно:25 марта 2019 г. (мета-код)

Почему только в Оформлении заказа и (Заказы) Заказы YITH мета пропадает, оставляя только мету даты.Мы будем благодарны за любую помощь, чтобы привязать мета к продуктам предварительного заказа по всей системе.

/* Add AVAILABLE DATE feature to YITH pre-order FREE plugin */

// create the custom field on product admin tab

add_action( 'woocommerce_product_options_general_product_data', 'create_preorder_product_custom_field' );
function create_preorder_product_custom_field() {
    global $post;
    echo '<div class="options_group">';
    woocommerce_wp_text_input( array(
        'id'            => '_preorder_date',
        'type'          => 'date',
        'label'         => __('Available Date', 'woocommerce' ),
        'placeholder'   =>  '',
    ) );
 echo '</div>';
}

// save the data value from this custom field

add_action( 'woocommerce_process_product_meta', 'save_preorder_custom_field' );
function save_preorder_custom_field( $post_id ) {
    global $post;
    $product_meta_yith = get_post_meta( $post->ID, '_ywpo_preorder',true );
    $wc_text_field = $_POST['_preorder_date'];

    if ( ( $product_meta_yith['meta_value']) === 'y' ) {    
        if ( $wc_text_field ) {
            update_post_meta( $post_id, '_preorder_date', esc_attr( $wc_text_field ) );
        }
    }
    else {
        update_post_meta( $post_id, '_preorder_date', '' ); // If pre-order not checked, clear date.
    }
}

// Store custom field in Cart

add_filter( 'woocommerce_add_cart_item_data', 'store_preorder_custom_field', 10, 2 );
function store_preorder_custom_field( $cart_item_data, $product_id ) {

    $preorder_item = get_post_meta( $product_id , '_preorder_date', true );

    if( !empty($preorder_item) ) {
        $cart_item_data[ '_preorder_date' ] = $preorder_item;
    }
    return $cart_item_data;
}

// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {
    $custom_items = array();
    // Woo 2.4.2 updates
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }

    if( isset( $cart_item['_preorder_date'] ) ) {

        $custom_items[] = array( "name" => __( "Available", "woocommerce" ), "value" => date (("d F Y"), strtotime($cart_item['_preorder_date']) ) );
    }


    return $custom_items;
}

// Add the information in the order as meta data

add_action('woocommerce_add_order_item_meta','add_date_to_order_item_meta', 1, 3 );
function add_date_to_order_item_meta( $item_id, $values, $cart_item_key ) {

    // Retrieving the product id for the order $item_id
    $product_id = wc_get_order_item_meta( $item_id, '_product_id', true );

    // Getting the meta value for this product Id
    $preorder = get_post_meta( $product_id, '_preorder_date', true );

    // Add the meta data to the order
    if ($preorder) {
        $preorder_uk = date("d F Y", strtotime($preorder));
        wc_add_order_item_meta($item_id, 'Available', $preorder_uk, true);
    }
}

// Display AVAILABLE DATE on single product

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);
function custom_get_availability( $availability, $_product ) {
  global $post, $product;
  $stock = $product->get_total_stock();

  $product_meta_yith = get_post_meta( $post->ID, '_ywpo_preorder',true );
  $product_meta_date = get_post_meta( $post->ID, '_preorder_date',true );
  $product_meta_date_uk = date("d F Y", strtotime($product_meta_date));


    if ( ( $product_meta_yith['meta_value']) === 'y' ) {
        if ( $_product->is_in_stock() ) { 

            $availability['availability'] = __($stock . ' Left FOR PRE-ORDER', 'woocommerce');

            if  ($product_meta_date) {
                echo 'Available: ' . $product_meta_date_uk;
            }
        }
    }
  return $availability;
}

// Display AVAILABLE DATE on shop loop

add_action( 'woocommerce_before_shop_loop_item_title' , 'fwuk_display_preorder_arrive_date_shop' );
function fwuk_display_preorder_arrive_date_shop ($product) {

  global $post, $product;

  $product_meta_date = get_post_meta( $post->ID, '_preorder_date',true );
  $product_meta_date_uk = date("d F Y", strtotime($product_meta_date));

    if  ($product->is_in_stock()) {
        if  ($product_meta_date) {
                echo '<div class="fwuk-out-of-stock">Available<br />' . $product_meta_date_uk . '</div>';
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...