通过WooCommerce产品设置中的自定义复选框将类添加到WooCommerce页面

尼克7

我的以下代码有问题。使用该代码,我们可以选择特定的产品,并在商店档案中为它们提供不同的样式。可以了

但是,我猜代码中有错误。

激活产品复选框后,即使我取消选中该复选框,它也始终以新样式显示。我假设我对该get_post_meta对象犯了一个错误

有人可以帮我吗?

代码以显示常规产品设置中的复选框并根据复选框中的值添加类

// 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;
}
7uc1f3r

更换

// 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 );

这足以使您的代码充分发挥作用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过WooCommerce产品设置中的自定义复选框禁用添加到购物车按钮

根据自定义“ woocommerce_variation_option”将复选框添加到WooCommerce结帐页面

在Woocommerce结帐页面中添加到购物车中的产品的复选框字段

向WooCommerce产品变体选项中添加自定义复选框

自定义复选框,在WooCommerce购物车页面中添加费用

如果产品在WooCommerce中缺货,则将自定义类添加到链接

在 Woocommerce 的前端显示自定义单选和复选框产品设置数据

将复选框添加到WooCommerce产品高级选项选项卡

在将产品添加到购物车之前,需要Woocommerce复选框

Woocommerce结帐:将产品添加到购物车的复选框

将产品添加到Woocommerce单个产品页面上的购物车后,激活自定义按钮

Woocommerce中管理员编辑产品的复选框自定义字段

将产品自定义字段添加到WooCommerce中的“管理产品”批量编辑表单中

购物车页面Woocommerce中的自定义“条款和条件”接受复选框

将图像添加到自定义复选框

将复选框列添加到自定义QSortFilterProxyModel

如何将图像添加到自定义复选框的背面

在管理面板中向woocommerce quickedit添加自定义复选框

复选框字段,用于在WooCommerce中添加/删除自定义费用

在Woocommerce中将自定义多选字段添加到管理产品选项设置

将基于ACF字段的自定义“添加到购物车”按钮添加到WooCommerce单个产品页面

如何使用php代码将woocommerce产品添加到自定义Product_type?

将产品自定义字段添加到WooCommerce已完成的订单电子邮件中

Woocommerce后端产品编辑页面中的“向产品类型添加复选框”选项

WooCommerce 中特定产品类别的自定义条款和条件复选框

将自定义字段添加到woocommerce产品中的特定类别

WooCommerce删除我添加到产品简短描述中的自定义jQuery代码

在WooCommerce 3中将自定义列添加到管理产品列表

将可变产品的自定义属性添加到WooCommerce中的购物车