Woocommerce, как удалить кодовое поле купона программно - PullRequest
0 голосов
/ 02 марта 2020

Интересно, можете ли вы помочь? Я пытаюсь заставить это работать. Я хочу показывать купон только в том случае, если цена превышает 19,99 И это не код продукта 455821 Я не могу заставить это работать. Не могли бы вы дать мне какие-нибудь идеи, почему?

Спасибо, Кевин

// START Remove coupon box when price is less than 19.99
add_filter( 'woocommerce_coupons_enabled','hide_coupon_field_on_cart' );
function hide_coupon_field_on_cart( $enabled ) {
    global $product;
//  $id = $product->get_id();
    $product_id = 455821;

    $price = "19.95";
    $cart = WC()->cart->get_cart();
        foreach ( $cart as $id => $cart_item ) {
        //  $prodid = $cart_item[ 'data' ]->$product->get_id();
        if( ($cart_item[ 'data' ]->get_price() <= $price) || ( $cart_item[ 'data' ]->$product->get_id() != $product_id ) ) {
            return false; // dont remove it 
        }
    }   


    return $enabled;
}

1 Ответ

1 голос
/ 02 марта 2020
// START Remove coupon box when price is less than 19.99
function hide_coupon_field_on_cart( $enabled ) {
    // settings
    $not_product_id = 455821;
    $min_price = 19.99;

    // Get cart object
    $cart = WC()->cart->get_cart();

    foreach ( $cart as $cart_item ) {
        // product id
        $product_id = $cart_item['product_id'];

        // Price
        $price = $cart_item['data']->get_price();

        if ( $product_id == $not_product_id && $price < $min_price ) {
            $enabled = false;
        }
    }

    // Cart total <= 0
    if ( WC()->cart->get_total() <= 0 ) {
        $enabled = false;
    }

    return $enabled;
}
add_filter( 'woocommerce_coupons_enabled','hide_coupon_field_on_cart' );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...