Jump to content

Funkcja zmiany cen dla produktów z kombinacjami


hakeryk2

Recommended Posts

Mam takie pytanie dla deweloperów, ostatnio musiałem napisać skrypt do zmiany cen produktów na podstawie kodu referencyjnego bądź też id_product i id_product_attribute i chciałbym zapytać czy może ktoś takiego już coś pisał i zwróci uwagę na co w sumie zwracać uwagę? Presta ma tyle zależności, że czasami można się walnąć a przy cenach bym tego nie chciał. 

Uwaga: skrypt był pisany dla produktów które mają kombinacje.

Wstawiam tutaj część kodu którą napisałem, dodałem warunki echo "" byście wiedzieli co i jak
 

public function updatePrices($id_product, $id_product_attribute = 0, $xml_brutto, $update = false, $decimal_places = 6)
    {
        if ($id_product && $id_product_attribute) {
            if ($xml_brutto <= 0)
                return false;

            $product = new Product((int)$id_product); // Using $id_product, true for full product is broken when without context
            if (!Validate::isLoadedObject($product))
                return false;

            $product_name = Product::getProductName($product->id, $id_product_attribute);
            $product->tax_rate = Tax::getProductTaxRate($product->id); // product

            $tax_rate = $product->tax_rate / 100 + 1; //return tax in ex. 1.23 format which is easier to use in math
            $xml_netto = round($xml_brutto / $tax_rate, $decimal_places);

            if (!$xml_netto){
                echo 'Something is wrong with netto price '.$product_name;
                return false;
            }

            if ($product->isDiscounted($product->id, 1, null, $id_product_attribute)){
                echo 'Product '.$product_name.' discounted, price wont be checked<br>';
                return false;
            }

            $combination = new Combination((int)$id_product_attribute);
            $combinations_quantity = Product::getProductCombinationsQuantity($product->id);

            $shop_brutto = round(($product->price + $combination->price) * $tax_rate); // needs to be rounded because values wont be equal
            $shop_netto = round($product->price + $combination->price, $decimal_places);

            if ($xml_brutto != $shop_brutto) {
                $price_difference = round($xml_netto - $shop_netto, $decimal_places);
                $compare = ($price_difference > 0) ? 'more expensive' : 'cheaper';
                
                echo 'Price for  '. $combination->reference .' is NOT the same.
                 In shop it is <strong>' . round($shop_netto * $tax_rate, 2) . '</strong> ('.$shop_netto.')
                 but in XML it is <strong>' . round($xml_netto * $tax_rate, 2) . '</strong> ('.$xml_netto.').
                 Product suppose to be '.$compare.' about <strong>'.round($price_difference*$tax_rate, 2).'</strong> PLN.<br>';
                if ($update && ($product->price + $combination->price) > 0) {
                    /* We need to check if product has price based on main price of product
                      or is combinations affecting price ny checking how many combinations it have */
                    if ($combinations_quantity <= 1) {
                        // just to prevent case when there was influence on price
                        // It is reset to 0 and set main price of product
                        if ($combination->price != 0) {
                            $combination->price = 0;
                            $combination->save();
                            echo 'Combination price returned to 0 due to only 1 or 0 combinations.<br>';
                        }

                        $product->price = $xml_netto;
                        $product->save();
                        echo 'Product '.$product_name.' saved with new price '.number_format($product->price*$tax_rate, 2).' <br>';
                        unset($product);
                    } else {
                        $combination->price = (float)($combination->price + $price_difference);
                        $combination->save();
                        echo 'Combination for '.$combination->reference.' was changed to '.number_format($combination->price*$tax_rate).'<br>';
                        unset($combination);
                    }
                }
            } else {
                echo 'Price for '.$combination->reference.' is OK<br>';
            }
        }
    }

I następnie wywoływana jest funkcja która synchronizuje ceny jeśli nagle wszystkie impact price są takie wiec ustawia główną cenę produktu na domyślną, a kombinacje spadają na zero. Przydatne gdy powyższa funkcja dla wszystkich np 6 kombinacji ustawiła impact na 6 zł, więc synchronizacja wyrówna główną cenę na +6 zł a impact poleci na 0.

 

    public function synchronizeCombinationsPricesWithProductPrice($id_product)
    {
        if ($id_product) {
            $product = new Product((int)$id_product);
            if ($product->isDiscounted($product->id))
                return false; // dont synchronize products with discounts

            $product_combinations = $product->getAttributeCombinations(0);
            if (count($product_combinations) > 1) {

                // holy shit that is lame but it works
                $combination_impact = $product_combinations[0]['price'];
                $same_impacts = false;

                if ($combination_impact != 0) {
                    foreach ($product_combinations as $attribute) {
                        if ($attribute['price'] == $combination_impact) {
                            $same_impacts = true;
                        } else {
                            $same_impacts = false;
                            break;
                        }
                    }
                }

                if ($same_impacts === true) {
                	if ($product->price + $combination_impact <= 0)
                		return false;

                    $product->price = ($product->price + $combination_impact);
                    $product->save();
                    foreach ($product_combinations as $attribute){
                        $combination = new Combination((int)$attribute['id_product_attribute']);
                        $combination->price = 0;
                        $combination->save();
                        unset($combination);
                    }
                    echo 'Synchronized prices for  '.$product->getProductName($product->id).'<br>';
                }
            }
        }
    }


Możecie się podzielić może Waszymi kodami?

 

Edited by hakeryk2 (see edit history)
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...