コード例 #1
0
ファイル: Bootstrap.php プロジェクト: nhp/shopware-4
    /**
     * This function updates the buyer protection article in the database
     * @throws Exception
     * @return Array | protection items
     */
	public function updateTrustedShopsProtectionItems()
	{
		$tsDataModel = new TrustedShopsDataModel();
		$TsProducts = $tsDataModel->getProtectionItems();
		foreach($TsProducts->item as $product) {
            $articleData = array(
                'name' => 'Käuferschutz',
                'active' => true,
                'tax' => 19,
                'supplier' => 'Trusted Shops',
                'mainDetail' => array(
                    'number' => $product->tsProductID,
                    'attribute' => array(
                        'attr19' => $product->protectedAmountDecimal,
                        'attr20' => $product->protectionDurationInt
                    ),
                    'prices' => array(
                        array(
                            'customerGroupKey' => 'EK',
                            'price' => $product->grossFee,
                        ),
                    )
                )
            );
            $articleResource = \Shopware\Components\Api\Manager::getResource('article');
            try {
                $articleDetailRepostiory = Shopware()->Models()->getRepository('Shopware\Models\Article\Detail');
                $articleDetailModel = $articleDetailRepostiory->findOneBy(array('number' => $articleData['mainDetail']['number']));
                if ($articleDetailModel) {
                    $articleModel = $articleDetailModel->getArticle();
                }
                if ($articleModel) {
                    $articleResource->update($articleModel->getId(), $articleData);
                } else  {
                    $articleResource->create($articleData);
                }
            } catch (\Shopware\Components\Api\Exception\ValidationException $ve) {
                $errors = array();
                /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
                foreach ($ve->getViolations() as $violation) {
                    $errors[] = sprintf(
                        '%s: %s',
                        $violation->getPropertyPath(),
                        $violation->getMessage()
                    );
                }
                throw new \Exception(implode(', ', $errors));
            }
		}

		return $TsProducts->item;
	}