Commit 8cddbf2d authored by Gorodkov Denis's avatar Gorodkov Denis

commerce

parent 1e886dda
...@@ -7,27 +7,28 @@ use Drupal\Core\Form\FormStateInterface; ...@@ -7,27 +7,28 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\batch\Batch\ImportSwapi; use Drupal\batch\Batch\ImportSwapi;
class BatchForm extends FormBase { class BatchForm extends FormBase {
public function getFormId() { public function getFormId() {
return 'batch_form'; return 'batch_form';
} }
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
$form['date'] = array( $form['date'] = array(
'#type' => 'datetime', '#type' => 'datetime',
'#title' => 'Date and time', '#title' => 'Date and time',
); );
$form['actions']['submit'] = array( $form['actions']['submit'] = array(
'#type' => 'submit', '#type' => 'submit',
'#value' => 'Import', '#value' => 'Import',
'#button_type' => 'primary', '#button_type' => 'primary',
); );
return $form; return $form;
} }
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$batch = new ImportSwapi(); $batch = new ImportSwapi();
$batch->getOperations(); $batch->getOperations();
$batch->setBatch(); $batch->setBatch();
} }
} }
name: Context definition
description: Custom task
package: Tasks
type: module
core: 8.x
core_version_requirement: ^8 || ^9
<?php
namespace Drupal\context_definiton\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides
*
* @Block(
* id = "test_context_definition",
* admin_label = @Translation("Test block with context_definition"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node")),
* "term" = @ContextDefinition("entity:taxonomy_term", required = FALSE, label = @Translation("Term")),
* }
* )
*/
class ContextDefinition extends BlockBase {
public function build() {
$node = $this->getContextValue('node');
$term = $this->getContextValue('term');
$build = ['#markup' => 'Hello, From mymodule/Block'];
if (!empty($node) or !empty($term)) {
empty($node) ? $entiy = $term : $entiy = $node;
empty($node) ? $entiy_type_id = 'taxonomy_term' : $entiy_type_id = 'node';
$build = \Drupal::entityTypeManager()->getViewBuilder($entiy_type_id)->view($entiy, 'teaser');
$tags = $entiy->getCacheTags();
foreach ($tags as $tag) {
$build['#cache']['tags'][] = $tag;
}
}
return $build;
}
}
...@@ -22,9 +22,13 @@ ...@@ -22,9 +22,13 @@
console.log(data); console.log(data);
var content = '<div class="cart">'; var content = '<div class="cart">';
data['items'].forEach(function (item) { data['items'].forEach(function (item) {
img_attr = 'width="60" height="60" src="' + src + item['uri'] +'"'; src_item = "";
if (item['uri']) {
src_item = src + item['uri'];
}
img_attr = 'width="60" height="60" src="' + src_item +'"';
img = '<div class="item-img-container"><img ' + img_attr + '></div>'; img = '<div class="item-img-container"><img ' + img_attr + '></div>';
content = content + '<div class="card">' content = content + '<div class="card">';
content = content + '<div class="item-img-container">' + img + '</div>'; content = content + '<div class="item-img-container">' + img + '</div>';
content = content + '<div class="item-name">Наименование товара: ' + item['name'] + '</div>'; content = content + '<div class="item-name">Наименование товара: ' + item['name'] + '</div>';
content = content + '<div class="item-count">Количество: ' + Math.round(item['quantity']) + '</div>'; content = content + '<div class="item-count">Количество: ' + Math.round(item['quantity']) + '</div>';
......
...@@ -27,13 +27,15 @@ class CustomNormalizer extends NormalizerBase { ...@@ -27,13 +27,15 @@ class CustomNormalizer extends NormalizerBase {
if (!empty($referens_arr['product_id'])) { if (!empty($referens_arr['product_id'])) {
$product = $referens->getProduct(); $product = $referens->getProduct();
if ($product->hasField('field_node')) { if ($product->hasField('field_node') && !empty($product->get('field_node')->getValue())) {
$node = $product->get('field_node')->referencedEntities(); $node = $product->get('field_node')->referencedEntities();
$node = $node[0]; $node = $node[0];
$media = $node->get('field_media')->referencedEntities(); if ($node->hasField('field_media') && !empty($node->get('field_media')->getValue())) {
$media = $media[0]; $media = $node->get('field_media')->referencedEntities();
$uri = $media->get('field_media_image')->entity->getFileUri(); $media = $media[0];
$uri = explode('//', $uri); $uri = $media->get('field_media_image')->entity->getFileUri();
$uri = explode('//', $uri);
}
} }
} }
} }
...@@ -45,6 +47,7 @@ class CustomNormalizer extends NormalizerBase { ...@@ -45,6 +47,7 @@ class CustomNormalizer extends NormalizerBase {
'item_total_price' => $price, 'item_total_price' => $price,
'uri' => $uri[1], 'uri' => $uri[1],
]; ];
unset($uri);
} }
$data = [ $data = [
......
...@@ -20,57 +20,24 @@ use Drupal\multivalue_form_element\Element\MultiValue; ...@@ -20,57 +20,24 @@ use Drupal\multivalue_form_element\Element\MultiValue;
*/ */
class CustomPromotion extends OrderPromotionOfferBase { class CustomPromotion extends OrderPromotionOfferBase {
public function getIdsFromForm($form_state) {
$info = $form_state->getUserInput();
$items = $info['offer'][0]["target_plugin_configuration"]["custom_commerce_custom_promotion"]["items"][0][0];
#$values = $form_state->getValue($form['items']);
foreach ($items as $item) {
if (!empty($item)) {
$item_id = explode('(', $item);
$item_id = end($item_id);
$item_id = explode(')', $item_id);
$id = $item_id[0];
$ids[] = $id;
}
}
return $ids;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state); $form = parent::buildConfigurationForm($form, $form_state);
$ids = $this->getIdsFromForm($form_state);
/*if (!empty($ids)) {
foreach ($ids as $id_product_item) {
$default_value = \Drupal::entityTypeManager()->getStorage('commerce_product')->load($id_product_item);
$items[] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'commerce_product',
'#title' => 'Выберите товар',
'#default_value' => !empty($default_value) ? $default_value : '',
];
}
} else {
$items[] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'commerce_product',
'#title' => 'Выберите товар',
];
}*/
$items[] = [ $default_first_item = \Drupal::entityTypeManager()->getStorage('commerce_product')->load($this->configuration['first_item']);
$default_second_item = \Drupal::entityTypeManager()->getStorage('commerce_product')->load($this->configuration['second_item']);
$form['first_item'] = [
'#type' => 'entity_autocomplete', '#type' => 'entity_autocomplete',
'#target_type' => 'commerce_product', '#target_type' => 'commerce_product',
'#title' => 'Выберите товар', '#title' => 'Выберите первый продукт',
'#default_value' => !empty($default_first_item) ? $default_first_item : '',
]; ];
$form['items'] = [ $form['second_item'] = [
'#type' => 'multivalue', '#type' => 'entity_autocomplete',
'#title' => $this->t('Items'), '#target_type' => 'commerce_product',
'#cardinality' => MultiValue::CARDINALITY_UNLIMITED, '#title' => 'Выберите второй продукт',
'item' => [ '#default_value' => !empty($default_second_item) ? $default_second_item : '',
'#type' => 'entity_autocomplete',
'#target_type' => 'commerce_product',
'#title' => 'Выберите товар',
],
]; ];
return $form; return $form;
...@@ -78,13 +45,15 @@ class CustomPromotion extends OrderPromotionOfferBase { ...@@ -78,13 +45,15 @@ class CustomPromotion extends OrderPromotionOfferBase {
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state->getErrors()) { if (!$form_state->getErrors()) {
$this->configuration['items'] = $this->getIdsFromForm($form_state); $values = $form_state->getValue($form['#parents']);
$this->configuration['first_item'] = $values['first_item'];
$this->configuration['second_item'] = $values['second_item'];
} }
} }
public function apply(EntityInterface $entity, PromotionInterface $promotion) { public function apply(EntityInterface $entity, PromotionInterface $promotion) {
$this->assertEntity($entity); $this->assertEntity($entity);
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ /** @var \Drupal\commerce_order\Entity\Order $order */
$order = $entity; $order = $entity;
$first_item = $this->configuration['first_item']; $first_item = $this->configuration['first_item'];
$second_item = $this->configuration['second_item']; $second_item = $this->configuration['second_item'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment