簡體   English   中英

通過 WooCommerce 產品設置中的自定義復選框將 class 添加到 WooCommerce 頁面

[英]Add class to WooCommerce pages via custom checkbox in WooCommerce product settings

我對下面的代碼有疑問。 使用該代碼,我們可以 select 特定產品,並在商店檔案中賦予它們不同的風格。 這是有效的。

但是,我猜代碼中存在錯誤。

激活產品的復選框后,即使我取消選中該復選框,它也始終以新樣式顯示。 我假設我在get_post_meta object 上出錯了。

有人可以幫我嗎?

在常規產品設置中顯示復選框並根據復選框中的值添加 class 的代碼

// Add checkbox
function action_woocommerce_product_general_options_product_style_listing_data() {
    // Checkbox
    woocommerce_wp_checkbox( array( 
        'id'             => '_special_product_listing_style', // Required, it's the meta_key for storing the value (is checked or not)
        'label'          => __( 'Special style', 'woocommerce' ), // Text in the editor label
        'desc_tip'       => false, // true or false, show description directly or as tooltip
        'description'    => __( 'Promote a product by changing the style of the product card', 'woocommerce' ) // Provide something useful here
    ) );
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_general_options_product_style_listing_data', 10, 0 );
        
// Save Field
function action_woocommerce_product_general_options_product_style_listing_save( $product ) {
    // Isset, yes or no
    $checkbox = isset( $_POST['_special_product_listing_style'] ) ? 'yes' : 'no';

    // Update meta
    $product->update_meta_data( '_special_product_listing_style', $checkbox );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_product_general_options_product_style_listing_save', 10, 1 );

// Is_special style
function filter_woocommerce_post_class( $classes, $product ) {    
    if ( get_post_meta( $product->get_id(), '_special_product_listing_style', true ) ) {
        $classes[] = 'custom-product-listing-class';
    }
    return $classes;
}
add_filter( 'woocommerce_post_class', 'filter_woocommerce_post_class', 10, 2 );

CSS 對特定產品進行樣式設置:

/* Custom special product listing style */
li.sales-flash-overlay.woocommerce-text-align-left.woocommerce-image-align-center.do-quantity-buttons.product.type-product.post-10800.status-publish.first.instock.product_cat-crafty-beer-club.has-post-thumbnail.featured.sold-individually.taxable.shipping-taxable.product-type-variable.custom-product-listing-class {
    border: 2px solid;
    border-style: dashed;
}

代替

// Is_special style
function filter_woocommerce_post_class( $classes, $product ) {    
    if ( get_post_meta( $product->get_id(), '_special_product_listing_style', true ) ) {
        $classes[] = 'custom-product-listing-class';
    }
    return $classes;
}
add_filter( 'woocommerce_post_class', 'filter_woocommerce_post_class', 10, 2 );

// Is_special style
function filter_woocommerce_post_class( $classes, $product ) {
    // Get meta
    $spls = $product->get_meta( '_special_product_listing_style' );
    
    // Compare
    if ( $spls == 'yes' ) {
        $classes[] = 'custom-product-listing-class';
    }
    
    return $classes;
}
add_filter( 'woocommerce_post_class', 'filter_woocommerce_post_class', 10, 2 );

這應該足以讓您的代碼完全達到 function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM