Advanced Shipping is an extension for the e-commerce solution Magento.
The syntax using PHP 5.6 allows a great flexibility in setting delivery charges.
Technical Informations
The usage of the PHP syntax (with an Abstract Syntax Tree) has been preferred to the usage of a JSON syntax (with regular expressions and the eval function) as in previous versions of the extension. This is the result of considerations on security and performance.
The PHP code defined in the configuration is not evaluated with the eval function for security reasons. The library PHP-Parser is used to obtain an Abstract Syntax Tree (AST). The AST is browsed and only a limited set of functions and variables is allowed.
As the AST is not version-dependent, you can use the PHP 5.6 syntax even if your server uses a lower version of PHP.
Discover our add-ons on Owebia Store.
mb_strtolower
, mb_strtoupper
,
mb_substr
, preg_replace
, strtolower
, strtoupper
and time
# You must be in Magento root directory composer require owebia/magento2-module-advanced-shipping-setting:^2.1.0 php bin/magento cache:clean php bin/magento module:enable Owebia_AdvancedSettingCore php bin/magento module:enable Owebia_AdvancedShippingSetting php bin/magento setup:upgrade # Execute setup:di:compile only if the store is in production mode php bin/magento setup:di:compile
A call to addMethod
function adds a shipping method.
object addMethod ( string $method_id , array $method_properties )
List of method properties:
title
price
description
(optional)enabled
(optional): conditions for displaying the shipping method
Warning: to avoid conflicts, use only the characters a-z
, 0-9
and _
for the method id.
You should also avoid identifiers that already correspond to variable names (quote
, request
…).
You can use variables, functions and operators to define a dynamic value (all operators are not supported).
There are global variables and local variables that you can access with array functions and callbacks.
If you activate the debug option, you can see all data available for each object that you use.
Remember that you don't have access to all methods of the original objects because they are not accessed directly.
These variables are global variables and can be accessed everywhere (use the global
keyword when you are inside a function).
\Magento\Quote\Model\Quote\Address\RateRequest
) given by Magento:
$request->all_items
: array of items in cart$request->country_id
*$request->region_id
$request->city
$request->postcode
$request->dest_country_id
*$request->dest_region_id
$request->dest_region_code
$request->dest_street
(known issue with Magento CE < 2.1.8)$request->dest_city
(known issue with Magento CE < 2.1.8)$request->dest_postcode
$request->package_value
: value of the package$request->package_value_with_discount
: value of the package with discount$request->package_weight
: weight of the package$request->package_qty
: number of products in the package$request->free_shipping
: free shipping calculated by Magento rules$request->*
: property of the request object\Magento\Quote\Model\Quote
): (known issue #2, known issue #3)
$quote->subtotal
: subtotal (excluding tax)$quote->subtotal_with_discount
: subtotal (excluding tax) with discount$quote->grand_total
: total (including tax) with discount (known issues #3, you should avoid using this value)$quote->base_subtotal
: subtotal (excluding tax) in base currency$quote->base_subtotal_with_discount
: subtotal (excluding tax) with discount in base currency$quote->base_grand_total
: total (including tax) with discount in base currency (known issues #3, you should avoid using this value)$quote->coupon_code
: the coupon code used in cart$quote->*
: property of the quote (ex: $quote->subtotal
)\Magento\Customer\Model\Group
):
$customer_group->id
: alias of $customer_group->customer_group_id
$customer_group->code
: alias of $customer_group->customer_group_code
$customer_group->name
: alias of $customer_group->customer_group_code
$customer_group->*
: property of the customer group (ex: $customer_group->tax_class_id
)\Magento\Customer\Model\Customer
):
$customer->id
: alias of $customer->entity_id
$customer->*
: attribute of the customer (ex: email, lastname, firstname, group_id…)$customer->getCustomAttribute('my_custom_attribute')->value
: custom attribute of the customer (where 'my_custom_attribute' should be replaced by custom attribute's code)\Magento\Variable\Model\Variable
):
$variable->*
: custom variable defined in Magento (ex: $variable->my_var
)\Magento\Store\Model\Store
):
$store->id
$store->code
$store->name
$store->address
$store->phone
* Magento apparently uses the ISO 3166-1 alpha-2 codes.
Note that the country code for United Kingdom is GB and not UK.
You can use $request->all_items
with array functions.
\Magento\Quote\Model\Quote\Item
):
$item->qty
: quantity of the item$item->weight
: weight of the item$item->base_original_price
: price excluding tax without discount$item->price_incl_tax
: price including tax without discount$item->options->*
: option (the available options depend on the product)\Magento\Catalog\Model\Product
):
$item->product->*
sku
name
weight
price
(as defined in Magento backoffice)special_price
: (as defined in Magento backoffice)tax_class_id
: the taxclass idgetAttributeText('tax_class_id')
: the tax class namegetAttributeText('attribute_code')
\Magento\Catalog\Model\Category
)
$item->product->category->*
: attribute of the first categoryid
name
is_active
$item->product->category_ids
: array of id of the categories\Magento\Eav\Model\Entity\Attribute\Set
):
$item->product->attribute_set
: the attribute set$item->product->attribute_set->*
: attribute of the attribute setid
attribute_set_name
\Magento\CatalogInventory\Model\Stock\Item
):
$item->product->stock_item->*
: attribute of the product stockis_in_stock
qty
Look at the PHP documentation if you want to know how to use these functions.
Functions allowed:
France excluding DOM/TOM
You can use the name or ID of the customer groups.
Formulas can be used to calculate the price of the delivery.
Using the configuration of another element
Defining a table
Using an associative table
Warning: you must notice that in Magento, a product can be in multiple categories.
So be particularly careful how you use this property.
Known issue with Magento CE < 2.1.8
In the following example, my_custom_attribute
is a custom customer attribute created by a custom extension.
$request->dest_street
, $request->dest_city
and $quote->getShippingAddress()->*
$quote
variable.
$quote->grand_total
and $quote->base_grand_total
as these values include shipping fees.
© 2016-2017 Owebia