laravel-woocommerce icon indicating copy to clipboard operation
laravel-woocommerce copied to clipboard

Search where('name', 'like', 'abcde%') possible ?

Open piloufaces opened this issue 5 years ago • 3 comments

Hi,

I have an issue when I try to query the Woocommerce products using Where ... like

Product::where('name', 'like', 'abcde%')->get();

Possible or do we need to setup a filter based on all products ?

piloufaces avatar May 07 '20 15:05 piloufaces

Hello @piloufaces

Thanks for creating the issue. Sorry, currently like operator not works. You can simple search using options

try {
    return Product::all([
	'search' => 'abcde'
    ]);
} catch (\Exception $ex) {
    echo $ex->getMesage();
}

Search functionality match your query both Title and Description. If match any result it returns the results. Below I added which filter covered which fields

  • [x] Search

Title (covered by ?search=x) Description (covered by ?search=x)

  • [ ] Sku

SKU (covered by ?sku=x)

  • [ ] Type

Type (covered by ?type=x)

  • [ ] Category

Category (covered by ?category=1)

  • [ ] Tag

Tag (covered by ?tag=1)

  • [ ] Date

Date (covered by ?after=YYYY-MM-DD and ?before=YYYY-MM-DD)

  • [ ] Attributes

Attributes (covered by attribute=x and attribute_term=1)

  • [ ] Stock

Stock: In stock, Out of Stock (covered by ?in_stock=true/false)

  • [ ] Featured product

Featured product (covered by ?featured=true/false)

  • [ ] Price

Price (covered by ?min_price=1&max_price=10)

  • [ ] Shipping classes

Shipping classes (covered by ?shipping_class=1)

  • [ ] Tax classes

Tax classes (covered by ?tax_class=x)

I've a plan to add it. I'll notify if I add.

Thanks

maab16 avatar May 07 '20 22:05 maab16

Hello @piloufaces

Good news. I added dynamic where clause in version v2.6. Now you can use below example from version 2.6 or above. Make sure your version is 2.6 or above otherwise update it before use.

// The 'title', 'name' and 'description' are same. Don't put any percent or other things. Just put your search value. It will automatically search all Title and Description and find out matching
$products = Product::where('title', 'like', 'hello')->get();
// this one same as previous one
$products = Product::whereTitle('hello')->get();

// When you use camelCase please make sure you used it correct form. If your search field is multi wards or separate by underscore (_) for example your search field is `min_price` then you must call it below format

$products = Product::whereMinPrice(5)->get();

// Here two words min and price for this reason we converted it Min and Price and joined with where

Search

$products = Product::where('name',  'hello')->get();
OR
$products = Product::whereName('hello')->get();
OR
$products = Product::where('title', 'like', 'hello')->get();
OR
$products = Product::whereTitle('hello')->get();
OR
$products = Product::where('description', ''hello')->get();
OR
$products = Product::whereDescription('hello')->get();

Sku

$products = Product::where('sku',  's120')->get();
OR
$products = Product::whereSku('s120')->get();

Type

$products = Product::where('type',  'value')->get();
OR
$products = Product::whereType('value')->get();

Category

$products = Product::where('category',  'value')->get();
OR
$products = Product::whereCategory('value')->get();

Tag

$products = Product::where('tag',  'value')->get();
OR
$products = Product::whereTag('value')->get();

Date before

$products = Product::where('before',  'YYYY-MM-DD')->get();
OR
$products = Product::whereBefore('YYYY-MM-DD')->get();

Date before

$products = Product::where('after',  'YYYY-MM-DD')->get();
OR
$products = Product::whereAfter('YYYY-MM-DD')->get();

Attribute

$products = Product::where('attribute',  'value')->get();
OR
$products = Product::whereAttribute('value')->get();

Attribute Term

$products = Product::where('attribute_term',  1)->get();
OR
$products = Product::whereAttributeTerm(1)->get();

In Stock

$products = Product::where('in_stock',  'true')->get();
OR
$products = Product::whereInStock('true')->get();

Out of Stock

$products = Product::where('in_stock',  'false')->get();
OR
$products = Product::whereInStock('false')->get();

Featured product

$products = Product::where('featured',  'true/false')->get();
OR
$products = Product::whereFeatured('true/false')->get();

Min price

$products = Product::where('min_price',  10)->get();
OR
$products = Product::whereMinPrice(10)->get();

Max price

$products = Product::where('min_price',  100)->get();
OR
$products = Product::whereMaxPrice(100)->get();

Shipping classes

$products = Product::where('shipping_class',  'value')->get();
OR
$products = Product::whereShippingClass('value')->get();

Tax classes

$products = Product::where('tax_class',  'value')->get();
OR
$products = Product::whereTaxClass('value')->get();

I hope you enjoy the code. If you have any other issue you can reply with details.

Thanks

maab16 avatar May 08 '20 00:05 maab16

Hi @Codexshaper and @maab16 Following this thread, can search orders where used a specific coupon? Alternatively, can search orders were used any coupon? hope can you help me! Thanks a lot

JacoboMachado avatar Jun 24 '21 18:06 JacoboMachado