2017-01-13 12 views
6

मैं Woocommerce करने के लिए कस्टम फ़ील्ड जोड़ लिया है/उत्पाद/सामान्य टैब इस उत्कृष्ट ट्यूटोरियल http://www.remicorson.com/mastering-woocommerce-products-custom-fields/woocommerce calculate और बचाने के मूल्य

कस्टम फ़ील्ड (ऊंचाई, चौड़ाई, गुणक) डेटाबेस ठीक करने के लिए सहेजा जा रहा है का उपयोग कर जोड़ें। मैं कस्टम फ़ील्ड के आधार पर मूल्य की गणना करना चाहता हूं, और कीमत को डेटाबेस में नियमित मूल्य के रूप में सहेजना चाहता हूं। समस्या यह है कि कीमत सहेजा नहीं जा रहा है।
यहाँ मेरे बच्चे विषय में functions.php से मेरे कोड है,:

/* CUSTOM FIELDS: Add custom fields to Product/General pane in Woocommerce 
    from http://www.remicorson.com/mastering-woocommerce-products-custom-fields/ */ 

// Display Fields 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 
// Save Fields 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 

function woo_add_custom_general_fields() 
{ 
    global $woocommerce, $post; 
    echo '<div class="options_group">'; 

    // Product Height 
    woocommerce_wp_text_input(
     array(
      'id' => '_product_height', 
      'label' => __('Product Height (inches)', 'woocommerce'), 
      'placeholder' => '', 
      'description' => __('Enter the product height in inches here.', 'woocommerce'), 
      'type' => 'number', 
      'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
     ) 
    ); 

    // Product Width 
    woocommerce_wp_text_input(
     array(
      'id' => '_product_width', 
      'label' => __('Product Width (inches)', 'woocommerce'), 
      'placeholder' => '', 
      'description' => __('Enter the product width in inches here.', 'woocommerce'), 
      'type' => 'number', 
      'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
     ) 
    ); 

    // Product Area Multiplier 
    woocommerce_wp_text_input(
     array(
      'id' => '_product_area_mult', 
      'label' => __('Product Area Multiplier', 'woocommerce'), 
      'placeholder' => '', 
      'description' => __('Enter Product Area Multiplier. ', 'woocommerce'), 
      'type' => 'number', 
      'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
     ) 
    ); 

    // Product Area Price: added this field to check if this value is being calculated 
    woocommerce_wp_text_input(
     array(
      'id' => '_product_area_price', 
      'label' => __('Product Area Price', 'woocommerce'), 
      'placeholder' => '', 
      'description' => __('Product Area Price. Calculated automatically', 'woocommerce'), 
      'type' => 'number', 
      'custom_attributes' => array(
       'step' => 'any', 
       'min' => '0' 
      ) 
     ) 
    ); 

    echo '</div>'; 
} 

function woo_add_custom_general_fields_save($post_id) 
{ 
    $woocommerce_product_height = $_POST['_product_height']; 
    $woocommerce_product_width = $_POST['_product_width']; 
    $woocommerce_product_area_mult = $_POST['_product_area_mult']; 
    $woocommerce_product_area_price = $_POST['_product_area_price']; 

    // save height, width, multiplier 
    if (!empty($woocommerce_product_height)) 
     update_post_meta($post_id, '_product_height', esc_attr($woocommerce_product_height)); 

    if (!empty($woocommerce_product_width)) 
     update_post_meta($post_id, '_product_width', esc_attr($woocommerce_product_width)); 

    if (!empty($woocommerce_product_area_mult)) 
     update_post_meta($post_id, '_product_area_mult', esc_attr($woocommerce_product_area_mult)); 


    // calculate and save _product_area_price, _regular_price, price as Height*Width*Mult 
    if (!empty($woocommerce_product_height) && !empty($woocommerce_product_width) && !empty($woocommerce_product_area_mult)) 
     $woocommerce_product_area_price = $woocommerce_product_height * $woocommerce_product_width * $woocommerce_product_area_mult; 

    if (!empty($woocommerce_product_area_price)) 
     update_post_meta($post_id, '_product_area_price', esc_attr($woocommerce_product_area_price)); 

    if (!empty($woocommerce_product_area_price)) 
    { 
     update_post_meta($post_id, '_regular_price', esc_attr($woocommerce_product_area_price)); 
     update_post_meta($post_id, '_price', esc_attr($woocommerce_product_area_price)); 
    } 
} 

सब कुछ डेटाबेस में कीमत क्षेत्रों को अद्यतन करने को छोड़कर काम कर रहा है। मेरे कस्टम फ़ील्ड एक ही वाक्यविन्यास के साथ अद्यतन कर रहे हैं। मैंने पुष्टि की है कि परिवर्तनीय $woocommerce_product_area_price मौजूद है, और यह कस्टम फ़ील्ड अपडेट कर रहा है, लेकिन वही चर _regular_price या _price फ़ील्ड अपडेट नहीं कर रहा है। तो इन पंक्तियों के काम नहीं कर रहे हैं, भले ही एक ही चर _product_area_price फ़ील्ड अपडेट होगा:

if (!empty($woocommerce_product_area_price)) 
{ 
    update_post_meta($post_id, '_regular_price', esc_attr($woocommerce_product_area_price)); 
    update_post_meta($post_id, '_price', esc_attr($woocommerce_product_area_price)); 
} 

हो सकता है कि वहाँ कुछ वाक्य रचना है या जाँच Woocommerce में कीमत अद्यतन करने के लिए मुझे याद आ रही?

+0

क्या आपने डीबी में चेक किया है कि कीमत बचाई जा रही है या नहीं? या आप समस्या यह है कि यह फ्रंटेंड में परिलक्षित नहीं हो रहा है? –

+0

डेटाबेस, राउनाक में सहेजा नहीं जा रहा है। – Norv

उत्तर

2

मुझे क्या बात है कि आपको कीमत के साथ कोई समस्या है जो सामने की ओर दिखा रहा है। यदि आप _regular_price को फ्रंटेंड में प्रतिबिंबित करना चाहते हैं तो आपको _price कुंजी अपडेट करने की आवश्यकता है।

if (!empty($area_price)){ 
    update_post_meta($post_id, '_regular_price', esc_attr($area_price)); 
    update_post_meta($post_id, '_price', esc_attr($area_price)); //<-- Add this line. 
} 

UPDATED

बस एक उच्च प्राथमिकता जोड़नेwoocommerce_process_product_meta कार्रवाई हुक करने के लिए, यह 'चाल करना होगा:

इस कोड का प्रयास करें।

add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save', 99); 

मैंने इसका परीक्षण किया है और यह ठीक काम कर रहा है।
आशा है कि इससे मदद मिलती है!

+0

@ user3547460: क्या आप अपने प्रश्न को अपडेट कर सकते हैं कि आपने 'woo_add_custom_general_fields_save' के लिए किस हुक का उपयोग किया है। –

+0

मैंने यह दिखाने के लिए पूर्ण कोड स्निपेट शामिल किया था कि हुक woocommerce_process_product_meta – Norv

+0

बहुत बहुत धन्यवाद राणाक, आपने अपना दिन चैंप बचाया! – Norv