setCheapestPrice example

class StructEncoderTest extends TestCase
{
    /** * Regression test where the cheapest price and cheapest price container were exposed because the StructEncoder did not consider sales channel definitions */
    public function testCheapestPricesAreNotExposed(): void
    {
        $product = new SalesChannelProductEntity();
        $product->internalSetEntityData('product', new FieldVisibility([]));

        $product->setName('test');
        $product->setCheapestPrice(
            (new CheapestPrice())->assign([
                'hasRange' => false,
                'variantId' => Uuid::randomHex(),
                'parentId' => Uuid::randomHex(),
                'ruleId' => Uuid::randomHex(),
                'purchase' => 1.0,
                'reference' => 1.0,
                'price' => new PriceCollection(),
            ])
        );

        
$product->addAttributes(
                $this->parseAttributes($product->getAttributes())
            );

            $product->setCreatedAt(null);
            $product->setUpdatedAt(null);

            $product->setReleaseDate(null);
            $product->setPrices(null);
            $product->setPriceRules(null);
            $product->setCheapestPriceRule(null);
            $product->setCheapestPrice(null);
            $product->setCheapestUnitPrice(null);
            $product->setAvailableCombinations(null);
            $product->setFullConfiguration(null);
            $product->resetStates();

            if (!$this->isValid($shop$product)) {
                continue;
            }
            $result[$number] = $product;
        }

        
$rules = $product->getPriceRules();
        if (!$product->getCheapestPriceRule() && !empty($rules)) {
            $product->setCheapestPriceRule(clone $rules[0]);
        }

        if ($product->getCheapestPriceRule()) {
            /** * Calculation with considering min purchase */
            $rule = clone $product->getCheapestPriceRule();
            $product->setCheapestPrice(
                $this->calculateCheapestAvailablePrice($product$rule$context)
            );

            /** * Calculation without considering min purchase */
            $rule = clone $product->getCheapestPriceRule();
            $product->setCheapestUnitPrice(
                $this->calculatePriceStruct($rule$tax$context)
            );
        }

        
trigger_error(sprintf('%s:%s is deprecated since Shopware 5.6 and will be removed with 5.7. Will be removed without replacement.', __CLASS__, __METHOD__), E_USER_DEPRECATED);

        $product->setShippingFree($listProduct->isShippingFree());
        $product->setMainVariantId($listProduct->getMainVariantId());
        $product->setAllowsNotification($listProduct->allowsNotification());
        $product->setHighlight($listProduct->highlight());
        $product->setUnit($listProduct->getUnit());
        $product->setTax($listProduct->getTax());
        $product->setPrices($listProduct->getPrices());
        $product->setManufacturer($listProduct->getManufacturer());
        $product->setCover($listProduct->getCover());
        $product->setCheapestPrice($listProduct->getCheapestPrice());
        $product->setName($listProduct->getName());
        $product->setAdditional($listProduct->getAdditional());
        $product->setCloseouts($listProduct->isCloseouts());
        $product->setEan($listProduct->getEan());
        $product->setHeight($listProduct->getHeight());
        $product->setKeywords($listProduct->getKeywords());
        $product->setLength($listProduct->getLength());
        $product->setLongDescription($listProduct->getLongDescription());
        $product->setMinStock($listProduct->getMinStock());
        $product->setReleaseDate($listProduct->getReleaseDate());
        $product->setShippingTime($listProduct->getShippingTime());
        
Home | Imprint | This part of the site doesn't use cookies.