2016-09-02 8 views
6

मैंने function.php के माध्यम से अपने उत्पादों के पृष्ठों में वारंटी के लिए एक कस्टम फ़ील्ड बनाया है।कार्ट में कस्टम कस्टम फ़ील्ड और डिस्प्ले वैल्यू सेट करें, चेकआउट और ऑर्डर

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'])); 
    } 
} 

मैं (प्लगइन के बिना) गाड़ी/क्रम में उत्पादों के आधार पर व्यवस्थापक आदेश पृष्ठ पर कुंजी और मान के साथ इस कस्टम फ़ील्ड "नकल" करने के लिए, एक स्वयं उत्पन्न कस्टम फ़ील्ड में करना चाहते हैं।

तो, ऑर्डर पेज पर इस कस्टम फ़ील्ड के साथ, मैं अंततः WooCommerce पीडीएफ चालान प्लगइन के साथ अपने पीडीएफ चालान में "वारंटी" प्रदर्शित करने में सक्षम हूं।

एक अन्य स्पष्टीकरण:

  1. व्यवस्थापक के रूप में, मैं में "उत्पाद 1" पृष्ठ _warranty मूल्य भरने
  2. जब "उत्पाद 1" एक आदेश में है, व्यवस्थापक आदेश दृश्य पर, मैं एक कस्टम देखना चाहेंगे उत्पाद 1 पृष्ठ से "_warranty + value" युक्त फ़ील्ड।

आपकी मदद के लिए बहुत धन्यवाद:

  • तो, व्यवस्थापक के रूप में, मैं {{_warranty}} WooCommerce पीडीएफ चालान प्लगइन में "15 साल वारंटी" प्रदर्शित करने के लिए सेट कर सकते हैं।

    मैं सिर्फ निम्नलिखित मामले का परीक्षण किया है: show product meta in order items table in Order Details

    लेकिन यह मुझे एक कस्टम फ़ील्ड नहीं देता है, इसलिए मैं अपने {{_warranty}} मूल्य यह चौड़ाई नहीं मिल सका।

    मैं क्या गलत कर रहा हूं?
    मैं इसे कैसे प्राप्त कर सकता हूं?

    धन्यवाद।

  • +0

    जैसे [उत्पाद जोड़ें ऑन] (https://woocommerce.com/products/product-add-ons/) आपके लिए एक अच्छा फिट हो सकता है। – helgatheviking

    उत्तर

    5

    पहले:"कुंजी और मान के साथ इस कस्टम फ़ील्ड नकल, व्यवस्थापक आदेश पृष्ठ पर एक स्वयं उत्पन्न कस्टम फ़ील्ड में"अच्छा तरीका नहीं है।

    आप जो उम्मीद कर रहे हैं उसे प्राप्त करने के लिए, आपने कुछ छोटी चीजें याद कर दी हैं। आप की जरूरत करने के लिए:

    1. स्टोर कार्ट में कस्टम फ़ील्ड (जब उत्पाद गाड़ी में जोड़ा जाता है)
    2. कार्ट और चेकआउट पृष्ठों पर यह प्रस्तुत
    3. मेटा डेटा के रूप में क्रम में जानकारी जोड़ें (यह हिस्सा बनाने के लिए ": 15 साल की वारंटी" आदेश का)

    बिंदु 3 के साथ आप प्रदर्शित करने के लिए WooCommerce पीडीएफ चालान प्लगइन पर इस प्राप्त करने में सक्षम हो जाएगा।

    तो कोड की जरूरत है:

    // 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); 
    } 
    

    जाहिर है, इस समारोह में चला जाता है।आपके सक्रिय बच्चे विषय (या विषय) की PHP फ़ाइल या किसी भी प्लगइन फ़ाइल में भी।

    यह कोड परीक्षण और काम करता है।


    संदर्भ:

    +0

    हाय LoicTheAztec! और आपकी मदद के लिए बहुत धन्यवाद! – pierre

    +0

    मैंने अभी आपके कोड का परीक्षण किया है और दुर्भाग्यवश जब मैं इसे अपने function.php में लागू करता हूं तो मुझे 502 खराब गेटवे मिलते हैं। क्या आपके पास कोई विचार है? और यदि संभव हो, तो मैं इस क्षेत्र को अपने कार्ट और चेकआउट पृष्ठों में जोड़ना नहीं चाहता, इस पृष्ठ पर यह जानकारी "वारंटी" आवश्यक नहीं है। यह केवल मेरे चालान में जरूरी है। क्या आप से अनावश्यक कोड को भी हटा सकते हैं, मैं डेवलपर नहीं हूं, इसलिए PHP में बहुत नोब ... बहुत धन्यवाद! – pierre

    0

    वहाँ कोड के अंत में एक त्रुटि है, तो आप पहली बार में एक चर $prod_id, और फिर $product_id कहते हैं। सही और कामकाजी कोड है:

    // 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); 
    } 
    
    संबंधित मुद्दे