Last active
May 10, 2021 07:02
-
-
Save basherr/35567af0e41797cea17115b4154adbc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Conversations; | |
use Illuminate\Support\Arr; | |
use App\Extensions\Elements\Button; | |
use App\Extensions\Elements\Buttonset; | |
use BotMan\BotMan\Messages\Incoming\Answer; | |
/** | |
* ProductOptions class | |
* | |
* The conversation class for suggesting options | |
* | |
* @package App\Conversations | |
*/ | |
class ProductOptions extends Category | |
{ | |
/** | |
* The options to suggest for selected sku | |
* | |
* @var string | |
*/ | |
protected $sku; | |
/** | |
* Receive incoming parameters from the route (phrase) | |
* | |
* @param string $options filter options | |
* @return \App\Conversations\Category | |
*/ | |
public function receive($categoryId = null, $sku = null, $phrase = null, $page = null) | |
{ | |
$this->categoryId = $categoryId; | |
$this->sku = $sku; | |
$this->phrase = $phrase; | |
$this->page = $page; | |
return $this; | |
} | |
/** | |
* Execute the conversation | |
* | |
* @return \App\Conversations\ProductListConversation | |
*/ | |
public function run() | |
{ | |
$this->bot->types(); | |
$product = $this->load() | |
->get('products') | |
->first(function($product) { | |
return $product->get('sku') === $this->sku; | |
}); | |
$this->suggestOptions($product); | |
return $this; | |
} | |
/** | |
* Suggest options for sizes | |
* | |
* @param \Illuminate\Support\Collection $product | |
* @return void | |
*/ | |
protected function suggestOptions($product) | |
{ | |
$sizes = $this->getSizes($product); | |
$buttons = $product->get('option') | |
->get('value') | |
->map(function($option) { | |
return Button::create($option->get('label')) | |
->value($option->get('code')); | |
}); | |
$buttonset = Buttonset::create() | |
->text('Please select any option') | |
->addButtons($buttons) | |
->asSuggestions(); | |
$this->ask($buttonset) | |
->then(function($answer) use ($product) { | |
if ($answer->isInteractiveMessageReply()) { | |
$this->addToCart($product, $answer->getText()); | |
} else { | |
$this->suggestOptions(); | |
} | |
}); | |
} | |
/** | |
* Add the option to the cart | |
* | |
* @param \Illuminate\Support\Collection $product | |
* @param string $option | |
* @return void | |
*/ | |
protected function addToCart($product, string $option) | |
{ | |
$query = Arr::query([ | |
'sku' => $product->get('sku'), | |
'options' => [], | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment