ConvertXToProductID bug fix when left null

This commit is contained in:
Prashant Singh 2019-07-18 18:20:21 +05:30
parent 750842b2a9
commit 48d068d760
4 changed files with 58 additions and 46 deletions

View File

@ -491,7 +491,7 @@
country_and_states: @json($cart_rule[2]), country_and_states: @json($cart_rule[2]),
category_options: @json($cart_rule[1]), category_options: @json($cart_rule[1]),
category_values: null, category_values: [],
attribute_values: [], attribute_values: [],
attr_object: { attr_object: {
@ -610,6 +610,8 @@
this.all_attributes.attributes = this.attribute_values; this.all_attributes.attributes = this.attribute_values;
this.all_attributes = JSON.stringify(this.all_attributes); this.all_attributes = JSON.stringify(this.all_attributes);
} else {
this.all_attributes = null;
} }
if (this.conditions_list.length != 0) { if (this.conditions_list.length != 0) {

View File

@ -692,6 +692,8 @@
this.all_attributes.attributes = this.attribute_values; this.all_attributes.attributes = this.attribute_values;
this.all_attributes = JSON.stringify(this.all_attributes); this.all_attributes = JSON.stringify(this.all_attributes);
} else {
this.all_attributes = null;
} }
if (this.conditions_list.length != 0) { if (this.conditions_list.length != 0) {

View File

@ -72,21 +72,23 @@ class ConvertXToProductId
$attributeValues = $attributeConditions->attributes; $attributeValues = $attributeConditions->attributes;
if (count($categoryValues)) { if (!isset($categoryValues) && ! isset($attributeValues)) {
return false;
}
if (isset($categoryValues) && count($categoryValues)) {
$categoryResult = $this->convertFromCategories($categoryValues); $categoryResult = $this->convertFromCategories($categoryValues);
} }
dd($categoryResult); if (isset($attributeValues) && count($attributeValues)) {
if (count($attributeValues)) {
$attributeResult = $this->convertFromAttributes($attributeValues); $attributeResult = $this->convertFromAttributes($attributeValues);
} }
// now call the function that will find all the unique product ids // now call the function that will find all the unique product ids
$productIds = $this->findAllUniqueIds($attributeResult, $categoryResult); $productIDs = $this->findAllUniqueIds($attributeResult, $categoryResult);
// save the product ids against the cart rules // save the product ids against the cart rules
$result = $this->saveIDs($ruleId, $productIds); $result = $this->saveIDs($ruleId, $productIDs);
if ($result) { if ($result) {
return true; return true;
@ -136,7 +138,8 @@ class ConvertXToProductId
)->get(); )->get();
foreach ($pavResults as $pavResult) { foreach ($pavResults as $pavResult) {
$products->push($pavResult->product); if ($pavResult->product->type == 'simple')
$products->push($pavResult->product);
} }
} }
} else { } else {
@ -188,7 +191,6 @@ class ConvertXToProductId
$data = $this->getAll($category->id); $data = $this->getAll($category->id);
} }
return $data; return $data;
} }
@ -204,14 +206,18 @@ class ConvertXToProductId
$attributeResult = $data[0]; $attributeResult = $data[0];
$categoryResult = $data[1]; $categoryResult = $data[1];
$pavs = $this->pav->all();
// find matched attribute options product ids // find matched attribute options product ids
$attributeRelatedIds = $this->convertFromAttributes($attributeResult); $mergedCollection = $attributeResult->merge($categoryResult);
dd($attributeRelatedIds); $productIDs = collect();
// find matched categories product ids foreach ($mergedCollection as $merged) {
$categoryRelatedIds = $this->convertFromCategories($categoryResult); $productIDs->push($merged->id);
}
// find all the unique product ids
$productIDs = $productIDs->unique();
return $productIDs->flatten()->all();
} }
/** /**
@ -219,12 +225,14 @@ class ConvertXToProductId
* *
* @return boolean * @return boolean
*/ */
public function saveIDs($ruleId, $productIds) public function saveIDs($ruleId, $productIDs)
{ {
$cartRule = $this->cartRule->find($ruleId); $cartRule = $this->cartRule->find($ruleId);
$productIDs = implode(',', $productIDs);
return $cartRule->update([ return $cartRule->update([
'product_ids' => $productIds 'product_ids' => $productIDs
]); ]);
} }
@ -234,41 +242,39 @@ class ConvertXToProductId
*/ */
public function getAll($categoryId = null) public function getAll($categoryId = null)
{ {
$params = request()->input(); $results = app('Webkul\Product\Repositories\ProductFlatRepository')->scopeQuery(function($query) use($categoryId) {
$channel = request()->get('channel') ?: (core()->getCurrentChannelCode() ?: core()->getDefaultChannelCode());
$results = app('Webkul\Product\Repositories\ProductFlatRepository')->scopeQuery(function($query) use($params, $categoryId) { $locale = request()->get('locale') ?: app()->getLocale();
$channel = request()->get('channel') ?: (core()->getCurrentChannelCode() ?: core()->getDefaultChannelCode());
$locale = request()->get('locale') ?: app()->getLocale(); $qb = $query->distinct()
->addSelect('product_flat.id')
->leftJoin('products', 'product_flat.product_id', '=', 'products.id')
->leftJoin('product_categories', 'products.id', '=', 'product_categories.product_id')
->where('product_flat.channel', $channel)
->where('product_flat.locale', $locale)
->whereNotNull('product_flat.url_key');
$qb = $query->distinct() if ($categoryId) {
->addSelect('product_flat.id') $qb->where('product_categories.category_id', $categoryId);
->leftJoin('products', 'product_flat.product_id', '=', 'products.id') }
->leftJoin('product_categories', 'products.id', '=', 'product_categories.product_id')
->where('product_flat.channel', $channel)
->where('product_flat.locale', $locale)
->whereNotNull('product_flat.url_key');
if ($categoryId) { if (is_null(request()->input('status'))) {
$qb->where('product_categories.category_id', $categoryId); $qb->where('product_flat.status', 1);
} }
if (is_null(request()->input('status'))) { if (is_null(request()->input('visible_individually'))) {
$qb->where('product_flat.status', 1); $qb->where('product_flat.visible_individually', 1);
} }
if (is_null(request()->input('visible_individually'))) { $queryBuilder = $qb->leftJoin('product_flat as flat_variants', function($qb) use($channel, $locale) {
$qb->where('product_flat.visible_individually', 1); $qb->on('product_flat.id', '=', 'flat_variants.parent_id')
} ->where('flat_variants.channel', $channel)
->where('flat_variants.locale', $locale);
});
$queryBuilder = $qb->leftJoin('product_flat as flat_variants', function($qb) use($channel, $locale) { return $qb->groupBy('product_flat.id');
$qb->on('product_flat.id', '=', 'flat_variants.parent_id') })->get();
->where('flat_variants.channel', $channel)
->where('flat_variants.locale', $locale);
});
return $qb->groupBy('product_flat.id');
})->get();
return $results; return $results;
} }

View File

@ -271,7 +271,9 @@ class CartRuleController extends Controller
$ruleCreated = $this->cartRule->create($data); $ruleCreated = $this->cartRule->create($data);
// can execute convert x here after when the rule is updated // can execute convert x here after when the rule is updated
$this->convertX->convertX($ruleUpdated->id, $attribute_conditions); if (isset($attribute_conditions) && $attribute_conditions != "[]" && $attribute_conditions != "") {
$this->convertX->convertX($ruleCreated->id, $attribute_conditions);
}
// create customer groups for cart rule // create customer groups for cart rule
$ruleGroupCreated = $this->cartRule->CustomerGroupSync($customer_groups, $ruleCreated); $ruleGroupCreated = $this->cartRule->CustomerGroupSync($customer_groups, $ruleCreated);