WooCommerce 和 WordPress 的條件標籤 (也可以使用 「WooCommerce 和 WordPress 條件邏輯」) 可以在您的 functions.php 中使用,以根據特定條件顯示內容。例如,您可以在單個 PHP 函式中顯示不同類別的不同內容。
您可以在這裡找到 WooCommerce 條件標籤列表和 WordPress 條件標籤。我不認為貼上它們是值得的,所以請使用這兩個連結作為參考 – 事實上,在這個部落格文章中,我想給您舉例,可能是您學習 WooCommerce 定製的最好方法。
您在 WooCommerce 單一產品頁面上工作嗎?
WooCommerce 中的單一產品頁面的好東西是 WordPress 知道他們是 「帖子」 。所以,您可以使用 is_single 。單個產品頁面的掛鉤列表可以在這裡找到。
PHP:僅在單個產品頁面上執行某些操作
add_action( 'woocommerce_before_main_content', 'bbloomer_single_product_pages' ); function bbloomer_single_product_pages() { if ( is_product() ) { echo 'Something'; } else { echo 'Something else'; } }
PHP:如果產品 ID = XYZ,請執行某些操作
add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_product_ID' ); function bbloomer_single_product_ID() { if ( is_single( '17' ) ) { echo 'Something'; } elseif ( is_single( '56' ) ) { echo 'Something else'; } }
PHP:如果產品屬於類別,請執行某些操作
add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_category_slug' ); function bbloomer_single_category_slug() { if ( has_term( 'chairs', 'product_cat' ) ) { echo 'Something'; } elseif ( has_term( 'tables', 'product_cat' ) ) { echo 'Something else'; } }
PHP:如果產品屬於標籤,請執行某些操作
add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_tag_slug' ); function bbloomer_single_tag_slug() { if ( has_term( 'blue', 'product_tag' ) ) { echo 'Something'; } elseif ( has_term( 'red', 'product_tag' ) ) { echo 'Something else'; } }
PHP:如果產品出售,做某事
add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_on_sale' ); function bbloomer_single_on_sale() { if ( $product->is_on_sale() ) { // do something } }
PHP:做一些事情,如果產品簡單,變數,外部…
add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_product_type' ); function bbloomer_single_product_type() { if( $product->is_type( 'simple' ) ){ // do something } elseif( $product->is_type( 'variable' ) ){ // do something } elseif( $product->is_type( 'external' ) ){ // do something } elseif( $product->is_type( 'grouped' ) ){ // do something } }
PHP:如果產品可下載,請執行某些操作
add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_downloadable' ); function bbloomer_single_downloadable() { if( $product->is_downloadable() ){ // do something } }
PHP:只在相關產品上做某事
相關產品由 「迴圈」 生成。有時您可能只想在單一產品頁面上使用 PHP(不包括相關的) 或反之亦然。
下面的程式碼段僅在單個產品頁面上隱藏,僅在相關產品部分。
add_filter( 'woocommerce_variable_price_html', 'bbloomer_remove_variation_price', 10, 2 ); function bbloomer_remove_variation_price( $price ) { global $woocommerce_loop; if ( is_product() && $woocommerce_loop['name'] == 'related' ) { $price = ''; } return $price; }
2. Are you working on the WooCommerce Shop/Category Page?
You can find all the shop/archive WooCommerce hooks here. Let』s see how to use conditional logic on these 「loop」 pages:
PHP: do something on the Shop page only
add_action( 'woocommerce_before_main_content', 'bbloomer_loop_shop' ); function bbloomer_loop_shop() { if ( is_shop() ) { echo 'This will show on the Shop page'; } else { echo 'This will show on all other Woo pages'; } }
PHP: do something on each product on the loop pages
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_loop_per_product' ); function bbloomer_loop_per_product() { if ( has_term( 'chairs', 'product_cat' ) ) { echo 'Great chairs!'; } elseif ( has_term( 'tables', 'product_cat' ) ) { echo 'Awesome tables!'; } }
PHP: do something on category pages only
add_action( 'woocommerce_before_main_content', 'bbloomer_loop_cat' ); function bbloomer_loop_cat() { if ( is_product_category() ) { echo 'This will show on every Cat pages'; } else { echo 'This will show on all other Woo pages'; } }
PHP: do something based on category name
add_action( 'woocommerce_before_main_content', 'bbloomer_loop_cat_slug' ); function bbloomer_loop_cat_slug() { if ( is_product_category( 'books' ) ) { echo 'This will show on the Books Cat page'; } elseif ( is_product_category( 'chairs' ) ) { echo 'This will show on the Chairs Cat page'; } }
PHP: do something on tag pages only
add_action( 'woocommerce_before_main_content', 'bbloomer_loop_tag' ); function bbloomer_loop_tag() { if ( is_product_tag() ) { echo 'This will show on every Cat pages'; } else { echo 'This will show on all other Woo pages'; } }
PHP: do something based on tag name
add_action( 'woocommerce_before_main_content', 'bbloomer_loop_tag_slug' ); function bbloomer_loop_tag_slug() { if ( is_product_tag( 'red' ) ) { echo 'This will show on the Red Tag page'; } elseif ( is_product_tag( 'yellow' ) ) { echo 'This will show on the Yellow Tag page'; } }
3. Are you working on WooCommerce Pages?
PHP: do something if on a WooCommerce page (excluding cart/checkout)
add_action( 'woocommerce_before_main_content', 'bbloomer_woo_page' ); function bbloomer_woo_page() { if ( is_woocommerce() ) { echo 'This will show on Woo pages'; } else { echo 'This will show on WP pages'; } }
PHP: do something if on Cart/Checkout
add_action( 'woocommerce_sidebar', 'bbloomer_cart_checkout' ); function bbloomer_cart_checkout() { if ( is_cart() ) { echo 'This will show on the Cart sidebar'; } elseif ( is_checkout() ) { echo 'This will show on the Checkout sidebar'; } }
4. Are you working on the WooCommerce Cart/Checkout?
PHP: do something if WooCommerce Cart/Checkout has Product ID
function bbloomer_find_id_in_cart() { global $woocommerce; foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) { $product = $values['data']; if ( $product->id == 123 ) { // do something } } }
如何新增此程式碼?
您可以將 PHP 程式碼片段放置在主題或子主題的 functions.php 檔案的底部 (如果是 CSS 程式碼,請新增到主題的 style.css 檔案底部),修改之前建議先備份原始檔案,若出現錯誤請先刪除此程式碼。
此程式碼是否可用?
如需幫助或是您有更好的方案想分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 使用者也希望您的參與。