Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent search by postal code + filters #25

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,33 @@ public function display_ss_pickup_points($method, $index)
($method_id == 'smart_send_shipping') &&
($chosen_shipping == $shipping_id) &&
(stripos($meta_data['smart_send_shipping_method'], 'agent') !== false)) {

if (!empty($_POST['s_country']) && !empty($_POST['s_postcode']) && !empty($_POST['s_address'])) {
$country = wc_clean($_POST['s_country']);
$postal_code = wc_clean($_POST['s_postcode']);
$city = (!empty($_POST['s_city']) ? wc_clean($_POST['s_city']) : null);//not required but preferred
$street = wc_clean($_POST['s_address']);


$agentSearchAddress = apply_filters('smart_send_agent_search_address', [
'country' => empty($_POST['s_country']) ? null : wc_clean($_POST['s_country']),
'postal_code' => empty($_POST['s_postcode']) ? null : wc_clean($_POST['s_postcode']),
'city' => empty($_POST['s_city']) ? null : wc_clean($_POST['s_city']),
'address' => empty($_POST['s_address']) ? null : wc_clean($_POST['s_address']),
]);

if ($agentSearchAddress['country'] && $agentSearchAddress['postal_code']) {
$carrier = SS_SHIPPING_WC()->get_shipping_method_carrier($meta_data['smart_send_shipping_method']);

$ss_agents = $this->find_closest_agents_by_address($carrier, $country, $postal_code, $city, $street);
// Production API does not allow street addresses shorter than 5 characters
if ($agentSearchAddress['address'] && strlen($agentSearchAddress['address']) >= 5) {
$ss_agents = $this->find_closest_agents_by_address(
$carrier,
$agentSearchAddress['country'],
$agentSearchAddress['postal_code'],
$agentSearchAddress['city'],
$agentSearchAddress['address']
);
} else {
$ss_agents = $this->find_closest_agents_by_postal_code(
$carrier,
$agentSearchAddress['country'],
$agentSearchAddress['postal_code']
);
}

if (!empty($ss_agents)) {

Expand Down Expand Up @@ -145,6 +162,35 @@ public function find_closest_agents_by_address($carrier, $country, $postal_code,
}
}

/**
* Find the closest agents by postal code
*
* @param $carrier string Unique carrier code
* @param $country string ISO3166-A2 Country code
* @param $postal_code string
*
* @return array
*/
public function find_closest_agents_by_postal_code($carrier, $country, $postal_code)
{
SS_SHIPPING_WC()->log_msg('Called "findClosestAgentByPostalCode" for website ' . SS_SHIPPING_WC()->get_website_url() . ' with carrier = "' . $carrier . '", country = "' . $country . '", postcode = "' . $postal_code . '"');

if (SS_SHIPPING_WC()->get_api_handle()->findClosestAgentByPostalCode($carrier, $country, $postal_code)) {

$ss_agents = SS_SHIPPING_WC()->get_api_handle()->getData();

SS_SHIPPING_WC()->log_msg('Response from "findClosestAgentByPostalCode": ' . SS_SHIPPING_WC()->get_api_handle()->getResponseBody());
// Save all of the agents in sessions
WC()->session->set('ss_shipping_agents', $ss_agents);

return $ss_agents;
} else {
SS_SHIPPING_WC()->log_msg( 'Response from "findClosestAgentByPostalCode": ' . SS_SHIPPING_WC()->get_api_handle()->getErrorString() );

return array();
}
}

/**
* Get the formatted address to display on the frontend
*/
Expand Down
2 changes: 1 addition & 1 deletion smart-send-logistics/includes/lib/Smartsend/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,4 @@ private function stripEndpointFromLink($url)
return $url;
}
}
}
}