Created
January 4, 2019 15:18
-
-
Save manuelricci/982662545645d1ee306023c8c821935a 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 | |
function wc_promo_alert_product() | |
{ | |
global $woocommerce; | |
global $post; | |
$cart = $woocommerce->cart; | |
$category_id = array(14, 8, 38, 16); // Categorie alla quale viene applicata la promozione | |
$simple_flag = 0; | |
$variation_flag = 0; | |
if ($cart->is_empty()) { | |
if (has_term($category_id, 'product_cat', $post->ID)) { | |
echo create_promo_message($post->post_title, 'nothing'); | |
} | |
} else { | |
$items_in_cart = $cart->get_cart(); | |
$parent_products = array(); | |
foreach ($items_in_cart as $item_in_cart => $values) { | |
$_product_type = $values['data']->get_type(); | |
if ($_product_type === "simple") { | |
$_product_id = $values['data']->get_id(); | |
if (has_term($category_id, 'product_cat', $_product_id)) { | |
$quantity = $values['quantity']; | |
$post_title = get_post($_product_id); | |
$post_title = $post_title->post_title; | |
if ($quantity > 0 && $quantity == 1) { | |
$message = create_promo_message($post->post_title, 'almost'); | |
$simple_flag = 1; | |
} else { | |
$message = create_promo_message($post_title, 'done'); | |
$simple_flag = 2; | |
} | |
} | |
} elseif ($_product_type === "variation") { | |
$_product_id = wp_get_post_parent_id($values['data']->get_id()); | |
$parent_products[] = $_product_id; // Essendo un prodotto variabile devo controllare se il genitore è lo stesso di altri prodotti | |
if (has_term($category_id, 'product_cat', $_product_id)) { | |
$quantity = array_count_values($parent_products); | |
$quantity_variation = $values['quantity']; | |
// var_dump("Quantità variazioni " . $quantity[$_product_id]); | |
// var_dump("Quantità prodotti " . $quantity_variation); | |
$post_title = get_post($_product_id); | |
$post_title = $post_title->post_title; | |
if ($quantity_variation == 1 && $quantity[$_product_id] == 1) { | |
$message = create_promo_message($post->post_title, 'almost'); | |
$variation_flag = 1; | |
} elseif ($quantity_variation >= 1 && $quantity[$_product_id] >= 1) { | |
$message = create_promo_message($post_title, 'done'); | |
$variation_flag = 2; | |
} | |
} | |
} | |
if ($simple_flag >= 1 && $variation_flag >= 1) { | |
$message = create_promo_message($post_title, 'done'); | |
} | |
} | |
echo $message; | |
} | |
} | |
add_action('woocommerce_before_add_to_cart_button', 'wc_promo_alert_product', 2); | |
function wc_promo_alert_cart() | |
{ | |
global $woocommerce; | |
global $post; | |
$cart = $woocommerce->cart; | |
$category_id = array(14, 8, 38, 16); // Categorie alla quale viene applicata la promozione | |
$simple_flag = 0; | |
$variation_flag = 0; | |
$items_in_cart = $cart->get_cart(); | |
$parent_products = array(); | |
foreach ($items_in_cart as $item_in_cart => $values) { | |
$_product_type = $values['data']->get_type(); | |
if ($_product_type === "simple") { | |
$_product_id = $values['data']->get_id(); | |
if (has_term($category_id, 'product_cat', $_product_id)) { | |
$quantity = $values['quantity']; | |
$post_title = get_post($_product_id); | |
$post_title = $post_title->post_title; | |
if ($quantity > 0 && $quantity == 1) { | |
$message = create_promo_message($post_title, 'almost'); | |
$simple_flag = 1; | |
} else { | |
$message = create_promo_message($post_title, 'done'); | |
$simple_flag = 2; | |
} | |
} | |
} elseif ($_product_type === "variation") { | |
$_product_id = wp_get_post_parent_id($values['data']->get_id()); | |
$parent_products[] = $_product_id; // Essendo un prodotto variabile devo controllare se il genitore è lo stesso di altri prodotti | |
if (has_term($category_id, 'product_cat', $_product_id)) { | |
$quantity = array_count_values($parent_products); | |
$quantity_variation = $values['quantity']; | |
// var_dump("Quantità variazioni " . $quantity[$_product_id]); | |
// var_dump("Quantità prodotti " . $quantity_variation); | |
$post_title = get_post($_product_id); | |
$post_title = $post_title->post_title; | |
if ($quantity_variation == 1 && $quantity[$_product_id] == 1) { | |
$message = create_promo_message($post_title, 'almost'); | |
$variation_flag = 1; | |
} elseif ($quantity_variation >= 1 && $quantity[$_product_id] >= 1) { | |
$message = create_promo_message($post_title, 'done'); | |
$variation_flag = 2; | |
} | |
} | |
} | |
if ($simple_flag >= 1 && $variation_flag >= 1) { | |
$message = create_promo_message($post_title, 'done'); | |
} | |
} | |
echo $message; | |
} | |
add_action('woocommerce_after_cart_contents', 'wc_promo_alert_cart', 2); | |
add_action('woocommerce_before_checkout_form', 'wc_promo_alert_cart', 2); | |
function create_promo_message($current_product, $type) | |
{ | |
switch ($type) { | |
case 'nothing': | |
$promo_message = "<div class='promo-box nothing'>Approfitta della promo! Acquista due ${current_product} per ricevere uno sconto del 20%</div>"; | |
break; | |
case 'almost': | |
$promo_message = "<div class='promo-box almost'>Ti manca poco, acquista 1 ${current_product} per ricevere uno sconto del 20%</div>"; | |
break; | |
case 'done': | |
$promo_message = "<div class='promo-box done'>Fantastico! Hai abbastanza prodotti per usufruire della promo. Abbiamo già applicato lo sconto nel tuo carrello.</div>"; | |
remove_action('woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10); | |
break; | |
} | |
return $promo_message; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment