我正在实施Drupal 2
我的用例
唱片发行等产品是以不同格式提供的产品,如LP、7英寸、CD。每种格式都有不同的运输成本。换句话说,不同的属性有不同的运输成本。
音乐专辑xxx
我们如何在Drupal商业运输中做到这一点?
我尝试使用“产品类别”分类法进行设置;但是,它只适用于产品级别,而不适用于产品属性。
发布于 2023-01-21 17:56:02
我不得不做一件类似的事情,并遇到了同样的问题。我在卖各种尺寸的印刷品。
每一张指纹都是一种产品。打印的每一个大小都是该产品内部的一个变化。根据印刷尺寸的不同,我想申请一定数量的运费。
最终,您需要添加一个自定义的送货条件。
What我做了
在Variation上,我添加了一个名为field_package_size的List (Text)字段。在这里,我把我的航运规模(小,中,大)。
然后,我创建了一个运输方法。但是正如你所看到的,从变化中获取信息是没有条件的。只是“产品类别”,它只涉及产品级别的分类法术语。
Create运输条件
商业文档有一个很好的创建条件的大纲:https://docs.drupalcommerce.org/commerce2/developer-guide/core/conditions#conditioninterface-and-conditionbase
我创建一个自定义模块来处理我的条件。除了基本的custom_module.info.yml文件外,所有模块都是一个文件。
custom_module/src/Plugin/Commerce/Condition/PackageSizeInOrder.php以下是整个文件:
'',
'exclude' => FALSE,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['package_size'] = [
'#type' => 'textfield',
'#title' => $this->t('Shipping format'),
'#description' => $this->t('Enter some shipping format to use as a condition for this offer. Separate with comma'),
'#default_value' => $this->configuration['package_size'],
'#required' => TRUE,
];
$form['exclude'] = [
'#type' => 'checkbox',
'#title' => $this->t('Exclusive condition'),
'#description' => $this->t('Check this box if the condition should be exclusive. IE the condition will pass if the order contains none of the package sizes listed.'),
'#default_value' => $this->configuration['exclude'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$values = $form_state->getValue($form['#parents']);
$this->configuration['package_size'] = $values['package_size'];
$this->configuration['exclude'] = $values['exclude'];
}
/**
* {@inheritdoc}
*/
public function evaluate(EntityInterface $entity) {
$exclude = (bool) $this->configuration['exclude'];
$this->assertEntity($entity);
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $entity;
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface $purchased_entity */
$items = $order->getItems();
$formats = preg_split('/\r\n|\r|\n/', $this->configuration['package_size']);
foreach ($items as $item) {
if (!$purchased_entity = $item->getPurchasedEntity()) {
continue;
}
if (!$purchased_entity || $purchased_entity->getEntityTypeId() !== 'commerce_product_variation') {
continue;
}
if (!method_exists($purchased_entity, 'getSku')) {
// Not sure what to do then.
continue;
}
$size = $purchased_entity->get('field_package_size')->getString();
foreach ($formats as $format) {
if (empty($format)) {
continue;
}
$config = explode(',', (string) $format);
foreach ($config as $config_format) {
$config_format = trim($config_format);
if (empty($config_format)) {
continue;
}
if (strpos($size, $config_format) === 0) {
return !$exclude;
}
}
}
}
return $exclude;
}
}我会把每个功能都做一遍。
defaultConfiguration()
在加载条件窗体时设置默认值。
buildConfigurationForm()
构建表单。对我来说,我真正需要的只是一个简单的字段,可以键入值的名称--小、中或大。
最初,我对我的产品变体有一个分类术语。理想情况下,这个字段将是这些术语的下拉列表,但是在分类法术语中加载超出了我的能力(这是可以做到的!我只是不知道该怎么做)。
这就是为什么我在产品变体中使用文本字段的原因。
submitConfigurationForm()
提交表格。
evaluate()
这将检查field_package_size上的值。如果您按照我的设置,您不需要在这里调整任何东西,除了您的字段的名称在您的产品变体上。
每一次运输费用的Create运输方法
在创建上面的文件之后,清除缓存,就会出现一个新的传送条件。
然后,为所拥有的每个方法创建一个船运方法。至于我的,我有小的、中等的和大的。
在每个方法上,我都添加了这个自定义条件,然后从field_package_size中填充值。下面是我的小型运输方法的示例。

<#>Combine产品一次装运
对于我的场景,如果有人购买了一个带有“小”和“中”标签的变体产品,那么我会用“中等”价格将这两种产品放在同一个包装/装运中。
您必须在船运方法中执行一些条件逻辑。我会把我的中小型和大型航运情况的截图,以防你遇到这样的麻烦:
小的

5~6成熟

大型

发布于 2021-06-04 06:25:27
嗯,如果你需要它坏,你将需要做你自己的自定义价格解析器-我所做的是在属性本身上放置一个价格字段-然后.
public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {
if ($entity->get('type')->getValue()[0]['target_id'] == 'something') {
$base = (float) $entity->price->number;
$code = $entity->price->currency_code;
$wrap = $entity->attribute_attrib_name->referencedEntities()[0];
$wrap_pice = (float) $wrap->field_price->getValue()[0]['number'];
$new_price = $base + $wrap_pice;
return new Price($new_price, $code);
}有关更多信息,请参见https://docs.drupalcommerce.org/commerce2/developer-guide/pricing/price-resolvers
https://drupal.stackexchange.com/questions/303315
复制相似问题