是否有任何方法可以根据使用的属性名称添加额外的价格?例如,所有产品 https://prnt.sc/v4dl0m属性名称为“全彩色烙印”的购物车总额中必须额外有$ 90。
我将这段代码作为入门指南,但看来我确实听不懂逻辑。
关于functions.php:
<?php
function is_attr_in_cart( $attribute_slug_term ){
$found = false; // Initializing
if( WC()->cart->is_empty() )
return $found; // Exit
else {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['variation_id'] > 0 ){
// Loop through product attributes values set for the variation
foreach( $cart_item['variation'] as $term_slug ){
// comparing attribute term value with current attribute value
if ( $term_slug === $attribute_slug_term ) {
$found = true;
break; // Stop current loop
}
}
}
if ($found) break; // Stop the first loop
}
return $found;
}
}
在购物车页面上(总是错误):
<?php
if( is_attr_in_cart( 'Imprint' ) ){
echo '"One Imprint" attribute value has been found in cart items<br>';
} else {
echo '"Full Color Imprint" <strong>NOT FOUND!!!</strong><br>';
}
?>