function getMainCategoryId($product_id)
{
    if (empty($product_id)) {
        return false;
    }
    $prod_cat = Product\Category\get($product_id, null, 1);
    return $prod_cat['category_id'];
}
function cw_apply_special_offer_discount(&$product)
{
    global $tables;
    cw_load('category');
    if ($product['taxed_price'] == 0) {
        return $product;
    }
    // Trick: let first time calculate products without offer to gather correct taxed_price
    $special_offers_apply =& cw_session_register("special_offers_apply");
    if (!empty($special_offers_apply['supply']) && $product['promotion_suite']['free_product'] != true) {
        $current_discount = array();
        $discounted_in_bundle = false;
        $discount_was_applied = false;
        $saved_price = $product['price'];
        // for product walk thru all bonuses
        foreach ($special_offers_apply['supply'] as $offer_id => $bonus) {
            if ($bonus['D']['apply'] != PS_APPLY_PRODS) {
                continue;
            }
            $offer_id = intval($offer_id);
            // Eliminate subindex for repeated offers, e.g. 10.2->10
            $is_bundle = cw_query_first_cell("SELECT pid FROM {$tables['ps_offers']} WHERE offer_id='{$offer_id}'") != 0;
            $is_valid_product = false;
            if (is_array($bonus['D']['products']) && in_array($product['product_id'], array_keys($bonus['D']['products']))) {
                $is_valid_product = true;
            } elseif (is_array($bonus['D']['categories'])) {
                foreach ($bonus['D']['categories'] as $cid => $qty) {
                    // Detect if product is from required category or its subcategories
                    $pcats = Product\Category\get($product['product_id']);
                    $parents = cw_category_get_path(array_column($pcats, 'category_id'));
                    $is_parent = in_array($cid, $parents);
                    if (!empty($is_parent)) {
                        $is_valid_product = true;
                        break;
                    }
                }
            }
            if (!$is_valid_product) {
                continue;
            }
            $_current_discount = price_format($bonus['D']['disctype'] == PS_DISCOUNT_TYPE_PERCENT ? $product['price'] * $bonus['D']['discount'] / 100 : $bonus['D']['discount']);
            if ($is_bundle) {
                if ($discounted_in_bundle) {
                    continue;
                }
                # Bundle discount can be applied only once
                # Apply bundle discount immediately to product price
                $product['price'] = max($product['price'] - $_current_discount, 0.0);
                $discounted_in_bundle = $_current_discount;
                $special_offers_apply['discount']['products'][$product['product_id']][] = array('discount_type' => $bonus['D']['disctype'], 'discount' => $bonus['D']['discount'], 'max_discount' => $_current_discount);
            } elseif ($_current_discount > $current_discount['max_discount']) {
                # hey, we've found better regular discount
                $current_discount = array('discount_type' => $bonus['D']['disctype'], 'discount' => $bonus['D']['discount'], 'max_discount' => $_current_discount);
                $special_offers_apply['discount']['products'][$product['product_id']][] = $current_discount;
            }
            $discount_was_applied = true;
        }
        if ($discount_was_applied) {
            $product['promotion_suite']['saved_price'] = $saved_price;
            $product['promotion_suite']['saved_taxed_price'] = $product['taxed_price'];
            $product['price'] = max($product['price'] - $current_discount['max_discount'], 0.0);
        }
    }
    return $product;
}
 // Possible bug - $_affected_product_ids contains products from previous offers too?
 if ($current_bonuses[PS_FREE_SHIP]['apply'] == PS_APPLY_COND) {
     foreach ($_affected_product_ids['ids'] as $pid => $qty) {
         $special_offers_apply['free_shipping']['products'][$pid] += $qty - $affected_product_ids['ids'][$pid];
     }
 }
 // If free shipping applicable to selected products
 if ($current_bonuses[PS_FREE_SHIP]['apply'] == PS_APPLY_PRODS) {
     // Check all free-shipping bonuses of the offer
     foreach ($current_bonuses[PS_FREE_SHIP]['products'] as $pid => $qty) {
         $special_offers_apply['free_shipping']['products'][$pid] += $qty;
     }
     foreach ($current_bonuses[PS_FREE_SHIP]['categories'] as $cid => $qty) {
         foreach ($cart['products'] as $kk => $vv) {
             // Detect if product is from required category or its subcategories
             $pcats = Product\Category\get($vv['product_id']);
             $parents = cw_category_get_path(array_column($pcats, 'category_id'));
             $is_parent = in_array($cid, $parents);
             if ($is_parent) {
                 $special_offers_apply['free_shipping']['products'][$vv['product_id']] += min($vv['amount'], $qty);
                 $qty -= min($vv['amount'], $qty);
             }
         }
     }
 }
 /*
  * END: Prepare common free_shipping array
  */
 /*
  * Prepare common free_products array
  */