Post by fguzels » Tue Oct 20, 2020 9:52 pm

There is a module I can promote on my website and I want to make some revisions on this module. I did some of it but I had a problem.

In normal use of the module: When x product is added to my cart, it applies a% discount to product y. I want to add x product to my cart. And if the second x item is added to the cart, only the second item should receive up to a% discount. The third product should not be discounted, but the fourth product.

In the module, when I apply a discount for multiple items, it does not check whether the product is the same product. It is sufficient to have any two of the discounted products in the cart.

For example; The products I discount are x, y, z. I require a 50% discount when there are at least 2 items in the cart.

Module: There is x, y in the cart. It applies 50%.

I want: There is x, y in the cart. There will be no discount. The cart has 2x, 1y. Discounts will only be applied to x. There are 2x and 2y. It will apply a discount to the second x and the second y.

I hope I could explain. Below is the required code block in the module.

My Opencart Version: 3.0.3.2

Code: Select all

$promo1 = $promo['condition_min_quantity'];
    $promo2 = $promo['condition_product_ids'];
    $promo3 = $promo['discount_quantity'];
    $promo4 = array_intersect_assoc($promo['condition_product_ids'], $promo['discount_product_ids']);
    $promo5 = $promo['discount_value'];

    // Block early
    if (!$promo1 || !$promo2 || !$promo3 || !$promo4 || !$promo5) {
        return $promo;
    }

    $products     = $this->_cart['products'];
    $p_conditions = array(  // Product conditions in cart
        'products'       => array(),
        'total_quantity' => 0,
        'condition_qty'  => '2',
        'base_multiply'  => 0,
        'qty_off'        => 0,
    );
    $p_discounts  = array(); // Product discounts in cart
    $p_discounts_total = 0; // total quantity of free product in cart

    foreach ($products as $product) {

        if (in_array($product['product_id'], $promo['condition_product_ids'])) {
            $p_conditions['products'][] = '<a href="' . $this->url->link('product/product', 'product_id=' . (int)$product['product_id'], true) . '">' . $product['name'] . '</a>';
            $p_conditions['total_quantity'] += $product['quantity'];
            $p_conditions['base_multiply']  = floor($p_conditions['total_quantity'] / $p_conditions['condition_qty']);
            if ($p_conditions['base_multiply']) {
                // Note: qty_off indicate the quantity of products that get discounts off
                $p_conditions['qty_off'] = ($promo['apply_once'] ? 1 : $p_conditions['base_multiply']) * 1;
            }
        }

        if (in_array($product['product_id'], $promo['discount_product_ids'])) {
            $p_discounts[$product['product_id']] = array(
                'product_id' => $product['product_id'],
                'name'       => $product['name'],
                'quantity'   => isset($p_discounts[$product['product_id']]['quantity']) ? $p_discounts[$product['product_id']]['quantity'] + $product['quantity'] : $product['quantity'],
                'prices'     => isset($p_discounts[$product['product_id']]['prices']) ? $p_discounts[$product['product_id']]['prices'] + array($product['islpr_cart_id'] => $product['price']) : array($product['islpr_cart_id'] => $product['price']),
                'tax_class_id' => $product['tax_class_id'],
            );
            $p_discounts[$product['product_id']]['least_price'] = min($p_discounts[$product['product_id']]['prices']);
            $p_discounts[$product['product_id']]['most_price']  = max($p_discounts[$product['product_id']]['prices']);

            $p_discounts_total = $p_discounts_total + $product['quantity'];
        }
    }

Newbie

Posts

Joined
Sat Oct 17, 2020 2:35 am

Post by straightlight » Sun Nov 15, 2020 12:26 am

While there might be better ways to code this, if you can't find an extension specific to your needs on the Marketplace, you could always create a new service request in the Commercial Support section of the forum, or contact an Opencart partner by using the Resources link at the top of the site, or contact me directly via PM to get this done as a custom job.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 63 guests