Skip to content

Commit

Permalink
ADD - Compat with Wk Product Combination
Browse files Browse the repository at this point in the history
  • Loading branch information
BadPixxel committed Jan 11, 2024
1 parent 1abed4a commit 6f91695
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 3 deletions.
22 changes: 20 additions & 2 deletions modules/splashsync/src/Objects/Product/MetaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PrestaShopException;
use Splash\Local\Services\LanguagesManager as SLM;
use Splash\Local\Services\TotSwitchAttributes;
use Splash\Local\Services\WkCombination;
use Translate;

/**
Expand Down Expand Up @@ -109,12 +110,19 @@ protected function getMetaFields(string $key, string $fieldName): void
break;
case 'available_for_order':
//====================================================================//
// For Compatibility with Tot Switch Attribute Module
// For Compatibility with Tot Switch Attribute Module
if (TotSwitchAttributes::isDisabled($this->AttributeId, $this->Attribute)) {
$this->out[$fieldName] = false;

break;
}
//====================================================================//
// For Compatibility with Webkul Prestashop Combination Module
if (WkCombination::isDisabled($this->AttributeId, $this->Attribute)) {
$this->out[$fieldName] = false;

break;
}

$this->getSimpleBool($fieldName);

Expand Down Expand Up @@ -189,7 +197,17 @@ protected function setMetaFields(string $fieldName, $fieldData): void
case 'available_for_order':
//====================================================================//
// For Compatibility with Tot Switch Attribute Module
if (TotSwitchAttributes::setAvailablility($this->AttributeId, $this->Attribute, (bool) $fieldData)) {
if (TotSwitchAttributes::setAvailability($this->AttributeId, $this->Attribute, (bool) $fieldData)) {
break;
}
//====================================================================//
// For Compatibility with Webkul Prestashop Combination Module
if (WkCombination::setAvailability(
$this->ProductId,
$this->AttributeId,
$this->Attribute,
(bool) $fieldData
)) {
break;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/splashsync/src/Services/TotSwitchAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static function isDisabled(?int $attributeId, ?Combination $attribute): b
*
* @return bool return TRUE if Product Attribute Status Updated
*/
public static function setAvailablility(?int $attributeId, ?Combination $attribute, bool $value): bool
public static function setAvailability(?int $attributeId, ?Combination $attribute, bool $value): bool
{
//====================================================================//
// Check if Module is Active
Expand Down
174 changes: 174 additions & 0 deletions modules/splashsync/src/Services/WkCombination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

/*
* This file is part of SplashSync Project.
*
* Copyright (C) Splash Sync <www.splashsync.com>
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Splash\Local\Services;

use Combination;
use Db;
use Module;
use PrestaShopException;
use Shop;
use Splash\Core\SplashCore as Splash;

/**
* Bridge to Manage Compatibility with Webkul Prestashop Combination Activate/Deactivate
*/
class WkCombination
{
/**
* Check if Webkul Prestashop Combination Module is Active
*
* @return bool
*/
public static function isFeatureActive()
{
//====================================================================//
// Check if Module is Active
if (!Module::isEnabled("wkcombinationcustomize")) {
return false;
}

return true;
}

/**
* Check if Product Combination is Disabled
*
* @param null|int $attributeId Ps Product Attribute ID
* @param null|Combination $attribute Ps Product Attribute Class
*
* @throws PrestaShopException
*
* @return bool return TRUE if Product Attribute is Disabled
*/
public static function isDisabled(?int $attributeId, ?Combination $attribute): bool
{
//====================================================================//
// Check if Module is Active
if (!self::isFeatureActive()) {
return false;
}
//====================================================================//
// Not on Attribute Context => Skip
if (($attributeId <= 0) || !($attribute instanceof Combination)) {
return false;
}
//====================================================================//
// Check if Product Attribute Id is Disabled
$sql = 'SELECT * FROM `'._DB_PREFIX_.'wk_combination_status`'
.' WHERE `id_ps_product_attribute` = '.(int) $attributeId
.' AND `id_shop` = '.(int) Shop::getContextShopID()
;

return !empty(Db::getInstance()->executeS($sql));
}

/**
* Update Product Combination Status if Possible
*
* @param null|int $productId Ps Product ID
* @param null|int $attributeId Ps Product Attribute ID
* @param null|Combination $attribute Ps Product Attribute Class
* @param bool $value New Product Attribute Status
*
* @return bool return TRUE if Product Attribute Status Updated
*/
public static function setAvailability(
int $productId,
?int $attributeId,
?Combination $attribute,
bool $value
): bool {
//====================================================================//
// Check if Module is Active
if (!self::isFeatureActive()) {
return false;
}
//====================================================================//
// Not on Attribute Context => Skip
if (($attributeId <= 0) || !($attribute instanceof Combination)) {
return false;
}
//====================================================================//
// Update Product Attribute Status
self::updateProductAttribute($productId, $attributeId, $value);

return true;
}

/**
* Update product attribute status
*/
private static function updateProductAttribute(int $idProduct, ?int $idAttribute, bool $value): void
{
$sql = array();
//====================================================================//
// Detect Shop Id
$idShop = Shop::getContextShopID(true);
if (null == $idShop) {
$idShops = Shop::getContextListShopID();
}
//====================================================================//
// Prepare Sql For Updates
if (!$value) {
if (is_array($idShops)) {
foreach ($idShops as $idShop) {
$sql[] = self::getDisableSql($idProduct, $idAttribute, $idShop);
}
} else {
$sql[] = self::getDisableSql($idProduct, $idAttribute, 1);
}
} else {
if (is_array($idShops)) {
foreach ($idShops as $idShop) {
$sql[] = self::getEnableSql($idProduct, $idAttribute, $idShop);
}
} else {
$sql[] = self::getEnableSql($idProduct, $idAttribute, 1);
}
}
//====================================================================//
// Execute Updates
foreach ($sql as $request) {
if (!Db::getInstance()->execute($request)) {
Splash::log()->errNull("Fail to update Product Attribute Webkul Status.");
}
}
}

/**
* Get Disable Product Attribute SQL
*/
private static function getDisableSql(int $idProduct, int $idAttribute, int $idShop): string
{
return 'REPLACE INTO `'
._DB_PREFIX_.'wk_combination_status`(`id_ps_product`, `id_ps_product_attribute`, `id_shop`)'
.' VALUES ('.$idProduct.', '.$idAttribute.', '.$idShop.');'
;
}

/**
* Get Enable Product Attribute SQL
*/
private static function getEnableSql(int $idProduct, int $idAttribute, int $idShop): string
{
return 'DELETE FROM `'._DB_PREFIX_.'wk_combination_status`'
.' WHERE `id_ps_product` = '.$idProduct
.' AND `id_ps_product_attribute` = '.$idAttribute
.' AND `id_shop` = '.$idShop
.';'
;
}
}

0 comments on commit 6f91695

Please sign in to comment.