Created
March 19, 2021 13:53
-
-
Save chillbits-legacy/86a264dae968cfa930401062dc94d2c6 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 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