Skip to content

Instantly share code, notes, and snippets.

@mrpsiho
Last active March 5, 2018 17:59
Show Gist options
  • Save mrpsiho/ddd09acf639ab486ca368461c80d68ff to your computer and use it in GitHub Desktop.
Save mrpsiho/ddd09acf639ab486ca368461c80d68ff to your computer and use it in GitHub Desktop.
ShipperHQ for WC (ver. 1.3.2 https://wordpress.org/plugins/woo-shipperhq/). Replace 'populateAttributes' method with one mentioned below in order to hook in and read cart item dimensions instead of static ones added to a product. It makes ShipperHQ compatible with UniCPO 4 plugin.
/**
* Reads attributes from the item
*
* @param $productData
* @param $product
* @return array
*/
protected function populateAttributes($productData, $product)
{
$attributes = [];
if($product) {
//SHQ16-2202 use the $product as this is the parent or child product for configurable
$productId = $product->get_id();
foreach (self::$_stdAttributeNames as $attributeName) {
$internalAttributeName = "_".$attributeName;
$customAttributeValue = apply_filters(
'shipperhq_custom_product_attribute_value',
get_post_meta($productId, $internalAttributeName, true ),
$attributeName,
$productId,
$productData
);
if (!is_null($customAttributeValue) && !empty($customAttributeValue) &&
!strstr($customAttributeValue, 'NONE')) {
$customAttributeValue = str_replace(',' , '#', $customAttributeValue);
$attributes[] = [
'name' => $attributeName,
'value' => $customAttributeValue
];
}
}
//SHQ16-2202 pass in correct productId for parent/child correct retrieval
$this->populateMetaData($productId, $attributes);
}
return $attributes;
}
@mrpsiho
Copy link
Author

mrpsiho commented Mar 5, 2018

@shipperhq ShipperHQ is a great service for calculating shipping rates. Unfortunately, it has to be tweaked a little bit in order to read dimensions from cart items, which can be set dynamically, instead of static values defined for a product. It does it for 'weight' but DOES NOT for 'width', 'height' and 'length'. So, I actually offer to add a filter so it will be possible to hook in and change the mentioned attributes dynamically.

It adds 'shipperhq_custom_product_attribute_value' filter.

The name of the filter can be changed. Specifically, this filter name is used to integrate ShipperHQ with UniCPO (https://wordpress.org/plugins/uni-woo-custom-product-options/) and add calculated dimensions to ShipperHQ and get shipping rates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment