Esempio n. 1
0
        $ajax->assign('force_redirection', fn_url('product_options.manage'));
    }
    //
    // Apply options to products
    //
} elseif ($mode == 'apply') {
    list($product_options, $search) = fn_get_product_global_options();
    Registry::get('view')->assign('product_options', $product_options);
    //
    // Update option
    //
} elseif ($mode == 'update') {
    $product_id = !empty($_REQUEST['product_id']) ? $_REQUEST['product_id'] : 0;
    $o_data = fn_get_product_option_data($_REQUEST['option_id'], $product_id);
    if (fn_allowed_for('ULTIMATE') && !empty($_REQUEST['product_id'])) {
        Registry::get('view')->assign('shared_product', fn_ult_is_shared_product($_REQUEST['product_id']));
        Registry::get('view')->assign('product_company_id', db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $_REQUEST['product_id']));
    }
    if (isset($_REQUEST['object'])) {
        Registry::get('view')->assign('object', $_REQUEST['object']);
    }
    Registry::get('view')->assign('option_data', $o_data);
    Registry::get('view')->assign('option_id', $_REQUEST['option_id']);
}
if (!fn_allowed_for('ULTIMATE:FREE')) {
    //
    // Product options exceptions
    //
    if ($mode == 'exceptions') {
        $exceptions = fn_get_product_exceptions($_REQUEST['product_id']);
        $product_options = fn_get_product_options($_REQUEST['product_id'], DESCR_SL, true);
Esempio n. 2
0
    Registry::set('navigation.tabs', array('detailed' => array('title' => __('general'), 'js' => true), 'images' => array('title' => __('images'), 'js' => true), 'seo' => array('title' => __('seo'), 'js' => true), 'qty_discounts' => array('title' => __('qty_discounts'), 'js' => true), 'addons' => array('title' => __('addons'), 'js' => true)));
    // [/Page sections]
    $product_data = fn_restore_post_data('product_data');
    Tygh::$app['view']->assign('product_data', $product_data);
    //
    // 'Multiple products addition' page
    //
} elseif ($mode == 'm_add') {
    //
    // 'product update' page
    //
} elseif ($mode == 'update') {
    $selected_section = empty($_REQUEST['selected_section']) ? 'detailed' : $_REQUEST['selected_section'];
    // Get current product data
    $skip_company_condition = false;
    if (fn_allowed_for('ULTIMATE') && fn_ult_is_shared_product($_REQUEST['product_id']) == 'Y') {
        $skip_company_condition = true;
    }
    $product_data = fn_get_product_data($_REQUEST['product_id'], $auth, DESCR_SL, '', true, true, true, true, false, false, $skip_company_condition);
    if (!empty($_REQUEST['deleted_subscription_id'])) {
        if (!Registry::get('runtime.company_id') || Registry::get('runtime.company_id') && $product_data['company_id'] == Registry::get('runtime.company_id')) {
            db_query("DELETE FROM ?:product_subscriptions WHERE subscription_id = ?i", $_REQUEST['deleted_subscription_id']);
        }
    }
    if (empty($product_data)) {
        return array(CONTROLLER_STATUS_NO_PAGE);
    }
    list($product_features, $features_search) = fn_get_paginated_product_features(array('product_id' => $product_data['product_id']), $auth, $product_data, DESCR_SL);
    Tygh::$app['view']->assign('product_features', $product_features);
    Tygh::$app['view']->assign('features_search', $features_search);
    $taxes = fn_get_taxes();
Esempio n. 3
0
/**
 * Apply promotion cart rule
 *
 * @param array $promotion promotion array
 * @param array $cart cart array
 * @param array $auth (optional) - auth array
 * @param array $cart_products (optional) - cart products array (for cart rules)
 * @return bool true if rule can be applied, false - otherwise
 */
function fn_promotion_apply_cart_rule($bonus, &$cart, &$auth, &$cart_products)
{
    // Clean bonuses
    if (!isset($cart['promotions'][$bonus['promotion_id']]['bonuses'])) {
        $cart['promotions'][$bonus['promotion_id']]['bonuses'] = array();
    }
    $bonus_id = count($cart['promotions'][$bonus['promotion_id']]['bonuses']);
    $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id] = $bonus;
    if ($bonus['bonus'] == 'order_discount') {
        if (floatval($cart['subtotal'])) {
            if (!isset($cart['subtotal_discount'])) {
                $cart['subtotal_discount'] = 0;
            }
            if (fn_allowed_for('MULTIVENDOR')) {
                $discount = fn_promotions_calculate_order_discount($bonus, $bonus_id, $cart);
            } else {
                $discount = fn_promotions_calculate_discount($bonus['discount_bonus'], $cart['subtotal'], $bonus['discount_value']);
            }
            if (floatval($discount)) {
                $cart['use_discount'] = true;
                $cart['subtotal_discount'] += fn_format_price($discount);
            }
        }
    } elseif ($bonus['bonus'] == 'discount_on_products') {
        foreach ($cart_products as $k => $v) {
            if (isset($v['exclude_from_calculate']) || !floatval($v['base_price']) && $v['base_price'] != 0) {
                continue;
            }
            if (fn_promotion_validate_attribute($v['product_id'], $bonus['value'], 'in') && !isset($cart['products'][$k]['extra']['promotions'][$bonus['promotion_id']])) {
                if (fn_promotion_apply_discount($bonus['promotion_id'], $bonus, $cart_products[$k], true, $cart, $cart_products)) {
                    $cart['use_discount'] = true;
                }
            }
        }
    } elseif ($bonus['bonus'] == 'discount_on_categories') {
        foreach ($cart_products as $k => $v) {
            if (isset($v['exclude_from_calculate']) || !floatval($v['base_price']) && $v['base_price'] != 0) {
                continue;
            }
            if (fn_promotion_validate_attribute($v['category_ids'], $bonus['value'], 'in') && !isset($cart['products'][$k]['extra']['promotions'][$bonus['promotion_id']])) {
                if (fn_promotion_apply_discount($bonus['promotion_id'], $bonus, $cart_products[$k], true, $cart, $cart_products)) {
                    $cart['use_discount'] = true;
                }
            }
        }
    } elseif ($bonus['bonus'] == 'give_usergroup') {
        $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id]['pending'] = true;
    } elseif ($bonus['bonus'] == 'give_coupon') {
        $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id]['pending'] = true;
        $cart['promotions'][$bonus['promotion_id']]['bonuses'][$bonus_id]['coupon_code'] = fn_generate_code('', COUPON_CODE_LENGTH);
    } elseif ($bonus['bonus'] == 'free_shipping') {
        $cart['free_shipping'][] = $bonus['value'];
    } elseif ($bonus['bonus'] == 'free_products') {
        foreach ($bonus['value'] as $p_data) {
            $product_data = array($p_data['product_id'] => array('amount' => $p_data['amount'], 'product_id' => $p_data['product_id'], 'extra' => array('exclude_from_calculate' => true, 'aoc' => empty($p_data['product_options']), 'saved_options_key' => $bonus['promotion_id'] . '_' . $p_data['product_id'])));
            if (!empty($cart['saved_product_options'][$bonus['promotion_id'] . '_' . $p_data['product_id']])) {
                $product_data[$p_data['product_id']]['product_options'] = $cart['saved_product_options'][$bonus['promotion_id'] . '_' . $p_data['product_id']];
            } elseif (!empty($p_data['product_options'])) {
                $product_data[$p_data['product_id']]['product_options'] = $p_data['product_options'];
            }
            // Restore object_id if needed
            if (!empty($cart['saved_object_ids'][$bonus['promotion_id'] . '_' . $p_data['product_id']])) {
                $product_data[$p_data['product_id']]['saved_object_id'] = $cart['saved_object_ids'][$bonus['promotion_id'] . '_' . $p_data['product_id']];
            }
            $existing_products = array_keys($cart['products']);
            if (!fn_allowed_for('ULTIMATE') || fn_allowed_for('ULTIMATE') && Registry::get('runtime.company_id') && (fn_check_company_id('products', 'product_id', $p_data['product_id'], Registry::get('runtime.company_id')) || fn_ult_is_shared_product($p_data['product_id'], Registry::get('runtime.company_id')) == 'Y')) {
                if ($ids = fn_add_product_to_cart($product_data, $cart, $auth)) {
                    $new_products = array_diff(array_keys($cart['products']), $existing_products);
                    if (!empty($new_products)) {
                        $hash = array_pop($new_products);
                    } else {
                        $hash = key($ids);
                    }
                    $_cproduct = fn_get_cart_product_data($hash, $cart['products'][$hash], true, $cart, $auth, !empty($new_products) ? 0 : $p_data['amount']);
                    if (!empty($_cproduct)) {
                        $cart_products[$hash] = $_cproduct;
                    }
                }
            }
        }
    }
    return true;
}
Esempio n. 4
0
        $ajax->assign('force_redirection', fn_url('product_options.manage'));
    }
    //
    // Apply options to products
    //
} elseif ($mode == 'apply') {
    list($product_options, $search) = fn_get_product_global_options();
    Tygh::$app['view']->assign('product_options', $product_options);
    //
    // Update option
    //
} elseif ($mode == 'update') {
    $product_id = !empty($_REQUEST['product_id']) ? $_REQUEST['product_id'] : 0;
    $o_data = fn_get_product_option_data($_REQUEST['option_id'], $product_id);
    if (fn_allowed_for('ULTIMATE') && !empty($_REQUEST['product_id'])) {
        Tygh::$app['view']->assign('shared_product', fn_ult_is_shared_product($_REQUEST['product_id']));
        Tygh::$app['view']->assign('product_company_id', db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $_REQUEST['product_id']));
    }
    if (isset($_REQUEST['object'])) {
        Tygh::$app['view']->assign('object', $_REQUEST['object']);
    }
    Tygh::$app['view']->assign('option_data', $o_data);
    Tygh::$app['view']->assign('option_id', $_REQUEST['option_id']);
}
if (!fn_allowed_for('ULTIMATE:FREE')) {
    //
    // Product options exceptions
    //
    if ($mode == 'exceptions') {
        $exceptions = fn_get_product_exceptions($_REQUEST['product_id']);
        $product_options = fn_get_product_options($_REQUEST['product_id'], DESCR_SL, true);
Esempio n. 5
0
/**
 * Updates product option
 *
 * @param array $option_data option data array
 * @param int $option_id option ID (empty if we're adding the option)
 * @param string $lang_code language code to add/update option for
 * @return int ID of the added/updated option
 */
function fn_update_product_option($option_data, $option_id = 0, $lang_code = DESCR_SL)
{
    /**
     * Changes parameters before update option data
     *
     * @param array  $option_data Option data
     * @param int    $option_id   Option identifier
     * @param string $lang_code   Two-letter language code (e.g. 'en', 'ru', etc.)
     */
    fn_set_hook('update_product_option_pre', $option_data, $option_id, $lang_code);
    // Add option
    if (empty($option_id)) {
        if (empty($option_data['product_id'])) {
            $option_data['product_id'] = 0;
        }
        $option_data['option_id'] = $option_id = db_query('INSERT INTO ?:product_options ?e', $option_data);
        foreach (fn_get_translation_languages() as $option_data['lang_code'] => $_v) {
            db_query("INSERT INTO ?:product_options_descriptions ?e", $option_data);
        }
        $create = true;
        // Update option
    } else {
        // if option inventory changed from Y to N, we should clear option combinations
        if (!empty($option_data['product_id']) && !empty($option_data['inventory']) && $option_data['inventory'] == 'N') {
            $condition = fn_get_company_condition('?:product_options.company_id');
            $old_option_inventory = db_get_field("SELECT inventory FROM ?:product_options WHERE option_id = ?i {$condition}", $option_id);
            if ($old_option_inventory == 'Y') {
                $inventory_filled = db_get_field('SELECT COUNT(*) FROM ?:product_options_inventory WHERE product_id = ?i', $option_data['product_id']);
                if ($inventory_filled) {
                    fn_delete_product_option_combinations($option_data['product_id']);
                }
            }
        }
        if (fn_allowed_for('ULTIMATE') && !empty($option_data['product_id']) && fn_ult_is_shared_product($option_data['product_id']) == 'Y') {
            $product_company_id = db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $option_data['product_id']);
            $option_id = fn_ult_update_shared_product_option($option_data, $option_id, Registry::ifGet('runtime.company_id', $product_company_id), $lang_code);
            if (Registry::get('runtime.company_id') && Registry::get('runtime.company_id') != $product_company_id) {
                $deleted_variants = array();
                fn_set_hook('update_product_option_post', $option_data, $option_id, $deleted_variants, $lang_code);
                return $option_id;
            }
        }
        db_query("UPDATE ?:product_options SET ?u WHERE option_id = ?i", $option_data, $option_id);
        db_query("UPDATE ?:product_options_descriptions SET ?u WHERE option_id = ?i AND lang_code = ?s", $option_data, $option_id, $lang_code);
    }
    if (fn_allowed_for('ULTIMATE')) {
        // options of shared product under the shared store hasn't a company_id. No necessary for updating.
        if (!empty($option_data['company_id'])) {
            fn_ult_update_share_object($option_id, 'product_options', $option_data['company_id']);
        }
        if (!empty($option_data['product_id'])) {
            fn_ult_share_product_option($option_id, $option_data['product_id']);
        }
    }
    if (!empty($option_data['variants'])) {
        $var_ids = array();
        // Generate special variants structure for checkbox (2 variants, 1 hidden)
        if ($option_data['option_type'] == 'C') {
            $option_data['variants'] = array_slice($option_data['variants'], 0, 1);
            // only 1 variant should be here
            reset($option_data['variants']);
            $_k = key($option_data['variants']);
            $option_data['variants'][$_k]['position'] = 1;
            // checked variant
            $v_id = db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE option_id = ?i AND position = 0", $option_id);
            $option_data['variants'][] = array('position' => 0, 'variant_id' => $v_id);
        }
        $variant_images = array();
        foreach ($option_data['variants'] as $k => $v) {
            if ((!isset($v['variant_name']) || $v['variant_name'] == '') && $option_data['option_type'] != 'C') {
                continue;
            }
            // Update product options variants
            if (isset($v['modifier'])) {
                $v['modifier'] = floatval($v['modifier']);
                if (floatval($v['modifier']) > 0) {
                    $v['modifier'] = '+' . $v['modifier'];
                }
            }
            if (isset($v['weight_modifier'])) {
                $v['weight_modifier'] = floatval($v['weight_modifier']);
                if (floatval($v['weight_modifier']) > 0) {
                    $v['weight_modifier'] = '+' . $v['weight_modifier'];
                }
            }
            //            if (isset($v['set_by_user'])) {
            //
            //            }
            $v['option_id'] = $option_id;
            if (empty($v['variant_id']) || !empty($v['variant_id']) && !db_get_field("SELECT variant_id FROM ?:product_option_variants WHERE variant_id = ?i", $v['variant_id'])) {
                $v['variant_id'] = db_query("INSERT INTO ?:product_option_variants ?e", $v);
                foreach (fn_get_translation_languages() as $v['lang_code'] => $_v) {
                    db_query("INSERT INTO ?:product_option_variants_descriptions ?e", $v);
                }
            } else {
                db_query("DELETE FROM ?:product_option_variants_disabled WHERE product_id=?i AND option_id =?i AND variant_id=?i", $option_data['extraProductId'], $option_id, $v['variant_id']);
                if ($v['status'] == 'Y') {
                    $vd = array('product_id' => $option_data['extraProductId'], 'option_id' => $option_id, 'variant_id' => $v['variant_id']);
                    db_query("INSERT INTO ?:product_option_variants_disabled ?e", $vd);
                    unset($v['status']);
                }
                db_query("UPDATE ?:product_option_variants SET ?u WHERE variant_id = ?i", $v, $v['variant_id']);
                db_query("UPDATE ?:product_option_variants_descriptions SET ?u WHERE variant_id = ?i AND lang_code = ?s", $v, $v['variant_id'], $lang_code);
            }
            $var_ids[] = $v['variant_id'];
            if ($option_data['option_type'] == 'C') {
                fn_delete_image_pairs($v['variant_id'], 'variant_image');
                // force deletion of variant image for "checkbox" option
            } else {
                $variant_images[$k] = $v['variant_id'];
            }
        }
        if ($option_data['option_type'] != 'C' && !empty($variant_images)) {
            fn_attach_image_pairs('variant_image', 'variant_image', 0, $lang_code, $variant_images);
        }
        // Delete obsolete variants
        $condition = !empty($var_ids) ? db_quote('AND variant_id NOT IN (?n)', $var_ids) : '';
        $deleted_variants = db_get_fields("SELECT variant_id FROM ?:product_option_variants WHERE option_id = ?i {$condition}", $option_id, $var_ids);
        if (!empty($deleted_variants)) {
            db_query("DELETE FROM ?:product_option_variants WHERE variant_id IN (?n)", $deleted_variants);
            db_query("DELETE FROM ?:product_option_variants_descriptions WHERE variant_id IN (?n)", $deleted_variants);
            foreach ($deleted_variants as $v_id) {
                fn_delete_image_pairs($v_id, 'variant_image');
            }
        }
    }
    if (!fn_allowed_for('ULTIMATE:FREE')) {
        // Rebuild exceptions
        if (!empty($create) && !empty($option_data['product_id'])) {
            fn_update_exceptions($option_data['product_id']);
        }
    }
    /**
     * Update product option (running after fn_update_product_option() function)
     *
     * @param array  $option_data      Array with option data
     * @param int    $option_id        Option identifier
     * @param array  $deleted_variants Array with deleted variants ids
     * @param string $lang_code        Language code to add/update option for
     */
    fn_set_hook('update_product_option_post', $option_data, $option_id, $deleted_variants, $lang_code);
    return $option_id;
}
Esempio n. 6
0
function fn_check_add_product_to_cart($cart, $product, $product_id)
{
    $result = true;
    /**
     * Change parmetres of checking if product can be added to cart (run before fn_check_add_product_to_cart func)
     *
     * @param array   $cart       Array of the cart contents and user information necessary for purchase
     * @param array   $product    Params with that product is adding to cart
     * @param int     $product_id Identifier of adding product
     * @param boolean $result     Flag determines if product can be added to cart
     */
    fn_set_hook('check_add_to_cart_pre', $cart, $product, $product_id, $result);
    if (fn_allowed_for('ULTIMATE,MULTIVENDOR')) {
        if (fn_allowed_for('ULTIMATE') && Registry::get('runtime.company_id') || fn_allowed_for('MULTIVENDOR') && isset($cart['company_id'])) {
            $product_company_id = db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $product_id);
        }
        if (fn_allowed_for('ULTIMATE') && Registry::get('runtime.company_id')) {
            if ($product_company_id != Registry::get('runtime.company_id') && fn_ult_is_shared_product($product_id, Registry::get('runtime.company_id')) != 'Y') {
                $result = false;
            }
        }
        if (fn_allowed_for('MULTIVENDOR') && isset($cart['company_id'])) {
            if ($product_company_id != $cart['company_id']) {
                $result = false;
            }
        }
    }
    /**
     * Change parmetres of checking if product can be added to cart (run before fn_check_add_product_to_cart func)
     *
     * @param array   $cart       Array of the cart contents and user information necessary for purchase
     * @param array   $product    Params with that product is adding to cart
     * @param int     $product_id Identifier of adding product
     * @param boolean $result     Flag determines if product can be added to cart
     */
    fn_set_hook('check_add_to_cart_post', $cart, $product, $product_id, $result);
    return $result;
}
/**
 * Updates product price for a storefront. Used on product import.
 *
 * @param integer $product_id Product ID
 * @param float $price Price
 * @param boolean $is_create True if the product has been created
 * @param string $store Comany name
 * @return boolean
 */
function fn_import_product_price($product_id, $price, $is_create, $store = '')
{
    if (fn_allowed_for('ULTIMATE')) {
        if (fn_ult_is_shared_product($product_id) == 'Y') {
            if (!($company_id = Registry::get('runtime.company_id'))) {
                $company_id = fn_get_company_id_by_name($store);
            }
            fn_update_product_prices($product_id, array('price' => $price, 'create' => $is_create), $company_id);
        }
    }
    fn_update_product_prices($product_id, array('price' => $price, 'create' => $is_create));
}
Esempio n. 8
0
function fn_ult_check_store_permission_product($table, $key, $key_id)
{
    return fn_check_company_id($table, $key, $key_id) || fn_ult_is_shared_product($key_id, Registry::get('runtime.company_id')) == 'Y';
}