我認為有一個非常簡單的解決方案:
在 WooCommerce 中,我想將我的交貨時間過濾器應用于單個產品,但前提是它是available/in stock. 在檢查產品庫存時出現任何錯誤。
function filter_woocommerce_germanized_delivery_time_html( $str_replace, $html ) {
global $product;
if( $product->is_in_stock() ) {
echo '<p >';
echo $str_replace;
echo '</p>';
echo '<p ><a href="https://xyz.at/info/lieferzeiten/" target="_blank"><i ></i> EU-Lieferzeiten</a></p></span>';
}
}
// add the filter
add_filter( 'woocommerce_germanized_delivery_time_html', 'filter_woocommerce_germanized_delivery_time_html', 10, 2 );
我也試過:
if($product->get_stock_quantity()>0)
但類似的錯誤如:
“未捕獲的錯誤:在 null 上呼叫成員函式 is_in_stock() ..”
謝謝您的幫助!
問候, 菲利克斯
uj5u.com熱心網友回復:
錯誤告訴你你在一個null值上使用了你的函式,這意味著它找不到$product變數。
不確定此woocommerce_germanized_delivery_time_html過濾器掛鉤的來源以及您使用的位置global $product,但您可以使用以下代碼段來獲取產品。
global $post;
$product = wc_get_product($post->ID);
現在你的整個代碼將是這樣的:
add_filter( 'woocommerce_germanized_delivery_time_html', 'filter_woocommerce_germanized_delivery_time_html', 10, 2 );
function filter_woocommerce_germanized_delivery_time_html($str_replace, $html)
{
global $post;
$product = wc_get_product($post->ID);
if ($product->is_in_stock()) {
echo '<p >';
echo $str_replace;
echo '</p>';
echo '<p >';
echo '<a href="https://xyz.at/info/lieferzeiten/" target="_blank"><i ></i> EU-Lieferzeiten</a>';
echo '</p>';
} else{
return $str_replace;
}
}
或者您可以像這樣獲得庫存數量:
add_filter( 'woocommerce_germanized_delivery_time_html', 'filter_woocommerce_germanized_delivery_time_html', 10, 2 );
function filter_woocommerce_germanized_delivery_time_html($str_replace, $html)
{
global $post;
$stock_quantity = get_post_meta($post->ID, '_stock', true);
if ($stock_quantity > 0) {
echo '<p >';
echo $str_replace;
echo '</p>';
echo '<p >';
echo '<a href="https://xyz.at/info/lieferzeiten/" target="_blank"><i ></i> EU-Lieferzeiten</a>';
echo '</p>';
} else {
return $str_replace;
}
}
讓我知道你是否可以讓它作業!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/323239.html
標籤:php WordPress的 求购 woocommerce-主题
上一篇:WordPress主題目錄下的editor-style.css檔案是什么?
下一篇:如何定位SVG?
