扩展prestashop webservice资源

时间:2016-06-23 16:04:55

标签: php web-services api prestashop

我尝试使用另一个字段来扩展carts资源,该字段应输出Cart::getSummaryDetails调用的结果。我已经扩展CartCore类来添加新参数和相应的方法,如下所示:

class Cart extends CartCore
{
    protected $webserviceParameters = [
        'fields'       => [
            'id_address_delivery' => ['xlink_resource' => 'addresses'],
            'id_address_invoice'  => ['xlink_resource' => 'addresses'],
            'id_currency'         => ['xlink_resource' => 'currencies'],
            'id_customer'         => ['xlink_resource' => 'customers'],
            'id_guest'            => ['xlink_resource' => 'guests'],
            'id_lang'             => ['xlink_resource' => 'languages'],
        ],
        'associations' => [
            'cart_rows' => [
                'resource'       => 'cart_row',
                'virtual_entity' => true,
                'fields'         => [
                    'id_product'           => ['required' => true, 'xlink_resource' => 'products'],
                    'id_product_attribute' => ['required' => true, 'xlink_resource' => 'combinations'],
                    'id_address_delivery'  => ['required' => true, 'xlink_resource' => 'addresses'],
                    'quantity'             => ['required' => true],
                ],
            ],
            'summary'   => [
                'getter'         => 'getWsSummaryDetails',
                'setter'         => false,
                'virtual_entity' => true,
            ],
        ],
    ];

    public function getWsSummaryDetails()
    {
        $result = $this->getSummaryDetails(null, true);
        unset($result['carrier']);
        unset($result['delivery']);
        unset($result['invoice']);
        return $result;
    }
}

预期的输出应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<carts>
    <cart>
        <id>105</id>
        <id_address_delivery xlink:href="http://prestashop.lo/api/addresses/8">8</id_address_delivery>
        <id_address_invoice xlink:href="http://prestashop.lo/api/addresses/8">8</id_address_invoice>
        <id_currency xlink:href="http://prestashop.lo/api/currencies/1">1</id_currency>
        <id_customer xlink:href="http://prestashop.lo/api/customers/8">8</id_customer>
        <id_guest>0</id_guest>
        <id_lang xlink:href="http://prestashop.lo/api/languages/1">1</id_lang>
        <id_shop_group>0</id_shop_group>
        <id_shop>1</id_shop>
        <id_carrier>4</id_carrier>
        <recyclable>0</recyclable>
        <gift>0</gift>
        <gift_message/>
        <mobile_theme>0</mobile_theme>
        <delivery_option/>
        <secure_key>ced146ac54cc140e7d8da06469003688</secure_key>
        <allow_seperated_package>0</allow_seperated_package>
        <date_add>2016-06-20 11:14:31</date_add>
        <date_upd>2016-06-20 11:14:31</date_upd>
        <associations>
            <cart_rows nodeType="cart_row" virtualEntity="true">
                <cart_row>
                    <id_product xlink:href="http://prestashop.lo/api/products/8">8</id_product>
                    <id_product_attribute xlink:href="http://prestashop.lo/api/combinations/0">0</id_product_attribute>
                    <id_address_delivery xlink:href="http://prestashop.lo/api/addresses/8">8</id_address_delivery>
                    <quantity>1</quantity>
                </cart_row>
                <cart_row>
                    <id_product xlink:href="http://prestashop.lo/api/products/11">11</id_product>
                    <id_product_attribute xlink:href="http://prestashop.lo/api/combinations/0">0</id_product_attribute>
                    <id_address_delivery xlink:href="http://prestashop.lo/api/addresses/8">8</id_address_delivery>
                    <quantity>1</quantity>
                </cart_row>
            </cart_rows>
            <summary nodeType="cart_summary" api="summary">
                <products/>
                <gift_products/>
                <discounts/>
                <is_virtual_cart/>
                <total_discounts/>
                <total_discounts_tax_exc/>
                <total_wrapping/>
                <total_wrapping_tax_exc/>
                ... <!-- and so on -->
            </summary>
        </associations>
    </cart>
</carts>
</prestashop>

但我得到<summary>[Array]</summary>或xml解析错误。

我尝试添加cart_summary的另一种方法是创建包含所需CartSummary字段的$webserviceParameters对象。但我无法了解如何从此字段中获取值以及如何将此cart_summary对象与carts资源相关联。

1 个答案:

答案 0 :(得分:0)

嗯,调试是一个灵丹妙药。事实证明,扩展carts资源比我想象的要容易一些。 首先,没有必要提供任何额外的资源。我们唯一需要的是列出我们需要的字段并设置它们的getter。我的扩展Cart课程:

class Cart extends CartCore
{
    protected $webserviceParameters = [
        'fields'       => [
            'id_address_delivery' => ['xlink_resource' => 'addresses'],
            'id_address_invoice'  => ['xlink_resource' => 'addresses'],
            'id_currency'         => ['xlink_resource' => 'currencies'],
            'id_customer'         => ['xlink_resource' => 'customers'],
            'id_guest'            => ['xlink_resource' => 'guests'],
            'id_lang'             => ['xlink_resource' => 'languages'],
        ],
        'associations' => [
            'cart_rows'    => [
                'resource'       => 'cart_row',
                'virtual_entity' => true,
                'fields'         => [
                    'id_product'           => ['required' => true, 'xlink_resource' => 'products'],
                    'id_product_attribute' => ['required' => true, 'xlink_resource' => 'combinations'],
                    'id_address_delivery'  => ['required' => true, 'xlink_resource' => 'addresses'],
                    'quantity'             => ['required' => true],
                ],
            ],
            'cart_summary' => [
                'resource'       => 'cart_summary_row',
                'virtual_entity' => true,
                'setter'         => false,
                'fields'         => [
                    'id'                        => '',
                    'is_virtual_cart'           => '',
                    'total_discounts'           => '',
                    'total_discounts_tax_exc'   => '',
                    'total_wrapping'            => '',
                    'total_wrapping_tax_exc'    => '',
                    'total_shipping'            => '',
                    'total_shipping_tax_exc'    => '',
                    'total_products_wt'         => '',
                    'total_products'            => '',
                    'total_price'               => '',
                    'total_tax'                 => '',
                    'total_price_without_tax'   => '',
                    'is_multi_address_delivery' => '',
                    'free_ship'                 => '',
                ],
            ],
        ],
    ];


    /**
     * @return array
     */
    public function getWsCartSummary()
    {
        $result = $this->getSummaryDetails(null, true);
        unset($result['carrier']);
        unset($result['delivery']);
        unset($result['invoice']);
        $result['id'] = $this->id;
        return ['cart_summary' => $result];
    }
}

WebserviceOutputBuilder不适合处理对象,它需要制作更复杂的字段列表和getter。我只需要标量值。所以我取消了carrierdeliveyinvoice个对象。 Prestashop默认搜索字段getWsCartSummary()的getter cart_summary。如果您有any_other字段,则为getWsAnyOther()

我得到的结果:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<carts>
<cart>
    <id><![CDATA[108]]></id>
    <id_address_delivery xlink:href="http://prestashop.lo/api/addresses/5"><![CDATA[5]]></id_address_delivery>
    <id_address_invoice xlink:href="http://prestashop.lo/api/addresses/5"><![CDATA[5]]></id_address_invoice>
    <id_currency xlink:href="http://prestashop.lo/api/currencies/1"><![CDATA[1]]></id_currency>
    <id_customer xlink:href="http://prestashop.lo/api/customers/2"><![CDATA[2]]></id_customer>
    <id_guest xlink:href="http://prestashop.lo/api/guests/25"><![CDATA[25]]></id_guest>
    <id_lang xlink:href="http://prestashop.lo/api/languages/1"><![CDATA[1]]></id_lang>
    <id_shop_group><![CDATA[1]]></id_shop_group>
    <id_shop><![CDATA[1]]></id_shop>
    <id_carrier><![CDATA[5]]></id_carrier>
    <recyclable><![CDATA[0]]></recyclable>
    <gift><![CDATA[0]]></gift>
    <gift_message></gift_message>
    <mobile_theme><![CDATA[0]]></mobile_theme>
    <delivery_option></delivery_option>
    <secure_key><![CDATA[18fcd352cc7355102674d9056705f43c]]></secure_key>
    <allow_seperated_package><![CDATA[0]]></allow_seperated_package>
    <date_add><![CDATA[2016-06-24 09:34:42]]></date_add>
    <date_upd><![CDATA[2016-06-27 03:23:18]]></date_upd>
<associations>
<cart_rows nodeType="cart_row" virtualEntity="true">
    <cart_row>
    <id_product xlink:href="http://prestashop.lo/api/products/5"><![CDATA[5]]></id_product>
    <id_product_attribute xlink:href="http://prestashop.lo/api/combinations/19"><![CDATA[19]]></id_product_attribute>
    <id_address_delivery xlink:href="http://prestashop.lo/api/addresses/5"><![CDATA[5]]></id_address_delivery>
    <quantity><![CDATA[1]]></quantity>
    </cart_row>
    <cart_row>
    <id_product xlink:href="http://prestashop.lo/api/products/6"><![CDATA[6]]></id_product>
    <id_product_attribute xlink:href="http://prestashop.lo/api/combinations/31"><![CDATA[31]]></id_product_attribute>
    <id_address_delivery xlink:href="http://prestashop.lo/api/addresses/5"><![CDATA[5]]></id_address_delivery>
    <quantity><![CDATA[1]]></quantity>
    </cart_row>
    <cart_row>
    <id_product xlink:href="http://prestashop.lo/api/products/11"><![CDATA[11]]></id_product>
    <id_product_attribute xlink:href="http://prestashop.lo/api/combinations/0"><![CDATA[0]]></id_product_attribute>
    <id_address_delivery xlink:href="http://prestashop.lo/api/addresses/5"><![CDATA[5]]></id_address_delivery>
    <quantity><![CDATA[1]]></quantity>
    </cart_row>
</cart_rows>
<cart_summary nodeType="cart_summary_row" virtualEntity="true">
    <cart_summary_row>
    <id><![CDATA[108]]></id>
    <is_virtual_cart></is_virtual_cart>
    <total_discounts></total_discounts>
    <total_discounts_tax_exc></total_discounts_tax_exc>
    <total_wrapping></total_wrapping>
    <total_wrapping_tax_exc></total_wrapping_tax_exc>
    <total_shipping></total_shipping>
    <total_shipping_tax_exc></total_shipping_tax_exc>
    <total_products_wt><![CDATA[67.06]]></total_products_wt>
    <total_products><![CDATA[64.48]]></total_products>
    <total_price><![CDATA[67.06]]></total_price>
    <total_tax><![CDATA[2.58]]></total_tax>
    <total_price_without_tax><![CDATA[64.48]]></total_price_without_tax>
    <is_multi_address_delivery></is_multi_address_delivery>
    <free_ship><![CDATA[1]]></free_ship>
    </cart_summary_row>
</cart_summary>
</associations>
</cart>
</carts>
</prestashop>