我有一個函式在 WooCommerce 中獲取一串“提前期”。一個例子是:Available! Estimated Lead Time: 2-3 Weeks. 我想得到2-3 Weeks. 到目前為止,我已經能夠使用以下功能做到這一點:
function display_lead_time_notice_card() {
global $product;
//Get Stock Status
$in_stock = $product->get_availability()['class'];
//If In Stock, parse the lead time
if ($in_stock == 'in-stock') {
//Lead Time Statement:
$availability = $product->get_availability()['availability'];
// Extract "2-3 Weeks"
preg_match('/[0-9]-[0-9] Weeks/', $availability, $matches, PREG_OFFSET_CAPTURE, 0);
// Get Lead Time
$lead_time = $matches[0][0];
echo '
<div >
<h4>A note on lead times:</h4>
<p >A Lead Time of ' . $lead_time . ' is just an estimate. Lead Times are subject to change depending on the current shop capacity and size of the order. We will always try to complete your order quicker than the stated lead time.</p>
</div>
';
}
}
add_action('woocommerce_single_variation', 'display_lead_time_notice_card', 10);
這是可行的,但我想知道是否有一種更清潔、更簡單的方法可以從$availability變數中提取子字串。
uj5u.com熱心網友回復:
很難相信這將永遠是幾周,可能是幾天還是幾個月?
話雖這么說,如果字串是一致的,就explode在冒號和空格上:
$lead_time = explode(': ', $availability)[1];
為了娛樂:
$lead_time = ltrim($availability, 'A..z!: ');
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/473013.html
