如果產品的庫存數量為零或更少,我正在嘗試禁用 WooCommerce 商店和存檔頁面上的“添加到購物車”按鈕。
我不想隱藏它,所以我想出了下面的代碼。
我的問題是代碼沒有向元素添加樣式,它替換了 html 樹中的整個按鈕代碼,因此根本不顯示按鈕。
關于如何克服這個問題的任何想法?
add_action( 'woocommerce_loop_add_to_cart_link', 'remove_price_from_variable_products', 9 );
function remove_price_from_variable_products() {
global $product;
if( $product->get_stock_quantity() <= 0 ) {
?>
<style>
.add_to_cart_button {
cursor: not-allowed !important;
}
</style>
<?php
add_action( 'woocommerce_after_shop_loop_item', 'custom_content_addtocart_button', 100 );
}
}
function custom_content_addtocart_button() {
echo '<br/><div >Contact Us for more information</div>';
}
uj5u.com熱心網友回復:
要從 WooCommerce 商店和存檔頁面上的現有添加到購物車按鈕添加/編輯/洗掉 CSS 類,您可以使用woocommerce_loop_add_to_cart_args過濾器掛鉤
所以你得到:
function action_woocommerce_loop_add_to_cart_args( $wp_parse_args, $product ) {
// Initialize
$custom_class = '';
// Not allowed
if ( $product->get_stock_quantity() <= 0 ) {
$custom_class = 'button-not-allowed';
}
// NOT empty
if ( ! empty ( $custom_class ) ) {
// Class
$wp_parse_args['class'] = implode(
' ',
array_filter(
array(
'button' . ' ' . $custom_class,
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
)
)
);
}
return $wp_parse_args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'action_woocommerce_loop_add_to_cart_args', 10, 2 );
注意:該$product->get_stock_quantity()功能最好與 結合使用$product->managing_stock(),但這取決于您的需要
然后應用以下CSS
.button-not-allowed {
cursor: not-allowed !important;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/409308.html
標籤:
