Search in order by SKU and SKU variation - search

I am trying to search by SKU and Variation SKU in AdminPanel>Orders>Search
I'm using this code, but it only searches by variation SKU.
For example, we sell shoes
Parent SKU - 1010
Sku of variation 1 - 1010-36
Sku of variation 2 - 1010-37
When I type 1010 into the search engine it doesn't give me any results, but when I type "1010-36" it shows the orders that contain that product.
The code I'm currently using is this:
add_filter( 'woocommerce_shop_order_search_results', 'bbloomer_order_search_by_sku', 9999, 3 );
function bbloomer_order_search_by_sku( $order_ids, $term, $search_fields ) {
global $wpdb;
if ( ! empty( $search_fields ) ) {
$variation_id = wc_get_product_id_by_sku( $wpdb->esc_like( wc_clean( $term ) ) );
if ( ! $variation_id ) return $order_ids;
$order_ids = array_unique(
$wpdb->get_col(
$wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_variation_id' AND meta_value = %d ) AND order_item_type = 'line_item'", $variation_id )
)
);
}
return $order_ids;
}

Related

Problems with the BQL "IN<>" statement

The requirement I have is to get a list of all discount codes defined in an instance and which ones a particular customer is currently assigned to, in the case given CustomerID=28. I further have to include only discount codes that naturally will be applicable to customers. There are only 3 of these; "Customer", "Customer and Item", "Customer and Item price Class". These are ARDiscount.ApplicableTo containing "CU", "CP","CI"
Select a.CompanyID, a.DiscountID, a.DiscountSequenceID, b.ApplicableTo, c.CustomerID
From DiscountSequence a
Join ARDiscount b On a.CompanyID = b.CompanyID and a.DiscountID = b.DiscountID
Left Outer Join DiscountCustomer c On a.CompanyID = c.CompanyID
And a.DiscountID = c.DiscountID
And a.DiscountSequenceID = c.DiscountSequenceID
And (IsNull(c.CustomerID,0) = 0 OR c.CustomerID = 72)
Where a.CompanyID = 2
And b.ApplicableTo In ('CU','CP','CI')
Order By a.DiscountID, a.DiscountSequenceID
I created data view delegate to return the 4 columns I need to display and in the view I created
to read the data like the SQL query above I used the BQL "IN<>" statement like this. The method was taken directlty from a blog post found here :
https://asiablog.acumatica.com/2017/11/sql-in-operator-in-bql.html
Object[] applicableTovalues = new String[] { "CP","CI","CU" }; // Customer and Price Class // Customer and Item// Customer
var Results = PXSelectJoin<DiscountSequence
, InnerJoin<ARDiscount, On<DiscountSequence.discountID, Equal<ARDiscount.discountID>>
, LeftJoin<DiscountCustomer, On<DiscountSequence.discountID, Equal<DiscountCustomer.discountID>,
And<DiscountSequence.discountSequenceID, Equal<DiscountCustomer.discountSequenceID>,
And<Where<DiscountCustomer.customerID, Equal<Current<Customer.bAccountID>>,
Or<DiscountCustomer.customerID, IsNull>>>>>>>
, Where<DiscountSequence.discountID, IsNotNull
, And<ARDiscount.applicableTo, In<Required<ARDiscount.applicableTo>>>>
, OrderBy<Asc<DiscountSequence.discountID, Asc<DiscountSequence.discountSequenceID>>>
>.Select(Base, applicableTovalues);
The problem is that the resulting SQL server select statement caught with TRACE only includes the first of the three IN values (''CU'') leaving (CI and CU) out.
I was expecting all three values in the IN statement like this : AND [ARDiscount].[ApplicableTo] IN ( ''CP'', ''CI'', ''CU'')
exec sp_executesql N'SELECT [DiscountSequence].[DiscountID], [DiscountSequence].[DiscountSequenceID], [DiscountSequence].[LineCntr],
<snip>
[DiscountCustomer].[CreatedDateTime], [DiscountCustomer].[LastModifiedByID], [DiscountCustomer].[LastModifiedByScreenID], [DiscountCustomer].[LastModifiedDateTime]
FROM [DiscountSequence] [DiscountSequence] INNER JOIN [ARDiscount] [ARDiscount] ON ( [ARDiscount].[CompanyID] = 2) AND [DiscountSequence].[DiscountID] = [ARDiscount].[DiscountID]
LEFT JOIN [DiscountCustomer] [DiscountCustomer] ON ( [DiscountCustomer].[CompanyID] = 2) AND [DiscountSequence].[DiscountID] = [DiscountCustomer].[DiscountID]
AND [DiscountSequence].[DiscountSequenceID] = [DiscountCustomer].[DiscountSequenceID] AND ( [DiscountCustomer].[CustomerID] = #P0 OR [DiscountCustomer].[CustomerID] IS NULL )
WHERE ( [DiscountSequence].[CompanyID] = 2)
AND ( [DiscountSequence].[DiscountID] IS NOT NULL
AND [ARDiscount].[ApplicableTo] IN ( ''CU''))
ORDER BY [DiscountSequence].[DiscountID], [DiscountSequence].[DiscountSequenceID]
OPTION(OPTIMIZE FOR UNKNOWN) /* AR.30.30.00 */',N'#P0 int',#P0=39
The issue is passing the array into the 'params' parameter. It thinks that you are passing a list of parameters into the bql query instead of a single array as a parameter.
If you cast it as follows it should work:
.Select(Base, (object)applicableTovalues);

PowerBI - Column output based on another column value and time function

I have a table like that:
I'm comparing values from CurrentMonth vs PreviousMonth (based on the month selected in the slicer) for all of the KPIs, and based on that, I assign a specific result.
However, for each KPI, there is a logic behind when comparing. The logic is:
So, for KPI A, the CurrentMonth value must be 10% higher then PreviousMonth. When this is true, then assign 1. If not true, then 0. For KPI B, it has to be 3% higher. And for KPI C, it just needs to be higher.
To pull the CurrentMonth and PreviousMonth values, I created two measures, one of them with a time function:
CurrentMonth = SUM( [KPIValue] )
and
PreviousMonth = CALCULATE (
SUM( [KPIValue] );
PREVIOUSMONTH( [Date] )
)
I tried to create a Column in my table to assign the output (Result), and used a Switch function like this:
Result = SWITCH (
[KPIName];
"KPI A"; IF( [CurrentMonth] >= [PreviousMonth]*1.10;1;0 );
"KPI B"; IF( [CurrentMonth] >= [PreviousMonth]*1.03;1;0 );
"KPI C"; IF( [CurrentMonth] >= [PreviousMonth];1;0 );
)
My issue is that when I try to use this Switch, it does not work since the PreviousMonth comes as blank (it can not calculate a time function for each row of the table - only works as a measure).
I'm now lost how should I proceed from here. I tried to create a Measure instead of a Column, but then the Switch function does not accept the KPIName as a parameter.
Any ideas? Hope I've been clear here, if not, let me know what further information you need.
Found the solution. As a Measure (not column), include SELECTEDVALUE to be able to retrieve KPIName.
Result = SWITCH (
SELECTEDVALUE([KPIName]);
"KPI A"; IF( [CurrentMonth] >= [PreviousMonth]*1.10;1;0 );
"KPI B"; IF( [CurrentMonth] >= [PreviousMonth]*1.03;1;0 );
"KPI C"; IF( [CurrentMonth] >= [PreviousMonth];1;0 );
)

Different approach for pagination - LIMIT when using SQLSRV

I'm trying to make an instant pagination using SQLSRV and PHP, I have successfully did this using MySQL but unable to do so when using SQL Server as it does not support LIMIT.
I have the following codes working in MySQL and I wanted to apply the same thing in sqlsrv but since this is not possible, I'm looking forward in creating a different approach(code) to achieve this, can someone give me an idea or a walkthrough to make this happen please, thanks in advanced.
if(isset($_POST['page'])):
$paged=$_POST['page'];
$sql="SELECT * FROM `member` ORDER BY `member`.`member_id` ASC";
if($paged>0){
$page_limit=$resultsPerPage*($paged-1);
$pagination_sql=" LIMIT $page_limit, $resultsPerPage";
}
else{
$pagination_sql=" FETCH 0 , $resultsPerPage";
}
$result=sqlsrv_query($sql.$pagination_sql);
Try the following code, I hope you find it helpful
$paged = filter_input(INPUT_POST, 'page', FILTER_SANITIZE_NUMBER_INT);
//Initialize these values
$Table = 'your_tbl_name'; //Table name
$IndexColumn = 'pk_col_name'; //Primary key column
$resultsPerPage = '10'; //Page size
$Where = ''; //Optional WHERE clause, may leave empty
$Order = ''; //Optional ORDER clause, may leave empty
$top = ($paged>0) ? $resultsPerPage * ($paged-1) : 0 ;
$limit = 'TOP ' . $resultsPerPage ;
$pagination_sql = "SELECT $limit *
FROM $Table
$Where ".(($Where=="")?" WHERE ":" AND ")." $IndexColumn NOT IN
(
SELECT $IndexColumn FROM
(
SELECT TOP $top *
FROM $Table
$Where
$Order
)
as [virtTable]
)
$Order";
$result=sqlsrv_query($conn, $pagination_sql);

laravel 5 - paginate total() of a query with distinct

I have a query to get photos according to values in a pivot table, that stores the relation of "pics" and "tags":
#photos
$q = PicTag::select(DB::raw('distinct(pics.id)'),
'pics.url',
'pics.titel',
'pics.hits',
'pics.created_at',
'users.username',
'users.displayname')
->leftJoin('pics', 'pics.id', '=', 'pic_tag.pic_id')
->leftJoin('users','users.id','=','pics.user_id')
->whereIn('pic_tag.tag_id', $tagids);
if($cat)
$q->where('typ',$cat);
if($year)
$q->where('jahrprod',$year);
$pics = $q->orderBy('pics.id','desc')
->paginate(30);
The problem is, when for a certain photo multiple (same) tags are stored like "Tag", "tag" and "tAG". Then the same photo would be shown 3 times in my gallery. That is why I use the distinct in the query.
Then the gallery is ok, but $pics->total() does not show "87 photos" but for example "90 photos", because the distinct is not used in the pagination. In laravel 4, I used groupBy('pics.id'), but this did not seem to be the fastest query and with laravel 5 it gives me a total() count result of 1.
How could I get the right total() value?
I know it's an old subject but it could help some other people.
I faced the same problem and the only good solution (low memory cost) I found was to do the request in two times:
$ids = DB::table('foo')
->selectRaw('foo.id')
->distinct()
->pluck('foo.id');
$results = $query = DB::table('foo')
->selectRaw('foo.id')
->whereIn('foo.id', $ids)
->paginate();
I tried this with 100k results, and had no problem at all.
Laravel has issue in paginate of complex queries. so you should handle them manually . In laravel 5 I did it in 2 steps :
Step 1: repository method :
public function getByPage($page = 1, $limit = 10 , $provinceId , $cityId , $expertiseId)
{
$array = ['users.deleted' => false];
$array["users.approved"] = true;
$array["users.is_confirmed"] = true;
if($provinceId)
$array["users.province_FK"] = $provinceId;
if($cityId)
$array["users.city_FK"] = $cityId;
if($expertiseId)
$array["ONJNCT_USERS_EXPERTISE.expertise_FK"] = $expertiseId;
$results = new \stdClass();
$results->page = $page;
$results->limit = $limit;
$results->totalItems = 0;
$results->items = array();
$users= DB::table('users')
->distinct()
->select('users.*','ONDEGREES.name as drgree_name')
->join('ONJNCT_USERS_EXPERTISE', 'users.id', '=', 'ONJNCT_USERS_EXPERTISE.user_FK')
->join('ONDEGREES', 'users.degree_FK', '=', 'ONDEGREES.id')
->where($array)
->skip($limit * ($page - 1))->take($limit)->get();
//$users = $usersQuery>skip($limit * ($page - 1))->take($limit)->get();
$usersCount= DB::table('users')
->select('users.*','ONDEGREES.name as drgree_name')
->join('ONJNCT_USERS_EXPERTISE', 'users.id', '=', 'ONJNCT_USERS_EXPERTISE.user_FK')
->join('ONDEGREES', 'users.degree_FK', '=', 'ONDEGREES.id')
->where($array)
->count(DB::raw('DISTINCT users.id'));
$results->totalItems = $usersCount;
$results->items = $users;
return $results;
}
Step 2:
In my Search Controller :
function search($provinceId , $cityId , $expertiseId){
$page = Input::get('page', 1);
$data = $this->userService->getByPage($page, 1 , $provinceId ,$cityId , $expertiseId);
$users = new LengthAwarePaginator($data->items, $data->totalItems, 1 , Paginator::resolveCurrentPage(),['path' => Paginator::resolveCurrentPath()]);
return View::make('guest.search.searchResult')->with('users' ,$users);
}
It worked for me well!

Drupal 7 - Ubercart - Attributes in Views Fields

I have products that have attributes for 'color' & 'strength'. I'm trying to get those options listed under those attributes as fields for views, so that I can use them as filters. So for example sort by color & strength.
I've looked all around on google and can only find modules for Drupal 6. Anyone know of anything for 7?
Like said in the previous post I had 2 attributes 'color' & 'strength' which I needed to match for exact product matches but ubercart didn't have anything for that, so I wrote them into URL for get statements as variables 1 & 2, so for example a URL which had both attributes selected would look like:
www.website.com/node/68?1=96&2=7
Sometimes one variable would be set but not the other, so I had to make up for that also by using a % wildcard. Here's the code for that part
// Since PHP's serialize function sometimes serializes in the incorrect order,
// here we manually build the comparison key
// Additionally append the image links urls with provided strength/color data
if( isset( $_REQUEST['1'] ) ) {
$_1 = $_REQUEST['1'];
if( $_1 !== '%' ) $url[] = "1=$_1";
} else $_1 = '%';
if( isset( $_REQUEST['2'] ) ) {
$_2 = $_REQUEST['2'];
if( $_2 !== '%' ) $url[] = "2=$_2";
} else $_2 = '%';
$combination = "a:2:{i:1;s:";
$combination .= $_1 == '%' ? '%:"%";' : strlen($_1) . ':"' . $_1 . '";';
$combination .= "i:2;s:";
$combination .= $_2 == '%' ? '%:"%";' : strlen($_2) . ':"' . $_2 . '";';
$combination .= '}';
// if some products don't have a second attribute at all
$combination2 = "a:1:{i:1;s:";
$combination2 .= $_1 == '%' ? '%:"%"' : strlen($_1) . ':"' . $_1 . '";';
$combination2 .= ';}';
Underneath that I had to do some extra queries like if a taxonomy was set and check if some dynamic properties were set, thats why the WHERE is stored in a variable. Frankly they'll just confuse anyone so I left them out. But for your sake the next part you just need to query it.
$where = "WHERE (pa.combination LIKE :pattern1 OR pa.combination LIKE :pattern2) AND s.stock IS NOT NULL";
$comparison = array( ':pattern1' => $combination,
':pattern2' => $combination2 );
$filtered = db_query(
"
SELECT pa.nid, pa.model, pa.combination, n.title, p.sell_price, f.uri
FROM {uc_product_adjustments} pa
LEFT JOIN {node} n ON pa.nid = n.nid
LEFT JOIN {uc_products} p ON pa.nid = p.nid
LEFT JOIN {field_data_uc_product_image} i ON i.entity_type = 'node' AND i.entity_id = n.nid
LEFT JOIN {file_managed} f ON f.fid = i.uc_product_image_fid
LEFT JOIN {uc_product_stock} s ON pa.model = s.sku AND s.stock <> '0'
$where
",
$comparison
);
Lastly loop over the results and store them in a normal array
foreach( $filtered as $i => $record ) {
if( is_int( array_search( $record->nid, $nids ) ) ) continue;
else {
$nids[] = $record->nid;
$result[] = $record;
}
}
This code checks for any products that match any of the attribute values that are currently in stock
You may have a look at these sandbox projects : UC Views Attributes Work and UC attribute views.
I'm looking for a similar feature, as it seems not possible to get UC attributes into a view. I cannot test myself right now as I have a deadline this weekend for a project, but I'd be happy to have your feedback.

Resources