2016-09-02 15 views
6

Tôi đã tạo trường tùy chỉnh để bảo hành trong các trang sản phẩm của tôi, thông qua function.php.Đặt trường tùy chỉnh sản phẩm và giá trị hiển thị trong giỏ hàng, thanh toán và xem đơn đặt hàng

add_action('woocommerce_product_options_general_product_data', 'test_custom_fields'); 
function test_custom_fields() { 
    // Print a custom text field 
    woocommerce_wp_text_input(array(
     'id' => '_warranty', 
     'label' => 'i.e. 15 years', 
     'description' => '', 
     'desc_tip' => 'true', 
     'placeholder' => 'i.e. 15 years' 
    ));   
} 

add_action('woocommerce_process_product_meta', 'test_save_custom_fields'); 
function test_save_custom_fields($post_id) { 
    if (! empty($_POST['_warranty'])) { 
     update_post_meta($post_id, '_warranty', esc_attr($_POST['_warranty'])); 
    } 
} 

Tôi muốn "trùng lặp" trường tùy chỉnh này với khóa và giá trị, trong trường tùy chỉnh tự tạo trên trang đặt hàng quản trị tùy thuộc vào sản phẩm trong giỏ hàng/đơn đặt hàng (không có plugin).

Vì vậy, với trường tùy chỉnh này trên trang đặt hàng, cuối cùng tôi sẽ có thể hiển thị "bảo hành" trong hóa đơn pdf của mình bằng plugin Hóa đơn PDF WooCommerce.

Một lời giải thích:

  1. Như admin, tôi điền vào giá trị _warranty trong trang "Product1"
  2. Khi "Product1" là trong một trật tự, trên quan điểm trật tự quản trị, tôi muốn nhìn thấy một tùy chỉnh trường chứa "_warranty + giá trị" từ trang sản phẩm1.
  3. Vì vậy, như admin, tôi có thể thiết lập {{_warranty}} trong WooCommerce PDF Invoice plugin để hiển thị "Bảo hành: 15 năm"

Rất cám ơn sự giúp đỡ của bạn.

Tôi vừa kiểm tra các trường hợp sau đây: show product meta in order items table in Order Details

Nhưng điều này không cho tôi một lĩnh vực tùy chỉnh, vì vậy tôi không thể nhận được giá trị {{_warranty}} tôi rộng nó.

Tôi đang làm gì sai?
Làm cách nào để đạt được điều này?

Cảm ơn.

+0

Âm thanh như [Product Add Ons] (https://woocommerce.com/products/product-add-ons/) có thể phù hợp với bạn. – helgatheviking

Trả lời

5

Đầu tiên:"nhân đôi lĩnh vực tùy chỉnh này với khoá và giá trị trong một lĩnh vực tùy chỉnh tự tạo ra trên trang đặt hàng admin"không phải là cách tiếp cận tốt.

Để đạt được những gì bạn mong đợi, bạn đã bỏ lỡ một số điều nhỏ nhặt. Bạn cần phải:

  1. cửa hàng lĩnh vực tùy chỉnh trong giỏ hàng (khi sản phẩm được thêm vào giỏ hàng)
  2. Render này vào giỏ hàng và thanh toán trang
  3. Thêm thông tin theo thứ tự như dữ liệu meta (để làm cho nó một phần của trật tự)

với điểm 3 bạn sẽ có thể để có được điều này trên WooCommerce PDF Invoice plugin để hiển thị "Bảo hành: 15 năm".

Vì vậy, các mã bạn cần là:

// create the custom field on product admin tab 
add_action('woocommerce_product_options_general_product_data', 'create_warranty_custom_field'); 
function create_warranty_custom_field() { 
    // Create a custom text field 
    woocommerce_wp_text_input(array(
     'id'   => '_warranty', 
     'type'   => 'text', 
     'label'   => __('Warranty', 'woocommerce'), 
     'description' => '', 
     'desc_tip'  => 'true', 
     'placeholder' => __('i.e. 15 years', 'woocommerce'), 
    )); 
} 

// save the data value from this custom field on product admin tab 
add_action('woocommerce_process_product_meta', 'save_warranty_custom_field'); 
function save_warranty_custom_field($post_id) { 
    $wc_text_field = $_POST['_warranty']; 
    if (!empty($wc_text_field)) { 
     update_post_meta($post_id, '_warranty', esc_attr($wc_text_field)); 
    } 
} 

// Store custom field in Cart 
add_action('woocommerce_add_cart_item_data', 'store_warranty_custom_field', 10, 2); 

function store_warranty_custom_field($cart_item_data, $product_id) { 
    $warranty_item = get_post_meta($product_id , '_warranty', true); 
    if(!empty($warranty_item)) { 
     $cart_item_data[ '_warranty' ] = $warranty_item; 

     // below statement make sure every add to cart action as unique line item 
     $cart_item_data['unique_key'] = md5(microtime().rand()); 
     WC()->session->set('days_manufacture', $warranty_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['_warranty'])) { 
     $custom_items[] = array("name" => __("Warranty", "woocommerce"), "value" => $cart_item['_warranty']); 
    } 
    return $custom_items; 
} 

// Add the information in the order as meta data 
add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3); 
function add_waranty_to_order_item_meta($item_id, $values, $cart_item_key) { 
    // Retrieving the product id for the order $item_id 
    $prod_id = wc_get_order_item_meta($item_id, '_product_id', true); 
    // Getting the warranty value for this product Id 
    $warranty = get_post_meta($product_id, '_warranty', true); 
    // Add the meta data to the order 
    wc_add_order_item_meta($item_id, 'Warranty', $warranty, true); 
} 

Đương nhiên, điều này đi vào hoạt động.tập tin php của chủ đề con chủ động của bạn (hoặc chủ đề) hoặc cũng trong bất kỳ tập tin plugin.

Mã này được kiểm tra và hoạt động.


Tài liệu tham khảo:

+0

Hi LoicTheAztec! Và cảm ơn rất nhiều vì sự giúp đỡ của bạn! – pierre

+0

Tôi vừa thử nghiệm mã của bạn và tiếc là tôi nhận được 502 Bad Gatweay khi tôi áp dụng nó trong hàm.php.php của mình. Bạn có ý tưởng gì không? Và nếu có thể, tôi không muốn thêm trường này vào trang giỏ hàng và thanh toán của mình, thông tin "bảo hành" này không cần thiết trên các trang này. Nó chỉ cần thiết trong hóa đơn của tôi. Bạn cũng có thể vui lòng xóa mã không cần thiết khỏi Tôi không phải là nhà phát triển, vì vậy rất noob trong php ... cảm ơn rất nhiều! – pierre

0

Có một lỗi ở phần cuối của mã này, bạn gọi một biến $prod_id lúc đầu, và sau đó $product_id. Mã đúng và đang hoạt động là:

// Add the information in the order as meta data 
add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3); 
function add_waranty_to_order_item_meta($item_id, $values, $cart_item_key) { 
    // Retrieving the product id for the order $item_id 
    $prod_id = wc_get_order_item_meta($item_id, '_product_id', true); 
    // Getting the warranty value for this product Id 
    $warranty = get_post_meta($prod_id, '_warranty', true); 
    // Add the meta data to the order 
    wc_add_order_item_meta($item_id, 'Warranty', $warranty, true); 
} 
Các vấn đề liên quan