简体   繁体   English

如果元素在 WP/WooCommerce 中有准确的文本,如何有条件地添加类名

[英]How to conditionally add classname if an element has exact text in WP/WooCommerce

I'm working on a webshop at the moment, and want to do some conditional rendering but I am unsure how to get it to work.我目前正在网上商店工作,并且想做一些条件渲染,但我不确定如何让它工作。 Right now I have a script that gives all products within the basket a classname matching the product ID.现在我有一个脚本,它为购物篮中的所有产品提供一个与产品 ID 匹配的类名。

Some of the products are variable products, and when chosen are included within the price of a main object.部分产品是可变产品,选择时包含在主 object 的价格内。 The item can also be bought individually, and therefore share the same product ID.该项目也可以单独购买,因此共享相同的产品 ID。 What I want to do is to add the class name only to the products which are included within the price.我想要做的是仅将 class 名称添加到价格中包含的产品中。

My thought was to do an if-statement to determine if the product has the text " Included in the price " as its product-total, and if so add the class name.我的想法是做一个 if 语句来确定产品是否将文本“包含在价格中”作为其产品总数,如果是,则添加 class 名称。 This if-statement is what is causing me grief.这个 if 语句是什么让我感到悲伤。 Here is the code that adds the class name to everything within the cart (and also the checkout page):这是将 class 名称添加到购物车(以及结帐页面)中的所有内容的代码:


Gives cart items a classname equal to the product ID

function so_42237701_cart_item_class( $class, $values, $values_key ) {

    if ( isset( $values[ 'product_id' ] ) ) {
        $class .= ' custom-' . $values[ 'product_id' ];
    }

    return $class;
}
add_filter( 'woocommerce_cart_item_class', 'so_42237701_cart_item_class', 10, 3 );

And for checkout:对于结帐:

function additional_class_to_order_item_classes ( $class, $values, $values_key ) {

    if ( isset( $values[ 'product_id' ] ) ) {
        $class .= ' custom-' . $values[ 'product_id' ];
    }

    return $class;
}
add_filter( 'woocommerce_order_item_class', 'additional_class_to_order_item_classes', 10, 3 );

How would I get around to make it conditional?我将如何解决它以使其有条件?

Variation products hold parent ID which is currently returning and also have variation ID which you can get from $values so your code should look like this变体产品包含当前正在返回的父 ID,并且还具有可以从 $values 获取的变体 ID,因此您的代码应如下所示

function so_42237701_cart_item_class( $class, $values, $values_key ) {
    //If we have variation ID its variable product
    if (!empty($values[ 'variation_id' ])) {
        $class .= ' custom-' . $values[ 'variation_id' ];
    } else {
        $class .= ' custom-' . $values[ 'product_id' ];
    }

    return $class;
}
add_filter( 'woocommerce_cart_item_class', 'so_42237701_cart_item_class', 10, 3 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 产品中每个产品 ID/WP 的价格后添加文本 - Add Text After the Price in WooCommerce Product Per Product ID/WP 以编程方式向Woocommerce 3有条件地添加折扣 - Add conditionally a discount programmatically to Woocommerce 3 如果Woocommerce订单商品具有特定的自定义元数据,请向电子邮件添加文本 - Add a text to emails if Woocommerce order item has a specific custom metadata 如果用户在 WooCommerce 中购买了特定产品,则更改“添加到购物车”文本 - Change “add to cart” text if a user has bought specific products in WooCommerce Woocommerce - 在自定义字段上有条件地禁用添加到购物车 - Woocommerce - Disable Add to Cart Conditionally on Custom Field 如何在Woocommerce中在价格之前添加自定义文本 - How to add custom text before price in Woocommerce 如何在 woocommerce 中的购物车数量旁边添加文本 - How to add text next to the quantity on the cart in woocommerce 如何在XPath中有条件地选择元素之后的文本()? - How to select the text() immediately following an element conditionally in XPath? 有条件地将关联元素添加到数组 - Conditionally add associative element to array 有条件地在数组中添加元素(...) - Conditionally add element inside an array(...)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM