Want to display a message after certain conditions, how to achieve that?
class ShipmentEstimationPlugin
{
public function __construct(
MagentoCustomerModelSession $customerSession
) {
$this->customerSession = $customerSession;
}
public function aroundEstimateByExtendedAddress(
ShipmentEstimationInterface $subject,
Closure $proceed,
$cartId,
MagentoQuoteApiDataAddressInterface $address
) {
$writer = new ZendLogWriterStream(BP . '/var/log/testshipping.log');
$logger = new ZendLogLogger();
$logger->addWriter($writer);
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$cart = $objectManager->get('MagentoCheckoutModelCart');
$itemsCollection = $cart->getQuote()->getItemsCollection();
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
$items = $cart->getQuote()->getAllItems();
$product = $objectManager->create('MagentoCatalogModelProduct');
$brands = array();
foreach($items as $item) {
$loaded_product = $product->load($item->getProductId());
$product = $product->load($item->getProductId());
$brand = null;
$brand=$loaded_product->getResource()->getAttribute('brand')->getFrontend()->getValue($loaded_product);
$brand = strval($brand);
$subTotal = $cart->getQuote()->getSubtotal();
// $items = array($brand);
array_push($brands, $brand);
}
$size = sizeof($brands);
$logger->info($brands);
$logger->info($size);
$pelican = in_array("Pelican",$brands);
$nanuk = in_array("Nanuk", $brands);
$other = in_array("Other", $brands);
$logger->info("Pelican");
$logger->info($pelican);
$logger->info("Nanuk");
$logger->info($nanuk);
$logger->info("Other");
$logger->info($other);
$code=$address->getPostcode();
$region=$address->getRegionId();
$logger->info($code);
$logger->info($region);
$check = stripos($code, '0') === 1;
$logger->info(stripos($code, '0') === 1);
$shippingMethods = $proceed($cartId, $address);
if ($code) {
if ($check ) {
foreach ($shippingMethods as $key => $shippingMethod) {
if($shippingMethod->getMethodCode() == "freeshipping"){
unset($shippingMethods[$key]);
}
}
}
}
if ($region) { // Check is regionid exists in request
$regionid = $region;
if ($regionid == 69 || $regionid == 72 || $regionid == 73 || $regionid == 75 || $regionid == 78) {
foreach ($shippingMethods as $key => $shippingMethod) {
if($shippingMethod->getMethodCode() == "freeshipping"){
unset($shippingMethods[$key]);
}
}
}
}
if( in_array("Pelican",$brands) || in_array("Nanuk",$brands)){
if($subTotal >= 150){
if( in_array("Other", $brands) || in_array("SKB", $brands))
{
if ($regionid == 69 || $regionid == 72 || $regionid == 73 || $regionid == 75 || $regionid == 78 || $check) { foreach ($shippingMethods as $key => $shippingMethod) {
if($shippingMethod->getMethodCode() == "freeshipping"){
unset($shippingMethods[$key]);
}
}
$logger->info("Pelican & Non Pelican found");
$logger->info("You are not eligible for free shipping");
}
else
{
foreach ($shippingMethods as $key => $shippingMethod) {
if($shippingMethod->getMethodCode() == "freeshipping"){
unset($shippingMethods[$key]);
}
}
$logger->info("Pelican & Non Pelican found");
$logger->info("You may be eligible for free shipping");
//not Working
$question = $objectManager->create('MagentoFrameworkMessageManagerInterface');
$question->addWarning(__("Warning"));
//not working
//Want to display message here
// echo "You may be eligible for free shipping. Contact at websales@productioncase.com for you free quote";
}
}
else
{
$logger->info("Only Pelican/Nanuk found");
}
}else{
foreach ($shippingMethods as $key => $shippingMethod) {
if($shippingMethod->getMethodCode() == "freeshipping"){
unset($shippingMethods[$key]);
}
}
}
}
else {
foreach ($shippingMethods as $key => $shippingMethod) {
if($shippingMethod->getMethodCode() == "freeshipping"){
unset($shippingMethods[$key]);
}
}
}
return $shippingMethods;
}
}