iosystems/shipping-bundle

此包已被弃用且不再维护。没有建议的替代包。

Symfony 2 包,用于轻松处理运费。

安装: 761

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2014-05-14 06:42 UTC

This package is not auto-updated.

Last update: 2018-12-29 17:09:47 UTC


README

Symfony 2 包,用于轻松处理运费。此包不依赖于任何外部概念、类或接口(购物车、运费方式、产品等)并且易于自定义。

安装

在您的 composer.json 中添加 iosystems/shipping-bundle

{
    "require": {
        "iosystems/shipping-bundle": "dev-master"
    }
}

在内核中启用该包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new iOSystems\ShippingBundle\iOSystemsShippingBundle(),
    );
}

最后使用 composer update 更新您的供应商。

配置

为了正常工作,您必须至少配置一个内置重量计算器和一个确切的价格计算器。

重量计算器

此包利用了 重量计算器 的概念,即用于计算单个 可称重实例 重量的服务。

内置计算器包括

内置重量计算器默认是 禁用的。使用 ~trueenabled: true 启用它们(只要没有必需的参数),使用 falseenabled: false 禁用它们。

# iOSystemsShippingBundle Configuration
iosystems_shipping:
	weight_calculators:
		gross: ~
		dimensional:
			divider: 5000
	# ...

您可以轻松添加 自定义计算器

重量计算策略(如何使用重量计算器)

包的重量计算策略基于 支持的计算器中的最大重量。例如,如果 $itemGrossWeightableInterfaceDimensionalWeightable 的实例,则选择毛重和体积重量的最大值。总重量只是上述值的总和。

重量计算策略可以 替换

价格计算器

为了计算单个可称重项(或一组可称重项)的运费,包使用了一个 价格计算器。价格计算器是 PriceCalculatorInterface 的实例,用于计算一组可称重项的总价格。

fixed_amount

根据固定金额(固定价格)计算可称重项的运费

# iOSystemsShippingBundle Configuration
iosystems_shipping:
	# ...
	price_calculator:
		type: fixed_amount
		amount: 9.99

per_product

根据单个价格字段计算可称重物品的运费(内部使用Symfony PropertyAccess 组件)

# iOSystemsShippingBundle Configuration
iosystems_shipping:
	# ...
	price_calculator:
		type: per_product
		shipping_price_field: shipping_price

weight_treshold

根据重量/价格阈值计算可称重物品的运费。阈值来自提供者,但实际上只支持Doctrine ORM

# iOSystemsShippingBundle Configuration
iosystems_shipping:
	# ...
	price_calculator:
		type: weight_treshold
		provider: orm
		entity_class: Acme\DemoBundle\Entity\WeightPriceTreshold

您的 entity_class 必须是扩展自 WeightTresholdInterface 的Doctrine实体。或者,您也可以扩展 WeightTreshold,这是一个提供 weightprice 映射的抽象映射超类。

例如,给定以下阈值

  • (A) 重量上限为 5,价格为 4.99
  • (B) 重量上限为 10,价格为 9.99
  • (C) 重量上限为 15,价格为 14.99
  • (D) 重量上限为 20,价格为 19.99

那么

  • 如果总重量为 2.125,价格为 4.99 (A)
  • 如果总重量为 9.750,价格为 9.99 (B)
  • 如果总重量为 11.250,价格为 14.99 (B)
  • 如果总重量为 25.125,价格为 19.99 (D)

使用方法

根据 weight_calculators 的配置部分,实现 GrossWeightableInterface 和/或 DimensionalWeightableInterface

<?php
// ...

use iOSystems\ShippingBundle\Weightable\GrossWeightableInterface;

class OrderDetail implements GrossWeightableInterface
{
	//...

	/**
	 * {@inheritdoc}
     */
	public function getQuantity()
    {
		return $this->quantity;
	}

	/**
	 * {@inheritdoc}
     */
	public function getWeigth()
    {
		return $this->weight;
	}
}

然后获取 iosystems_shipping.manager 服务来计算单个(或数组)可称重物品的重量/价格

// Controller code
/** @var \iOSystems\ShippingBundle\Manager\ShippingManagerInterface $sm */
$sm = $this->get('iosystems_shipping.manager');

// Calculate the total price of a single weightable or array of weightables
$sm->getPrice($orderDetails);
$sm->getPrice($order->getDetails());

自定义

使用Symfony 2容器标签添加重量计算器

重量计算器声明它们支持的可称重类型。为了创建自定义计算器,实现 WeightCalculatorInterface 接口,提供 supports()getWeight() 方法

<?php
// ...

class MyWeightCalculator implements WeightCalculatorInterface
{
    /**
     * {@inheritdoc}
     */
    public function getWeight(WeightableInterface $weightable)
    {
		/** @var MyWeightableInterface $weightable */
		// Use your custom logic and return weight for a single weightable
		// ...
    }

	/**
	 * {@inheritdoc}
     */
	public function supports(WeightableInterface $weightable)
    {
		return $weightable instanceof MyWeightableInterface;
	}
}

当使用 iosystems_shipping.weight_calculator 标签时,自定义计算器会自动添加到默认计算策略(或任何扩展自抽象类 AbstractWeightCalculationStrategy 的自定义策略)中。

贡献,TODO和想法

欢迎为此项目做出贡献

  • 降低Symfony的要求(实际上 >=2.3)
  • 指定如何更改默认的重量计算策略
  • 配置和扩展类重构
  • 删除不需要的服务和参数(根据配置)
  • 使用自定义映射传递或基于Symfony版本的 DoctrineOrmMappingsPass
  • 添加新的阈值提供者(从文件、从配置等)
  • 添加新的价格计算器(反向阈值、基于数量等)
  • 重写注释和此README的英文