Skip to content

Instantly share code, notes, and snippets.

@chillbits-legacy
Created March 19, 2021 13:53
Show Gist options
  • Save chillbits-legacy/86a264dae968cfa930401062dc94d2c6 to your computer and use it in GitHub Desktop.
Save chillbits-legacy/86a264dae968cfa930401062dc94d2c6 to your computer and use it in GitHub Desktop.
<?php
namespace Bree\Features\Cart;
use Bree\Cart\Cart;
use Bree\Product\Box;
use Bree\Exceptions\BoxNotFoundException;
class MaybeAddBoxesToCart
{
protected Box $box;
protected Cart $cart;
public function __construct(Box $box, Cart $cart)
{
$this->box = $box;
$this->cart = $cart;
}
public function __invoke()
{
add_action('bree_after_item_added_to_cart', [$this, 'process']);
}
public function process(array $item)
{
try {
$box = $this->box->resolveFromItemArray($item);
$success = $this->cart->addBox($box);
if ($success) {
do_action('bree_box_added_to_cart', $box);
}
} catch (BoxNotFoundException $exception) {
//
} catch (\Exception $exception) {
// Handle other exception
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment