Last active
April 12, 2024 15:33
-
-
Save BBGuy/8da71901c0cdbf5d34a0ceff26517ea6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Working with commerce stock 8.x-1.x | |
// ****** Usfull commerce code. ****** | |
// Get the curent store and user | |
$currentStore = \Drupal::service('commerce_store.current_store')->getStore(); | |
$currentUser = \Drupal::currentUser(); | |
// Load the user object | |
$user = \Drupal\user\Entity\User::load($current_user->id()); | |
// Get a context | |
$context = new Context($user, $currentStore); | |
// Load an order by ID | |
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */ | |
$order = \Drupal::entityTypeManager() | |
->getStorage('commerce_order') | |
->load($order_id); | |
// Cycle order lie items. | |
foreach ($order->getItems() as $id => $order_item) { | |
} | |
// Check if an order item is associated with (has) a Purchasable Entity. | |
// Do not use if (!$order_item->hasPurchasedEntity()) { | |
$purchased_entity = $order_item->getPurchasedEntity; | |
if (!$purchased_entity) { | |
// Get it's name | |
$name = $purchased_entity->getTitle(); | |
} | |
// ****** Stock commands ****** | |
// Get the Stock service manager | |
/** @var \Drupal\commerce_stock\StockServiceManagerInterface $stockServiceManager */ | |
$stockServiceManager = \Drupal::service('commerce_stock.service_manager'); | |
// Get the stock service (from the manager) | |
/** @var \Drupal\commerce_stock\StockServiceInterface $stockService */ | |
$stock_service = $stockServiceManager->getService($entity); | |
$stock_service_name = $stockServiceManager->getService($entity)->getName(); | |
// Get the stock checker (from the service) | |
$stock_checker = $stock_service->getStockChecker(); | |
// Get the stock updater (from the service) | |
$stock_updater = $stock_service->getStockUpdater(); | |
// Get the context from a purchasable entity using the stock trait | |
use Drupal\commerce_stock\ContextCreatorTrait; | |
class X .. { | |
use ContextCreatorTrait; | |
..... | |
$context = $this->getContext($entity); | |
// Get the list of locations for a purchasable entity. | |
$context = new Context($user, $currentStore); | |
$locations = $stockService->getConfiguration()->getAvailabilityLocations($context, $entity); | |
// Get information on all active locations | |
/** @var \Drupal\commerce_stock_local\StockLocationStorage $locationStorage */ | |
$locationStorage = \Drupal::entityTypeManager()->getStorage('commerce_stock_location'); | |
$locations = $locationStorage->loadEnabled($product_variation); | |
foreach ($locations as $location) { | |
$id = $location->getId(); | |
$type = $location->getType(); | |
$status = $location->status->value; | |
$name = $location->name->value; | |
$api_id = $location->field_external_api_id->value; // a custom field | |
} | |
// Common oparations | |
// Check stock level | |
$stockServiceManager = \Drupal::service('commerce_stock.service_manager'); | |
$stock = $stockServiceManager->getStockLevel($product_variation_entity); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\stock_demo_code; | |
use Drupal\commerce_stock\ContextCreatorTrait; | |
class StockUpdateDemo { | |
use ContextCreatorTrait; | |
public function setStockLevel($variation, $free_stock) { | |
/** @var \Drupal\commerce_stock\StockServiceManagerInterface $stockServiceManager */ | |
$stockServiceManager = \Drupal::service('commerce_stock.service_manager'); | |
// Get the current website stock level. | |
$stock_level = $stockServiceManager->getStockLevel($variation); | |
// If stock level changed. | |
if ($free_stock != $stock_level) { | |
// Calculate the transaction stock. | |
$t_stock = $free_stock - $stock_level; | |
// Get the location of the transaction. | |
$context = $this->getContext($variation); | |
$location = $stockServiceManager->getTransactionLocation($context, $variation, $t_stock); | |
// Determine the type of transaction. | |
if ($t_stock > 0) { | |
$transaction_type_id = StockTransactionsInterface::STOCK_IN; | |
} | |
else { | |
$transaction_type_id = StockTransactionsInterface::STOCK_OUT; | |
} | |
$metadata = [ | |
'data' => [ | |
'message' => 'set by updater_demo', | |
], | |
]; | |
// Create the transaction | |
$this->stockServiceManager->createTransaction($variation, $location->getId(), '', $t_stock, NULL, $currency_code = NULL, $transaction_type_id, $metadata); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment