Gekissimo.net - Opi ansaitsemaan rahaa webilläsi Internetissä!

Magento 2 API – toivelista [Custom API]

Seuraava artikkeli auttaa sinua: Magento 2 API – toivelista [Custom API]

käytä Magento\Framework\DataObject;

käytä Magento\Framework\Event\ManagerInterfacea;

käytä Magento\Framework\Exception\LocalizedException;

käytä Magento\Framework\Exception\NoSuchEntityException;

käytä Magento\Wishlist\Model\WishlistFactory;

käytä Meetanshi\CustomRestApi\Api\WishlistManagementInterface;class WishlistManagement toteuttaa WishlistManagementInterface

{

/**

* @var WishlistFactory

*/

yksityinen $toivelistaTehdas;

/**

* @var ProductRepositoryInterface

*/

yksityinen $productRepository;

/**

* @var ManagerInterface

*/

yksityinen $tapahtumanhallinta;

/**

* @var \Magento\Catalog\Helper\Product\Configuration

*/

yksityinen $productHelper;/**

* WishlistManagement rakentaja.

* @param WishlistFactory $wishlistFactory

* @param ProductRepositoryInterface $productRepository

* @param ManagerInterface $eventManager

* @param \Magento\Catalog\Helper\Product\Configuration $productHelper

*/

julkinen funktio __construct(

WishlistFactory $wishlistFactory,

ProductRepositoryInterface $productRepository,

ManagerInterface $eventManager,

\Magento\Catalog\Helper\Product\Configuration $productHelper

) {

$this->wishlistFactory = $wishlistFactory;

$this->productRepository = $productRepository;

$this->eventManager = $tapahtumanhallinta;

$this->productHelper = $productHelper;

}/**

* @param int $customerId

* @return \Magento\Wishlist\Model\Wishlist|\Meetanshi\CustomRestApi\Api\Data\WishlistInterface

* @heittää NoSuchEntityExceptionin

*/

julkinen funktio get(int $customerId)

{

$toivelista = $this->wishlistFactory->create()->loadByCustomerId($customerId);if (!$wishlist->getId()) {

$toivomuslista[‘items’] = [];

$toivomuslista[‘error’] = [‘Customer does not yet have a wishlist’];

return $wishlist;}$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$helper = $objectManager->get(‘Meetanshi\CustomRestApi\Helper\Data’);

$helperPool = $objectManager->get(‘Magento\Catalog\Helper\Product\ConfigurationPool’);$storeUrl = $helper->getStoreUrl();

$mediaUrl = $storeUrl . ‘/pub/media/catalog/product/’;

if ($helper->isRemovePub()) {

$mediaUrl = $storeUrl . ‘/media/catalog/product/’;

}

$product_img = [];

$productItems = [];

foreach ($wishlist->getItemCollection()->getItems() as $item) {

$tuotetunnus = $tuote->getProductId();

yrittää{

$tuote = $this->productRepository->getById($productId);

kytkin ($product->getTypeId()) {

case “bundle” : $helperInstance = \Magento\Bundle\Helper\Catalog\Product\Configuration::class; tauko;

case “ladattava” : $helperInstance = \Magento\Downloadable\Helper\Catalog\Product\Configuration::class; tauko;

oletus : $helperInstance = \Magento\Catalog\Helper\Product\Configuration::class;}

$productItems[] =

[

“wishlist_item_id” => $item->getId(),

“product_id” => $productId,

“name” => $product->getData(‘name’),

“price” => number_format($item->getProduct()->getFinalPrice() ?? 0, 2, ‘.’, ”),

“sku” => $product->getSku(),

“qty” => $item->getQty(),

“special_price” => number_format($item->getProduct()->getSpecialPrice() ?? 0, 2, ‘.’, ”),

“image” => $mediaUrl.$product->getData(‘image’),

“thumbnail” => $mediaUrl.$product->getData(‘thumbnail’),

“small_image” => $mediaUrl.$product->getData(‘small_image’),

“product_type” => $product->getTypeId(),

“product_options” => $this->getConfiguredOptions($item, $helperPool, $helperInstance)

];

}

saalis (\poikkeus $e) {

}

}$toivelista[‘items’] = array_merge($productItems, $product_img);return $toivomuslista;

}/**

* @param $item

* @param $helperPool

* @param $mainHelper

* @return sekoitettu

*/

julkinen toiminto getConfiguredOptions($item, $helperPool, $mainHelper)

{

$auttaja = $auttajaPool->get($mainHelper);

$optiot = $helper->getOptions($item);

foreach ($options as $index => $option) {

if (is_array($option) && array_key_exists(‘value’, $option)) {

if (!(array_key_exists(‘has_html’, $option) && $optio[‘has_html’] === totta)) {

if (is_array($optio[‘value’])) {

🔥 Empfohlen:  Kuinka luoda sisennys Canva-tekstilaatikkoon

foreach ($optio[‘value’] kuten $avain => $arvo) {

$optio[‘value’][$key] = $arvo;

}

}

}

$optiot[$index][‘value’] = $optio[‘value’];

}

}

palauttaa $optiot;

}/**

* @param int $customerId

* @param \Meetanshi\CustomRestApi\Api\Data\RequestInterface $item

* @return \Magento\Wishlist\Model\Wishlist|\Meetanshi\CustomRestApi\Api\Data\WishlistInterface

* @heittää LocalizedExceptionin

* @heittää NoSuchEntityExceptionin

*/

julkinen funktion lisäys(int $asiakastunnus, $item)

{

$toivelista = $this->wishlistFactory->luo()->loadByCustomerId($asiakastunnus, tosi);

$tuote = $this->productRepository->get($tuote->getProduct());

if (!$product->isVisibleInCatalog()) {

throw new LocalizedException(__(“Valitettavasti tätä kohdetta ei voi lisätä toivelistalle”), null, 1);

}

$buyRequest = new DataObject();

$customAttributes = $tuote->getCustomAttributes();

if ($customAttributes) {

$superAttributes = [];

$bundleOptionQtys = [];

$bundleOptions = [];

foreach ($customAttributes kuin $customAttribute) {

if (strpos($customAttribute->getAttributeCode(), ‘super_attribute_’) === 0) {

$superAttributeId = str_replace(‘super_attribute_’, ”, $customAttribute->getAttributeCode());

$superAttributes[$superAttributeId] = $customAttribute->getValue();

jatkaa;

}

if (strpos($customAttribute->getAttributeCode(), ‘bundle_option_qty_’) === 0) {

$bundleOptionQty = str_replace(‘bundle_option_qty_’, ”, $customAttribute->getAttributeCode());

$bundleOptionQtys[$bundleOptionQty] = $customAttribute->getValue();

jatkaa;

}

if (strpos($customAttribute->getAttributeCode(), ‘bundle_option_’) === 0) {

$bundleOption = str_replace(‘bundle_option_’, ”, $customAttribute->getAttributeCode());

$bundleOption = explode(‘_’, $bundleOption);

if (count($bundleOption) === 1) {

$bundleOptions[$bundleOption[0]]= $customAttribute->getValue();

} elseif (count($bundleOption) === 2) {

$bundleOptions[$bundleOption[0]][$bundleOption[1]]= $customAttribute->getValue();

}

jatkaa;

}

}

if ($superAttributes) {

$buyRequest->setData(‘superattribuutit’, $superAttributes);

}

if ($bundleOptionQtys) {

$buyRequest->setData(‘bundle_option_qty’, $bundleOptionQtys);

}

if ($bundleOptions) {

$buyRequest->setData(‘bundle_option’, $bundleOptions);

}

}

$tulos = $toivelista->lisääUusiTuote($tuote, $ostopyyntö);

if (on_string($tulos)) {

heittää uusi LocalizedException(__($tulos), null, 2);

}

if ($wishlist->isObjectNew()) {

$toivelista->tallenna();

}

$this->eventManager->lähetys(

‘wishlist_add_product’,

[‘wishlist’ => $wishlist, ‘product’ => $product, ‘item’ => $result] );

$toivomuslista[‘items’] = $toivelista->getItemCollection()->getItems();

palauttaa $toivomuslista;

}

/**

* @param int $customerId

* @param int $quoteId

* @param int $itemId

* @return \Meetanshi\CustomRestApi\Api\Data\WishlistInterface

*/

julkisen funktion siirto(int $asiakastunnus, int $quoteId, int $itemId)

{

$toivelista = $this->wishlistFactory->luo()->loadByCustomerId($asiakastunnus, tosi);

$buyRequest = new DataObject();

$om = \Magento\Framework\App\ObjectManager::getInstance();

$quote = $om->get(‘Magento\Quote\Model\Quote’);

$quoteItems = $lainaus->lataus($quoteId)->getAllVisibleItems();

$tila = false;

yrittää {

foreach ($quoteItems muodossa $quoteItem) {

$_productId = $quoteItem->getProductId();

$tuote = $this->productRepository->getById($_productId);

if (!$product->isVisibleInCatalog()) {

throw new LocalizedException(__(“Valitettavasti tätä kohdetta ei voi lisätä toivelistalle”), null, 1);

}

if ($quoteItem->getId() == $itemId) {

$buyRequest = $quoteItem->getBuyRequest();

$tila = tosi;

tauko;

}

}

$optiot = $buyRequest->getOptions();

if ($buyRequest->getOptions()) {

foreach ($buyRequest->getOptions() as $key => $option) {

if (is_array($optio)) {

if (isset($optio[‘date_internal’])) {

unset($options[$key]);

$optiot[$key] = $optio[‘date_internal’];

}

}

}

$buyRequest->setData(‘optiot’, $optiot);

}

if ($status) {

$tulos = $toivelista->lisääUusiTuote($tuote, $ostopyyntö);

if (on_string($tulos)) {

🔥 Empfohlen:  Kuinka näyttää Magento 2 -moduuliluettelo

heittää uusi LocalizedException(__($tulos), null, 2);

}

if ($wishlist->isObjectNew()) {

$toivelista->tallenna();

}

$this->eventManager->lähetys(

‘wishlist_add_product’,

[‘wishlist’ => $wishlist, ‘product’ => $product, ‘item’ => $result] );

$toivomuslista[‘items’] = $toivelista->getItemCollection()->getItems();

}

}

saalis (\poikkeus $e) {

heittää uusi LocalizedException(__($e->getMessage()), null, 1);

}

palauttaa $toivomuslista;

}

/**

* @param int $customerId

* @param int $itemId

* @return bool

* @heitot \Poikkeus

*/

julkinen funktio delete(int $asiakastunnus, int $itemId): bool

{

$toivomuslista = $this->wishlistFactory->create()->loadByCustomerId($customerId);

$tuote = $toivelista->getItem($itemId);

if (!$item) {

heittää uusi NoSuchEntityException(__(‘Ei kohdetta ID:llä %1’, $itemId), null, 1);

}

$tuote->poista();

palauttaa tosi;

}

}