Commit c30aab22 authored by Johnny Santos's avatar Johnny Santos Committed by Omkar Deshpande
Browse files

Issue #3285287 by Johnny Santos, gaurav.kapoor, WagnerMelo: Fix all the PHPCS issues in the module

parent d5820443
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -3,4 +3,5 @@
1. Install fresh Magento 2.3.x
2. Create a new Magento System Integration from System >> Integrations
3. Provide access to all the resources
4. Add the `Magento Site URL` and `Access Token` of the integration to `/admin/config/magento`
4. Add the `Magento Site URL` and `Access Token` of the 
integration to `/admin/config/magento`
+3 −2
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

/**
 * @file
 * Contains hook install function for Magento.
 */

/**
+18 −11
Original line number Diff line number Diff line
@@ -5,9 +5,10 @@
 * Contains hook_implementations of Magento module.
 */

use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\ProductType;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity\BundleFieldDefinition;
use Drupal\magento\Model\SearchCriteria;

@@ -30,7 +31,7 @@ function magento_cron() {
}

/**
 *
 * Function to create product types.
 */
function _create_product_types() {
  /** @var \Drupal\magento\Services\MagentoClient $magentoClient */
@@ -61,8 +62,8 @@ function _create_product_types() {
        commerce_product_add_body_field($productType);
        $attributes = $magentoClient->getProductAttributeSetAttributes($attributeSet['attribute_set_id']);
        foreach ($attributes as $index => $attribute) {
          // TODO: Create Images/media on the basis of 'frontend_input'.
          // TODO: Create select fields with allowed values.
          // @todo Create Images/media on the basis of 'frontend_input'.
          // @todo Create select fields with allowed values.
          if (!in_array($attribute['attribute_code'], $baseFields)) {
            $fieldType = $attribute['attribute_code'] !== 'category_ids'
              ? _get_field_type($attribute['backend_type'], $attribute['frontend_input']) : 'entity_reference';
@@ -71,7 +72,7 @@ function _create_product_types() {
              ->setTargetBundle($productType->id())
              ->setName($attribute['attribute_code'])
              ->setLabel($attribute['default_frontend_label'])
            // TODO: See if it is possible to get this from API.
            // @todo See if it is possible to get this from API.
              ->setCardinality(1)
              ->setRequired($attribute['is_required'])
              ->setTranslatable(TRUE)
@@ -151,7 +152,10 @@ function _get_field_type($backendType, $frontEndType) {
  return $drupalField;
}

function getDrupalProductId($sku) {
/**
 * Function to get drupal product id via sku.
 */
function get_drupal_product_id($sku) {
  $productId = -1;
  $query = \Drupal::database()->select('commerce_product_variation_field_data', 'cpvfd')
    ->condition('cpvfd.sku', $sku, '=')
@@ -161,23 +165,26 @@ function getDrupalProductId($sku) {
  foreach ($rows as $row) {
    $id = $row->variation_id;
  }
  $variation = \Drupal\commerce_product\Entity\ProductVariation::load($id);
  $variation = ProductVariation::load($id);
  if ($variation) {
    $productId = $variation->getProductId();
  }
  return $productId;
}

function fetchRelatedProducts($magentoClient, $product) {
/**
 * Function to fetch related products.
 */
function fetch_related_products($magentoClient, $product) {
  $defaultVariation = $product->getDefaultVariation();
  if ($defaultVariation) {
    $sku = $defaultVariation->getSku();
    $magentoProduct = $magentoClient->getProduct($sku);
    $productLinks = isset($magentoProduct['product_links']) ? $magentoProduct['product_links'] : [];
    $productLinks = $magentoProduct['product_links'] ?? [];
    foreach ($productLinks as $productLink) {
      if ($productLink['link_type'] === 'upsell') {
        $relatedProductId = getDrupalProductId($productLink['linked_product_sku']);
        $relatedProduct = \Drupal\commerce_product\Entity\Product::load($relatedProductId);
        $relatedProductId = get_drupal_product_id($productLink['linked_product_sku']);
        $relatedProduct = Product::load($relatedProductId);
        if ($relatedProduct) {
          $product->field_upsell_products->appendItem([
            'target_id' => $relatedProductId,
+83 −83
Original line number Diff line number Diff line
@@ -16,8 +16,7 @@ use Drupal\magento\Services\MagentoUtils;
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @link https://www.drupal.org/project/magento
 */
class ImportProduct extends DrushCommands
{
class ImportProduct extends DrushCommands {
  /**
   * Magento Client.
   *
@@ -53,11 +52,14 @@ class ImportProduct extends DrushCommands
    $this->magentoClient = $magentoClient;
    $this->magentoUtils = $magentoUtils;
  }

  /**
     * Import Magento Product Singly with entering SKU from magento
   * Import Magento Product Singly with entering SKU from magento.
   *
   * @param string $sku
   *   SKU informed from magento.
   * @param string $options
   *   Options arr will import product in the basis of array with spaces.
   *
   * @command magento:import
   * @aliases magento-import
@@ -68,11 +70,8 @@ class ImportProduct extends DrushCommands
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
     * 
     * @return string
   */
    public function importProduct($sku , $options = ['arr' => false])
    {
  public function importProduct($sku, $options = ['arr' => FALSE]) {
    if ($options['arr']) {
      try {
        $this->output->writeln('The Product is getting imported ....');
@@ -101,4 +100,5 @@ class ImportProduct extends DrushCommands
      }
    }
  }

}
+3 −3
Original line number Diff line number Diff line
@@ -37,9 +37,9 @@ class SearchCriteria {
   *   SearchCriteria data.
   */
  public function __construct(array $data) {
    $this->condition = isset($data['condition']) ? $data['condition'] : 'eq';
    $this->field = isset($data['field']) ? $data['field'] : '';
    $this->value = isset($data['value']) ? $data['value'] : '';
    $this->condition = $data['condition'] ?? 'eq';
    $this->field = $data['field'] ?? '';
    $this->value = $data['value'] ?? '';
  }

}
Loading