Read More
Read More
Menu Close

    Update module configurations with native PrestaShop webservices

    In this blog, we are going to learn how to update module data or configurations with native PrestaShop Webservice. As we know that Prestashop has Webservice API for its core PrestaShop tables, If we modify, or get data from native Prestashop API like products, customers, etc then it does not show any third-party module data associated with a particular product or customer. In this blog, we’ll see step-by-step processes to achieve it.

    1- Enable PrestaShop webservices

    Enable Prestashop Webservice from Advanced Parameters->Webservice section and Webservice account key for products. It will be shown like this

    ps_webservice_img_1

    2- Override core Prestashop class

    Start your headless eCommerce
    now.
    Read More

    Suppose, we have a module demomodule that set some configuration for the product and we want to update this data with the native products API of Prestashop

    For this, override $webserviceParameters protected variable of core Prestashop file product.php by module and add association for demomodule in the demomodule/override/product.php like below code :

        protected $webserviceParameters = [
            'objectMethods' => [
                'add' => 'addWs',
                'update' => 'updateWs',
            ],
            'objectNodeNames' => 'products',
            'fields' => [
                'id_manufacturer' => [
                    'xlink_resource' => 'manufacturers',
                ],
                'id_supplier' => [
                    'xlink_resource' => 'suppliers',
                ],
                'id_category_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_option_values' => [
                    'resource' => 'product_option_value',
                    'fields' => [
                        'id' => ['required' => true],
                    ],
                ],
                'product_features' => [
                    'resource' => 'product_feature',
                    'fields' => [
                        'id' => ['required' => true],
                        'id_feature_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_product_attribute' => ['required' => true],
                    ],
                    'setter' => false,
                ],
                '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_product_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 function to set and get data. These functions are responsible for the set and get data of module data. These are already declared in the association. wk_hbcs is the table to store the module data associated with products.

        /**
         * get data from module
         *
         * @return void
         */
        public function getWsWkHbcss()
        {
            return Db::getInstance()->executeS('SELECT * FROM  `'._DB_PREFIX_.'wk_hbcs` WHERE `id_product` = '.$this->id);
        }
    
        /**
         * Set data for module
         *
         * @param int $idCountries
         * @return void
         */
        public function setWsWkHbcss($idCountries)
        {
            Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'wk_hbcs` WHERE id_product = ' .(int)$this->id . ' AND id_shop=' . (int)$this->id_shop_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',
                        $data,
                    );
                }
            }
            return true;
        }

    Now, when you fetch product data with API then it will also show module data –

    screenshot-2022.08.23-22_12_54

    If you want to create a separate API for the module just like native Prestashop tables then follow our other blog.

    Similarly, this process will also work for other classes of Prestashop like customers.

    That’s all about this blog.

    If any issue or doubt please feel free to mention it in the comment section.

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services & a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected]

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top