Skip to content

Instantly share code, notes, and snippets.

@felipe-pita
Last active June 6, 2019 15:30
Show Gist options
  • Save felipe-pita/48c31eccb5953fdcbfd65acbb2cf7e68 to your computer and use it in GitHub Desktop.
Save felipe-pita/48c31eccb5953fdcbfd65acbb2cf7e68 to your computer and use it in GitHub Desktop.
<?php
/**
* Adiciona desconto baseado nos produtos do carrinho
*/
function add_cart_fee_based_on_products($cart_object) {
global $woocommerce;
$fee_products = array(63, 48);
$fee_percent = 10;
$cart_products = array();
foreach ($cart_object->cart_contents as $key => $value) {
$cart_products[] = $value['product_id'];
}
$cart_has_fee_preducts = count(array_intersect($fee_products, $cart_products)) == count($fee_products);
if ($cart_has_fee_preducts) {
$fee_value = ($woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * ($fee_percent / 100) * -1;
$woocommerce->cart->add_fee('Promoção x (10% de desconto no carrinho)', $fee_value, true, 'standard');
}
}
add_action('woocommerce_cart_calculate_fees', 'add_cart_fee_based_on_products');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment