Update module configurations with indigenous PrestaShop webservices

by:

Web Development

In this weblog, we are heading to understand how to update module information or configurations with indigenous PrestaShop Webservice. As we know that Prestashop has Webservice API for its main PrestaShop tables, If we modify, or get information from indigenous Prestashop API like products, shoppers, and many others then it does not present any third-party module details connected with a specific products or buyer. In this blog, we’ll see step-by-step processes to realize it.

1- Permit PrestaShop webservices

Empower Prestashop Webservice from State-of-the-art Parameters->Webservice segment and Webservice account key for products and solutions. It will be proven like this

ps_webservice_img_1

2- Override main Prestashop course

Suppose, we have a module demomodule that established some configuration for the product or service and we want to update this info with the native items API of Prestashop

For this, override $webserviceParameters guarded variable of main Prestashop file product.php by module and include affiliation for demomodule in the demomodule/override/products.php like below code :

    shielded $webserviceParameters = [
        'objectMethods' => [
            'add' => 'addWs',
            'update' => 'updateWs',
        ],
        'objectNodeNames' => 'products',
        'fields' => [
            'id_manufacturer' => [
                'xlink_resource' => 'manufacturers',
            ],
            'id_supplier' => [
                'xlink_resource' => 'suppliers',
            ],
            'id_class_default' => [
                'xlink_resource' => 'categories',
            ],
            'new' => [],
            'cache_default_attribute' => [],
            'id_default_image' => [
                'getter' => 'getCoverWs',
                'setter' => 'setCoverWs',
                'xlink_resource' => [
                    'resourceName' => 'images',
                    'subResourceName' => 'products',
                ],
            ],
            'id_default_combination' => [
                'getter' => 'getWsDefaultCombination',
                'setter' => 'setWsDefaultCombination',
                'xlink_resource' => [
                    'resourceName' => 'combinations',
                ],
            ],
            'id_tax_rules_group' => [
                'xlink_resource' => [
                    'resourceName' => 'tax_rule_groups',
                ],
            ],
            'position_in_category' => [
                'getter' => 'getWsPositionInCategory',
                'setter' => 'setWsPositionInCategory',
            ],
            'manufacturer_name' => [
                'getter' => 'getWsManufacturerName',
                'setter' => false,
            ],
            'quantity' => [
                'getter' => false,
                'setter' => false,
            ],
            'type' => [
                'getter' => 'getWsType',
                'setter' => 'setWsType',
            ],
        ],
        'associations' => [
            'categories' => [
                'resource' => 'category',
                'fields' => [
                    'id' => ['required' => true],
                ],
            ],
            'images' => [
                'resource' => 'image',
                'fields' => ['id' => []],
            ],
            'combinations' => [
                'resource' => 'combination',
                'fields' => [
                    'id' => ['required' => true],
                ],
            ],
            'product_possibility_values' => [
                'resource' => 'product_option_value',
                'fields' => [
                    'id' => ['required' => true],
                ],
            ],
            'product_features' => [
                'resource' => 'product_feature',
                'fields' => [
                    'id' => ['required' => true],
                    'id_characteristic_value' => [
                        'required' => true,
                        'xlink_resource' => 'product_feature_values',
                    ],
                ],
            ],
            'tags' => ['resource' => 'tag',
                'fields' => [
                    'id' => ['required' => true],
                ], ],
            'stock_availables' => ['resource' => 'stock_available',
                'fields' => [
                    'id' => ['required' => true],
                    'id_merchandise_attribute' => ['required' => true],
                ],
                'setter' => untrue,
            ],
            'attachments' => [
                'resource' => 'attachment',
                'api' => 'attachments',
                'fields' => [
                    'id' => ['required' => true],
                ],
            ],
            'accessories' => [
                'resource' => 'product',
                'api' => 'products',
                'fields' => [
                    'id' => [
                        'required' => true,
                        'xlink_resource' => 'products', ],
                ],
            ],
            'product_bundle' => [
                'resource' => 'product',
                'api' => 'products',
                'fields' => [
                    'id' => ['required' => true],
                    'id_products_attribute' => [],
                    'quantity' => [],
                ],
            ],
            'wk_hbcss' => [
                'resource' => 'wk_hbcs',
                'api' => 'wk_hbcss',
                'fields' => [
                    'id_wk_hbcs' => ['required' => true,],
                    'country' => ['required' => true],
                    'id_product' => ['required' => true,],
                    'id_shop' => ['required' => true],
                ],
                'setter' => 'getWsWkHbcss',
                'getter' => 'setWsWkHbcss',
                'xlink_resource' => 'wk_hbcs',
            ],
        ],
    ]

3- Define setter/getter

Define the setter and getter purpose to established and get details. These features are dependable for the established and get info of module details. These are presently declared in the affiliation. wk_hbcs is the desk to store the module knowledge associated with products and solutions.

    /**
     * get knowledge from module
     *
     * @return void
     */
    public purpose getWsWkHbcss()
    
        return Db::getInstance()->executeS('SELECT * FROM  `'._DB_PREFIX_.'wk_hbcs` Wherever `id_product` = '.$this->id)
    

    /**
     * Set details for module
     *
     * @param int $idCountries
     * @return void
     */
    general public perform setWsWkHbcss($idCountries)
    
        Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'wk_hbcs` The place id_solution = ' .(int)$this->id . ' AND id_store=' . (int)$this->id_store_default)
        if (is_array($idCountries) && !empty($idCountries)) 
            foreach ($idCountries as $idCountry) 
                $data = array(
                'country' => (int) $idCountry['country'],
                'id_product' => (int) $this->id,
                'id_shop' => (int) $this->id_shop_default,
            )
                Db::getInstance()->insert(
                    'wk_hbcs',
                    $info,
                )
            
        
        return real
    

Now, when you fetch item information with API then it will also clearly show module info –

screenshot-2022.08.23-22_12_54

If you want to produce a individual API for the module just like indigenous Prestashop tables then comply with our other site.

Equally, this approach will also do the job for other classes of Prestashop like prospects.

That’s all about this site.

If any situation or question make sure you really feel absolutely free to point out it in the remark segment.

I would be joyful to help.

Also, you can explore our PrestaShop Improvement Services & a massive assortment of quality PrestaShop Modules.

For any doubt speak to us at [email protected]

Leave a Reply

Your email address will not be published.