Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Multiple fixes for configurable products and add tests. #158

Open
wants to merge 6 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/acm/config/install/acm.connector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ product_publish: false
api_version: v2
filter_root_category: false
text_format: rich_text
delete_disabled_skus: true
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing config fields and schema was failing tests, haven't added update hook specifically for this one as I'm setting default values here, let me know if update hook is required (note: they are already editable via admin form)

product_title_use_sku: false
6 changes: 6 additions & 0 deletions modules/acm/config/schema/acm.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ acm.connector:
category_field_name:
type: string
label: 'category_field_name'
delete_disabled_skus:
type: boolean
label: 'delete_disabled_skus'
product_title_use_sku:
type: boolean
label: 'product_title_use_sku'

acm.session:
type: config_object
Expand Down
14 changes: 14 additions & 0 deletions modules/acm_cart/src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,18 @@ public function get($property_name) {
return NULL;
}

/**
* {@inheritdoc}
*/
public function hasItem(string $sku) {
$items = $this->items();

if (empty($items)) {
return FALSE;
}

$skus = array_column($items, 'sku');
return in_array($sku, $skus);
}

}
11 changes: 11 additions & 0 deletions modules/acm_cart/src/CartInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,15 @@ public function setGuestCartEmail($email);
*/
public function get($property_name);

/**
* Check if Cart has an sku.
*
* @param string $sku
* SKU to check.
*
* @return bool
* TRUE if sku is already in cart.
*/
public function hasItem(string $sku);

}
11 changes: 11 additions & 0 deletions modules/acm_cart/src/CartStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,4 +871,15 @@ public function clearPayment() {
unset($this->cart->payment);
}

/**
* {@inheritdoc}
*/
public function hasItem(string $sku) {
if ($this->isEmpty()) {
return FALSE;
}

return $this->cart->hasItem($sku);
}

}
36 changes: 36 additions & 0 deletions modules/acm_sku/acm_sku.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Hooks specific to the acm_sku module.
*/

use Drupal\acm_sku\Entity\SKU;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\TermInterface;

Expand Down Expand Up @@ -69,6 +70,41 @@ function hook_acm_sku_commerce_category_alter(TermInterface $term, array $catego

}

/**
* Alter the children of configurable products.
*
* @param array $children
* Variants for the SKU.
* @param \Drupal\acm_sku\Entity\SKU $sku
* Parent sku which is being added to cart.
*/
function hook_acm_sku_configurable_variants_alter(array &$children, SKU $sku) {

}

/**
* Alter the configurations for configurable product.
*
* @param array $configurations
* Configurations available for configurable product.
* @param \Drupal\acm_sku\Entity\SKU $sku
* Parent sku which is being added to cart.
*/
function hook_acm_sku_configurable_product_configurations_alter(array &$configurations, SKU $sku) {

}
/**
* Alter the options added to cart item.
*
* @param array $options
* Options to be added to cart item.
* @param \Drupal\acm_sku\Entity\SKU $sku
* Parent sku which is being added to cart.
*/
function hook_acm_sku_configurable_cart_options_alter(array &$options, SKU $sku) {

}

/**
* @} End of "addtogroup hooks".
*/
34 changes: 34 additions & 0 deletions modules/acm_sku/acm_sku.install
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,37 @@ function acm_sku_update_8109() {
$field->setTranslatable(FALSE);
$field->save();
}

/**
* Implements hook_update_N().
*
* Re-structure attribute weights to make it schema compatible.
*/
function acm_sku_update_8110() {
$config = \Drupal::configFactory()->getEditable('acm_sku.configurable_form_settings');

$attribute_weights = [];
foreach ($config->get('attribute_weights') ?? [] as $attribute_set => $weights) {
$fields = [];

foreach ($weights as $field => $weight) {
if (is_array($weight)) {
// Ignore corrupt data created because of buggy code.
continue;
}

$fields[] = [
'field' => $field,
'weight' => $weight,
];
}

$attribute_weights[] = [
'attribute_set' => $attribute_set,
'fields' => $fields,
];
}

$config->set('attribute_weights', $attribute_weights);
$config->save(TRUE);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
attribute_weights:
default:
size: 1
-
attribute_set: default
fields:
-
field: 'size'
weight: 1
show_quantity: TRUE
18 changes: 15 additions & 3 deletions modules/acm_sku/config/schema/acm_sku.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,21 @@ acm_sku.configurable_form_settings:
sequence:
type: mapping
mapping:
size:
label: 'Size'
type: integer
attribute_set:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this schema change was required to make sure tests work fine for dynamically added fields

Schema errors for acm_sku.configurable_form_settings with the following errors: acm_sku.configurable_form_settings:attribute_weights.default.color missing schema

label: 'attribute_set'
type: string
fields:
label: 'fields'
type: sequence
sequence:
type: mapping
mapping:
field:
label: 'field'
type: 'string'
weight:
label: 'weight'
type: integer
show_quantity:
type: boolean
label: 'show_quantity'
Expand Down
35 changes: 33 additions & 2 deletions modules/acm_sku/src/CartFormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ public function __construct(ConfigFactoryInterface $config_factory) {
public function getConfigurableAttributeWeights($attribute_set = 'default') {
$attribute_set = strtolower($attribute_set);
$weights = $this->config->get('attribute_weights');
$set_weights = $weights[$attribute_set] ?? $weights['default'];

$processed_weights = [];
foreach ($weights as $weight) {
foreach ($weight['fields'] ?? [] as $field) {
$processed_weights[$weight['attribute_set']][$field['field']] = $field['weight'];
}
}

$set_weights = $processed_weights[$attribute_set] ?? $processed_weights['default'];
asort($set_weights);
return $set_weights;
}
Expand All @@ -66,9 +74,32 @@ public function getConfigurableAttributeWeights($attribute_set = 'default') {
public function setConfigurableAttributeWeights($attribute_set = 'default', array $weights = []) {
$attribute_set = strtolower($attribute_set);

$fields = [];
foreach ($weights as $field => $weight) {
$fields[] = [
'field' => $field,
'weight' => $weight,
];
}

// Update weights for particular attribute set in config.
$existing_weights = $this->config->get('attribute_weights');
$existing_weights[$attribute_set] = $weights;

$exists = FALSE;
foreach ($existing_weights as &$weight) {
if ($weight['attribute_set'] == $attribute_set) {
$weight['fields'] = $fields;
$exists = TRUE;
break;
}
}

if (!$exists) {
$existing_weights[] = [
'attribute_set' => $attribute_set,
'fields' => $fields,
];
}

$config = $this->configFactory->getEditable(self::CONFIG_KEY);
$config->set('attribute_weights', $existing_weights);
Expand Down
Loading