/** * Process shipping method and save * * @param EventObserver $observer * @return void */ public function execute(EventObserver $observer) { if ($this->shipperDataHelper->getConfigValue('carriers/shipper/active')) { $request = $observer->getEvent()->getRequest(); $shippingMethods = $request->getPost('shipping_method', ''); if (!is_array($shippingMethods)) { return; } foreach ($shippingMethods as $addressId => $shippingMethod) { if (empty($shippingMethod)) { return; } $quote = $observer->getEvent()->getQuote(); $addresses = $quote->getAllShippingAddresses(); $shippingAddress = false; foreach ($addresses as $address) { if ($address->getId() == $addressId) { $shippingAddress = $address; break; } } $this->carrierGroupHelper->saveCarrierGroupInformation($shippingAddress, $shippingMethod); } } }
/** * Update saved shipping methods available for ShipperHQ * * @param EventObserver $observer * @return void */ public function execute(EventObserver $observer) { if ($this->shipperDataHelper->getConfigValue('carriers/shipper/active')) { $refreshResult = $this->shipperCarrier->refreshCarriers(); if (array_key_exists('error', $refreshResult)) { $message = __($refreshResult['error']); $this->messageManager->addError($message); } else { $message = __('%1 carriers have been updated from ShipperHQ', count($refreshResult)); $this->messageManager->addSuccess($message); } } }
/** * Record order shipping information after order is placed * * @param EventObserver $observer * @return void */ public function execute(EventObserver $observer) { if ($this->shipperDataHelper->getConfigValue('carriers/shipper/active')) { $requestData = $observer->getRequestModel()->getPost(); if (isset($requestData['order'])) { $orderData = $requestData['order']; } $quote = $observer->getSession()->getQuote(); //if(!empty($orderData['shipping_method_flag'])) if (!empty($orderData['shipping_method'])) { $shippingMethod = $orderData['shipping_method']; $this->carrierGroupHelper->saveCarrierGroupInformation($quote->getShippingAddress(), $shippingMethod); } //} } }
public function toOptionArray() { $arr = []; $carriers = $this->shippingConfig->getAllCarriers($this->storeManager->getStore()); foreach ($carriers as $carrierCode => $carrierModel) { $carrierTitle = $this->shipperDataHelper->getConfigValue('carriers/' . $carrierCode . '/title'); if (strpos($carrierCode, 'shipper') === 0 || $carrierTitle == '') { continue; } if ($this->shipperDataHelper->getConfigValue('carriers/' . $carrierCode . '/model') == 'ShipperHQ\\Shipper\\Model\\Carrier\\Shipper') { continue; } $arr[] = ['value' => $carrierCode, 'label' => $carrierTitle]; } array_unshift($arr, ['value' => false, 'label' => __('No Carrier')]); return $arr; }
/** * Save config value to db * @param $path * @param $value * @param string $scope * @param int $scopeId * @return $this */ public function saveConfig($path, $value, $scope = 'default', $scopeId = 0, $refreshRequired = true) { if ($this->shipperDataHelper->getConfigValue($path) != $value) { $this->resourceConfig->saveConfig(rtrim($path, '/'), $value, $scope, $scopeId); if ($refreshRequired) { $this->shipperDataHelper->getCheckout()->setConfigUpdated(true); } } }
/** * Set up additional attribute array * This takes the values from core_config_data * * Not currently implemented for v1 Magento2. * * @param $item * @return array */ protected function getCustomAttributes($item) { $rawCustomAttributes = explode(',', $this->shipperDataHelper->getConfigValue('carriers/shipper/item_attributes')); $customAttributes = []; foreach ($rawCustomAttributes as $attribute) { $attribute = str_replace(' ', '', $attribute); if (!in_array($attribute, self::$stdAttributeNames) && !in_array($attribute, self::$legacyAttributeNames) && $attribute != '') { $customAttributes[] = $attribute; } } return self::populateAttributes($customAttributes, $item); }
protected function compareAttributeData($latestAttributes) { $result = array(); $productAttributes = $this->shipperDataHelper->getProductAttributes(); foreach ($latestAttributes as $attribute) { switch ($attribute->type) { case 'product': try { $existingAttributeOptions = array(); if (!in_array($attribute->code, $productAttributes)) { $this->shipperLogger->postDebug('Shipperhq_Shipper', 'Attribute ' . $attribute->code . ' does not exist.', ''); continue; } $existingAttributeInfo = $this->attributeOptionManagement->getItems($attribute->code); if (is_array($existingAttributeInfo)) { $existingAttributeOptions = $existingAttributeInfo; } } catch (\Exception $e) { $e->getMessage(); $this->shipperLogger->postDebug('Shipperhq_Shipper', 'Unable to find attribute ' . $attribute->code, 'Error: ' . $e->getMessage()); $result = false; break; } $trackValues = $existingAttributeOptions; foreach ($attribute->attributes as $latestValue) { $found = false; foreach ($existingAttributeOptions as $key => $option) { if ($option->getLabel() == $latestValue->name) { $found = true; unset($trackValues[$key]); continue; } } if (!$found) { $result[] = array('attribute_type' => $attribute->type, 'attribute_code' => $attribute->code, 'value' => $latestValue->name, 'status' => self::ADD_ATTRIBUTE_OPTION, 'date_added' => date('Y-m-d H:i:s')); } } if (count($trackValues) > 0) { //TODO add store selector in here $storeId = ''; foreach ($trackValues as $key => $option) { if (ctype_space($option->getLabel()) || $option->getLabel() == '') { unset($trackValues[$key]); continue; } $isAssigned = $this->getIsAttributeValueUsed($attribute->code, $option->getValue(), $storeId, true); $deleteFlag = self::AUTO_REMOVE_ATTRIBUTE_OPTION; if ($isAssigned) { $deleteFlag = self::REMOVE_ATTRIBUTE_OPTION; } $result[] = array('attribute_type' => $attribute->type, 'attribute_code' => $attribute->code, 'value' => $option->getLabel(), 'option_id' => $option->getValue(), 'status' => $deleteFlag, 'date_added' => date('Y-m-d H:i:s')); } } break; case 'global': if ($attribute->code == 'global_settings') { foreach ($attribute->attributes as $globalSetting) { $value = $globalSetting->value == 'true' ? 1 : 0; if ($this->shipperDataHelper->getConfigValue('carriers/shipper/' . $globalSetting->code) != $value) { $result[] = array('attribute_type' => 'global_setting', 'attribute_code' => $globalSetting->code, 'value' => $value, 'option_id' => '', 'status' => self::ADD_ATTRIBUTE_OPTION, 'date_added' => date('Y-m-d H:i:s')); } } } case 'customer': //compare customer groups break; default: break; } } $this->shipperLogger->postDebug('Shipperhq_Shipper', 'Compare attributes result: ', $result); return $result; }
/** * * Build up an error message when no carrier rates returned * @return Mage_Shipping_Model_Rate_Result */ protected function returnGeneralError($message = null) { $result = $this->rateFactory->create(); $error = $this->_rateErrorFactory->create(); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $error->setCarriergroupId(''); if ($message && $this->shipperDataHelper->getConfigValue('carriers/shipper/debug')) { $error->setErrorMessage($message); } else { $error->setErrorMessage($this->getConfigData('specificerrmsg')); } $result->append($error); return $result; }
/** * Define if debugging is enabled * * @return bool * @SuppressWarnings(PHPMD.BooleanGetMethodName) * @api */ public function getDebugFlag() { return $this->shipperDataHelper->getConfigValue('carriers/shipper/debug'); }