如果产品具有特定的属性名称Woocommerce,则添加附加价格

时间:2020-10-22 08:11:22

标签: php wordpress woocommerce cart woocommerce-rest-api

是否有任何方法可以根据使用的属性名称添加额外的价格?例如,所有产品 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>';
}
?>

0 个答案:

没有答案
相关问题