在使用 WordPress Woocommerce 構建的網站上作業,如果產品頁面處于缺貨狀態,我正在嘗試更改產品頁面上的屬性背景顏色。
例如,尺碼 4 應該有不同的背景顏色,因為它缺貨并且可以延期交貨:

WPClever 的WooCommerce的WPC 變化色板用于屬性。
我在functions.php中實作以下代碼:
function is_on_backorder() {
if ( 'onbackorder' === $this->get_stock_status() ) {
?>
<script>
( function( $ ) {
'use strict';
$( '.wpcvs-term span' ).css("background-color","red");
;
} ( jQuery ) );
</script>
<?php }
}
這是 HTML 的樣子:
<div class="wpcvs-terms wpcvs-type-button wpcvs-style-rounded" data-attribute="pa_size">
<span class="wpcvs-term wpcvs-enabled wpcvs-selected" aria-label="US 4" title="US 4" data-term="us-4">
<span>US 4</span>
</span>
<span class="wpcvs-term wpcvs-enabled" aria-label="US 4.5" title="US 4.5" data-term="us-4-5">
<span>US 4.5</span>
</span>
<span class="wpcvs-term wpcvs-enabled" aria-label="US 5" title="US 5" data-term="us-5">
<span>US 5</span>
</span>
<span class="wpcvs-term wpcvs-enabled" aria-label="US 5.5" title="US 5.5" data-term="us-5-5">
<span>US 5.5</span>
</span>
<span class="wpcvs-term wpcvs-enabled" aria-label="US 6" title="US 6" data-term="us-6">
<span>US 6</span>
</span>
<span class="wpcvs-term wpcvs-enabled" aria-label="US 6.5" title="US 6.5" data-term="us-6-5">
<span>US 6.5</span>
</span>
<span class="wpcvs-term wpcvs-enabled" aria-label="US 7" title="US 7" data-term="us-7">
<span>US 7</span>
</span>
<span class="wpcvs-term wpcvs-enabled" aria-label="US 7.5" title="US 7.5" data-term="us-7-5">
<span>US 7.5</span>
</span>
<span class="wpcvs-term wpcvs-enabled" aria-label="US 8" title="US 8" data-term="us-8">
<span>US 8</span>
</span>
</div>
這段代碼有什么問題?我應該添加一個動作嗎?如何?
uj5u.com熱心網友回復:
首先,您必須覆寫該變體開關的 HTML。為此,您可以使用woocommerce_dropdown_variation_attribute_options_html過濾器鉤子。
但是您必須洗掉WPC Variation Swatches插件的默認過濾器掛鉤。
下面是幫助您洗掉類中定義的默認過濾器的函式。感謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/375143.html
標籤:php WordPress的 求购
上一篇:按元框值自定義帖子型別查詢
