2015-06-08 8 views
7

मैं अपने woocommerce साइटक्वेरी woocommerce बुकिंग उपलब्धता दिनांक द्वारा

के लिए Woothemes booking का उपयोग के रूप में मैं उत्पादों के लिए कस्टम खोज का निर्माण, मैं उपलब्धता भी के लिए खोज करने के लिए है, यहाँ मैं कैसे बो Booking plugin B.O से उपलब्धता को प्रबंधित करने और प्लगइन चाहिए डेटाबेस के लिए भंडार

a:8:{i:0;a:4:{s:4:"type";s:4:"days";s:8:"bookable";s:3:"yes";s:4:"from";s:1:"3";s:2:"to";s:1:"3";}i:1;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-09";s:2:"to";s:10:"2015-07-09";}i:2;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-08";s:2:"to";s:10:"2015-07-08";}i:3;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-10";s:2:"to";s:10:"2015-07-10";}i:4;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-15";s:2:"to";s:10:"2015-07-15";}i:5;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-16";s:2:"to";s:10:"2015-07-16";}i:6;a:4:{s:4:"type";s:6:"custom";s:8:"bookable";s:3:"yes";s:4:"from";s:10:"2015-07-17";s:2:"to";s:10:"2015-07-17";}i:7;a:4:{s:4:"type";s:6:"months";s:8:"bookable";s:2:"no";s:4:"from";s:1:"8";s:2:"to";s:1:"8";}} 

मैं वर्डप्रेस में निर्मित समारोह get_post_meta से इन मूल्यों को प्राप्त कर सकते हैं,

$avail = get_post_meta($product->id, '_wc_booking_availability'); 

और परिणाम है:

Array 
(
[0] => Array 
    (
     [type] => days 
     [bookable] => yes 
     [from] => 3 
     [to] => 3 
    ) 

[1] => Array 
    (
     [type] => custom 
     [bookable] => yes 
     [from] => 2015-07-09 
     [to] => 2015-07-09 
    ) 

[2] => Array 
    (
     [type] => custom 
     [bookable] => yes 
     [from] => 2015-07-08 
     [to] => 2015-07-08 
    ) 

[3] => Array 
    (
     [type] => custom 
     [bookable] => yes 
     [from] => 2015-07-10 
     [to] => 2015-07-10 
    ) 

[4] => Array 
    (
     [type] => custom 
     [bookable] => yes 
     [from] => 2015-07-15 
     [to] => 2015-07-15 
    ) 

[5] => Array 
    (
     [type] => custom 
     [bookable] => yes 
     [from] => 2015-07-16 
     [to] => 2015-07-16 
    ) 

[6] => Array 
    (
     [type] => custom 
     [bookable] => yes 
     [from] => 2015-07-17 
     [to] => 2015-07-17 
    ) 

[7] => Array 
    (
     [type] => months 
     [bookable] => no 
     [from] => 8 
     [to] => 8 
    ) 

) 

आप उपयोगकर्ता देख सकते हैं अगर उत्पाद बुक किये जा सकते है या दिन या महीने की एक श्रेणी में नहीं निर्दिष्ट करने के लिए सक्षम है, इसलिए मेरे सवाल यह है कि मैं एक अगर जाँच करने के लिए एसक्यूएल क्वेरी कर सकते हैं डेट वैरिएबल इस उत्पाद के लिए उपलब्ध है, मुझे लगता है कि मेटा_क्वायरी नौकरी (नीचे के रूप में) करेगी लेकिन मैं अनुपलब्ध दिनांक के लिए कैसे निर्दिष्ट कर सकता हूं? तुम कैसे सोचते हो?

if($_GET['when']){ 

     /* checkIsAValidDate() >> check date format */ 
     if (checkIsAValidDate($_GET['when'])){ 
      $quand = $_GET['when']; 
      $available = array(
       "post_type" => "product", 
       "meta_query" => array(
        "relation" => "AND", 
        "key"  => '_wc_booking_availability', 
        "value"  => $when, 
        'compare' => 'IN' 
       ) 
      ); 

     } 
    } 

उत्तर

1

मेटा मूल्य के बाद से एक सरणी, it may not be possible to use WP_Meta_Query है। हालांकि मैं मानता हूं कि WP_Meta_Query का उपयोग करने के लिए यह और अधिक शानदार तरीका है, मैंने हमेशा यह फ़ंक्शन वास्तव में भ्रमित कर लिया है, और चूंकि आप बुक करने योग्य ऑब्जेक्ट्स की सरणी प्राप्त कर सकते हैं, ऐसा लगता है कि यह एक फ़ंक्शन लिखने के लिए सीधा होगा:

/** 
* Returns an array of bookable products according to supplied criteria 
* 
* @param int $pid  the $product->ID of the bookable object 
* @param mixed $when  a date in YYYY-MM-DD format or integer representing a day or month 
* @param string $type  a value of 'day', 'month', or 'custom' 
* @param string $bookable whether the bookable object is bookable or not ('yes' or 'no') 
* @return array   an array of bookable objects for the product 
*/ 
function getBookables($pid, $when, $type, $bookable = 'yes') { 
    $result = array(); 

    // is $when in the YYYY-MM-DD format? 
    if ('custom' == $type) { 
     // it's a custom date so convert $when to DateTime 
     $when = DateTime::createFromFormat('Y-m-d', $when); 
    } 

    $availability = get_post_meta($pid, '_wc_booking_availability'); 
    foreach ($availability as $a) { 
     if ($a[ 'bookable' ] == $bookable) { 
      // is it in the YYYY-MM-DD format? 
      if ($when instanceof DateTime) { 
       // it's a custom date so use date compare 
       $from = DateTime::createFromFormat('Y-m-d', $a[ 'from' ]); 
       $to = DateTime::createFromFormat('Y-m-d', $a[ 'to' ]); 
       if ($when >= $from && $when <= $to) { 
        $result[] = $a; 
       } 
      } else { 
       // it is an integer value (day or month) 
       if ($type == $a[ 'type' ] && ($when >= $from && $when <= $to)) { 
        $result[] = $a; 
       } 
      } 
     } 
    } 
    return $result; 
} 

सूचना है कि यह शायद भले ही के बाद से मैं int मूल्यों से YYYY-MM-DD मूल्यों भेद कर सकते हैं खोज करने के लिए समारोह type पारित करने के लिए आवश्यक है, वहाँ day मूल्यों से month भेद करने के लिए कोई आसान तरीका है।

if ($_GET[ 'when' ] && $_GET[ 'type' ]) { 
    $when = $_GET[ 'when' ]; 
    $type = $_GET[ 'type' ]; 
    $bookables = getBookables($product->ID, $when, $type); 
} 

यह भी ध्यान रखें कि यदि आप कार्य करने के लिए चौथे पैरामीटर के रूप में स्ट्रिंग 'no' गुजरती हैं, आप अनुपलब्ध बार की सूची प्राप्त कर सकते हैं: जैसे आप ऊपर फ़ंक्शन का उपयोग हो सकता है।

आशा है कि इससे मदद मिलती है!

संबंधित मुद्दे