Last active
June 24, 2019 15:41
-
-
Save felipe-pita/e981cf7b690699bd5ad2630e4a1c2f11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_before_cart', 'check_cart_product', 10, 1); | |
function check_cart_product($cart_item_data){ | |
global $woocommerce; | |
$restrict_products = array(42, 45); | |
$cart = WC()->cart; | |
$get_cart = WC()->cart->cart_contents; | |
$has_restrict_product = false; | |
$has_only_restrict_product = true; | |
$product_ids = array(); | |
if (!empty($get_cart)) { | |
// Verifica todos os itens do carrinho | |
foreach ($get_cart as $key => $value) { | |
$product_id = $value['product_id']; | |
$product_ids[] = $product_id; | |
// Verifica se o carrinho possui algum item de pré-venda | |
if (in_array($product_id, $restrict_products)) { | |
$has_restrict_product = true; | |
// Informa que o carrinho não pussui apenas produtos de pré-venda | |
} else { | |
$has_only_restrict_product = false; | |
} | |
} | |
$product_ids = array_unique($product_ids); | |
// Se o carinho possuir um item de pré-venda e outro comum ele esvazia o carrinho deixando apenas o de pré-venda | |
if ($has_restrict_product && !$has_only_restrict_product) { | |
$woocommerce->cart->empty_cart(); | |
foreach ($product_ids as $product_id) { | |
if (in_array($product_id, $restrict_products)) { | |
$cart_restrict_product = $product_id; | |
} | |
} | |
WC()->cart->add_to_cart($cart_restrict_product); | |
echo '<div class="woocommerce-notices-wrapper"><div class="woocommerce-message" role="alert">Produtos em pré-venda devem ser comprados exclusivamente.</div></div>'; | |
} | |
// Caso tente adicionar 2 produtos de pré-venda | |
if ($has_only_restrict_product && count($product_ids) !== 1) { | |
$woocommerce->cart->empty_cart(); | |
foreach ($product_ids as $product_id) { | |
if (in_array($product_id, $restrict_products)) { | |
$cart_restrict_product = $product_id; | |
} | |
} | |
WC()->cart->add_to_cart($cart_restrict_product); | |
echo '<div class="woocommerce-notices-wrapper"><div class="woocommerce-message" role="alert">Seu carrinho já possui um produto em pré-venda. Produtos em pré-venda devem ser comprados exclusivamente.</div></div>'; | |
} | |
return WC()->cart->cart_contents; | |
} | |
} | |
add_filter ( 'wc_add_to_cart_message_html', 'wc_add_to_cart_message_filter', 10, 2 ); | |
function wc_add_to_cart_message_filter($message, $product_id = null) { | |
global $woocommerce; | |
$restrict_products = array(42, 45); | |
$cart = WC()->cart; | |
$get_cart = WC()->cart->cart_contents; | |
$has_restrict_product = false; | |
$has_only_restrict_product = true; | |
if (!empty($get_cart)) { | |
foreach ($get_cart as $key => $value) { | |
$product_id = $value['product_id']; | |
$product_ids[] = $product_id; | |
// Verifica se o carrinho possui algum item de pré-venda | |
if (in_array($product_id, $restrict_products)) { | |
$has_restrict_product = true; | |
// Informa que o carrinho não pussui apenas produtos de pré-venda | |
} else { | |
$has_only_restrict_product = false; | |
} | |
} | |
$product_ids = array_unique($product_ids); | |
if ($has_restrict_product && !$has_only_restrict_product || $has_only_restrict_product && count($product_ids) !== 1) { | |
$message = 'Seu carrinho contém um produto de pré-venda. Os produtos de pré-venda devem ser comprados exclusivamente.'; | |
} | |
} | |
return $message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment