Jump to content

force customer group to ONLY ORDER articles.


tommiwtf

Recommended Posts

 

2 hours ago, ndiaga said:

Hi,

You  want  to  allow   only  certain  customers  to  add  products  to  cart   when out  of   stock  ?

I got two customer groups, normal and stockits. 

First, I needed the stockits to not update (decrease) the quantity, but I think make them able to ONLY ORDER (like, ask for a certain quantity of product when it's not available) would fix the issue. 
So basically I would like them to look at the products like they're all out of stocks and order a certain quantity so they don't impact availability for normal customers. 

Link to comment
Share on other sites

Just add the condition to PaymentModule.php

search // updates stock in shops and add condition for customer group.

 

find:

if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $update_stock_ === true) {
                        $product_list = $order->getProducts();
                        foreach ($product_list as $product) {
                            // if the available quantities depends on the physical stock
                            if (StockAvailable::dependsOnStock($product['product_id'])) {
                                // synchronizes
                                StockAvailable::synchronize($product['product_id'], $order->id_shop);
                            }
                        }
                    }

 

replace:

(change id_group == 3 to you group)

$customer = new Customer((int) $order->id);
                    if ($customer->id_group == 3) {$update_stock_ = true;} else {$update_stock_ = false;}
                    // updates stock in shops
                    if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && ($update_stock_ === true)) {
                        $product_list = $order->getProducts();
                        foreach ($product_list as $product) {
                            // if the available quantities depends on the physical stock
                            if (StockAvailable::dependsOnStock($product['product_id'])) {
                                // synchronizes
                                StockAvailable::synchronize($product['product_id'], $order->id_shop);
                            }
                        }
                    }

 

Edited by Guest (see edit history)
Link to comment
Share on other sites

1 hour ago, Guest said:

Just add the condition to PaymentModule.php

search // updates stock in shops and add condition for customer group.

 

find:


if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $update_stock_ === true) {
                        $product_list = $order->getProducts();
                        foreach ($product_list as $product) {
                            // if the available quantities depends on the physical stock
                            if (StockAvailable::dependsOnStock($product['product_id'])) {
                                // synchronizes
                                StockAvailable::synchronize($product['product_id'], $order->id_shop);
                            }
                        }
                    }

 

replace:

(change id_group == 3 to you group)


$customer = new Customer((int) $order->id);
                    if ($customer->id_group == 3) {$update_stock_ = true;} else {$update_stock_ = false;}
                    // updates stock in shops
                    if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && ($update_stock_ === true)) {
                        $product_list = $order->getProducts();
                        foreach ($product_list as $product) {
                            // if the available quantities depends on the physical stock
                            if (StockAvailable::dependsOnStock($product['product_id'])) {
                                // synchronizes
                                StockAvailable::synchronize($product['product_id'], $order->id_shop);
                            }
                        }
                    }

 

are you sure you get the correct id_group in this way and not 

$customer = new Customer(intval($cookie->id_customer));

?


ALSO: prestashop crashes when I try to complete the order with that code

Edited by tommiwtf (see edit history)
Link to comment
Share on other sites

Yes, you write it right.
I wrote the code quickly and did not check. I just gave a demonstration of how to solve it without a module.

$customer = new Customer((int) $order->id_customer);

Edited by Guest (see edit history)
Link to comment
Share on other sites

15 hours ago, Guest said:

Yes, you write it right.
I wrote the code quickly and did not check. I just gave a demonstration of how to solve it without a module.

$customer = new Customer((int) $order->id_customer);

Even like this, debugger results:

 

(1/1) ContextErrorException

Notice: Undefined property: Customer::$id_group

Link to comment
Share on other sites

4 minutes ago, Guest said:

$customer->id_default_group

obrazek.png.a048c970bf47661534964aad1887338e.png

thank you, you're actually very helpful but I tried to set "update_stock_" always false and I noticed in the backend that product quantity is still updating, oddly. 

I mean, according to code $update_stock_ is always false but when I confirm the order, the product still gets a -1 in quantity. 
 

Link to comment
Share on other sites

Now I don't have much time to investigate.
Approx. in three hours I'll look at it and test with me.
Then I put the complete code here.

Link to comment
Share on other sites

30 minutes ago, Guest said:

Now I don't have much time to investigate.
Approx. in three hours I'll look at it and test with me.
Then I put the complete code here.

also I think It should happens more in "stockavailable::updatequantity" but the function doesn't reach order or customer informations

Link to comment
Share on other sites

Hi,

change .classes/order/OrderDetail.php or create override

1. find function

protected function checkProductStock($product, $id_order_state)

2. replace afunction and change $customer->id_default_group !== '4'

protected function checkProductStock($product, $id_order_state)
    {
        $order = new Order($this->id_order);
        $customer = new Customer((int) $order->id_customer);

        if ($id_order_state != Configuration::get('PS_OS_CANCELED') && $id_order_state != Configuration::get('PS_OS_ERROR') && $customer->id_default_group !== '4'){
  
            if (!StockAvailable::dependsOnStock($product['id_product'])) {
                $update_quantity = StockAvailable::updateQuantity($product['id_product'], $product['id_product_attribute'], -(int) $product['cart_quantity']);
            }
            
            if ($update_quantity) {
                 $product['stock_quantity'] -= $product['cart_quantity'];
            }

            if ($product['stock_quantity'] < 0 && Configuration::get('PS_STOCK_MANAGEMENT')) {
                $this->outOfStock = true;
            }
            Product::updateDefaultAttribute($product['id_product']);
        }
      
    }

 

Edited by Guest (see edit history)
Link to comment
Share on other sites

9 minutes ago, Guest said:

Hi,

change .classes/order/OrderDetail.php or create override

1. find function


protected function checkProductStock($product, $id_order_state)

2. replace afunction and change $customer->id_default_group !== '4'


protected function checkProductStock($product, $id_order_state)
    {
        $order = new Order($this->id_order);
        $customer = new Customer((int) $order->id_customer);

        if ($id_order_state != Configuration::get('PS_OS_CANCELED') && $id_order_state != Configuration::get('PS_OS_ERROR') && $customer->id_default_group !== '4'){
  
            if (!StockAvailable::dependsOnStock($product['id_product'])) {
                $update_quantity = StockAvailable::updateQuantity($product['id_product'], $product['id_product_attribute'], -(int) $product['cart_quantity']);
            }
            
            if ($update_quantity) {
                 $product['stock_quantity'] -= $product['cart_quantity'];
            }

            if ($product['stock_quantity'] < 0 && Configuration::get('PS_STOCK_MANAGEMENT')) {
                $this->outOfStock = true;
            }
            Product::updateDefaultAttribute($product['id_product']);
        }
      
    }

 

this looks much better. I actually was thinkering and I had to edit various files to solve the fact that when you cancel an order, the stock comes back to the previous quantity. 

I'm going to try your code, because if it solves that by just editing this part, it will be much better. 

Link to comment
Share on other sites

I only tried when you placed an order.

The $current_state parameter can also be added.

eg.

($customer->id_default_group !== '4' && $id_order_state == 2)

Or something like that.
$ id_order_state == 2 is the new order status (order_conf).

So it's actually a condition:
when not defaul group = 4 and it's a new order,
so write down the change in quantity of products.

Link to comment
Share on other sites

Just now, Guest said:

I only tried when you placed an order.

The $current_state parameter can also be added.

eg.


($customer->id_default_group !== '4' && $id_order_state == 2)

Or something like that.
$ id_order_state == 2 is the new order status (order_conf).

So it's actually a condition:
when not defaul group = 4 and it's a new order,
so write down the change in quantity of products.

You actually helped me a lot because I finally came to a result lol

But I kinda took what you wrote just as inspiration because what I do is kinda different:


 I passed the parameter "$order" everytime the function "StockAvailable::updateQuantity" was called in the code and then I did what you did INSIDE the "unpdateQuantity" function and now looks like it's working even when I delete the order.

Thank you! 

Link to comment
Share on other sites

1 minute ago, Guest said:

I gladly helped.
I could write a module for that, but I have little free time.

I'm not that good at writing modules but let me know when you will lol

kinda interested in controlling that from the backend.
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...