[英]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.