Last active
December 28, 2018 08:17
-
-
Save renjith-ph/5c6475f7e7004d0b949e4da96b37d8b3 to your computer and use it in GitHub Desktop.
Snippet to support Addon_Plugin_dividing_single_product.
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
/** | |
* Snippet to support Addon_Plugin_dividing_single_product. | |
* Created at : 24 Dec 2018 | |
* PluginHive Plugins : https://www.pluginhive.com/plugins/ | |
*/ | |
add_filter( 'wf_fedex_commodities', 'wf_fedex_request_update_commodity', 10, 3); | |
function wf_fedex_request_update_commodity($commodities,$request,$fedex_packages) | |
{ | |
$final_product=array(); | |
if(!empty($commodities)) | |
{ | |
$old_commodities=$commodities; | |
foreach ( $fedex_packages as $key => $parcel ) { | |
if ( $parcel['packed_products'] ) { | |
foreach ( $parcel['packed_products'] as $product ) { | |
$product = $this->wf_load_product($product); | |
$product_id=wp_get_post_parent_id($product->get_id()); | |
if(empty($product_id)) | |
{ | |
$product_id=$product->get_id(); | |
} | |
if ( isset( $final_product[ $product->id ] ) ) { | |
if($final_product[$product_id]['width']==$product->width && $final_product[$product_id]['height']==$product->height && $final_product[$product_id]['length']==$product->length && $final_product[$product_id]['weight']==$product->weight ) | |
{ | |
$final_product['$product_id']['quantity']++; | |
$final_product[$product_id]['value']=$this->wf_get_insurance_amount($product); | |
} | |
else{ | |
$final_product[$product_id]['final_weight']+=$product->weight; | |
} | |
continue; | |
} | |
else | |
{ | |
$final_product[$product_id]=array('id'=>$product_id,'weight'=>$product->get_weight(),'height'=>$product->get_height(),'length'=>$product->get_length(),'width'=>$product->get_width(),'quantity'=>1,'Value'=>$this->wf_get_insurance_amount($product),'final_weight'=>$product->weight); | |
} | |
$product_id = $product->get_type() == 'simple' ? $product->get_id() : $product->get_parent_id(); | |
$wf_hs_code = get_post_meta( $product_id , '_wf_hs_code', 1 ); | |
// for backword compatiblity | |
if(!$wf_hs_code){ | |
$product_data = wc_get_product( $product->id ); | |
$wf_hs_code = $product_data->get_attribute( 'wf_hs_code' ); | |
} | |
if( !empty($wf_hs_code) ){ | |
$final_product[ $product->id ]['HarmonizedCode'] = $wf_hs_code; | |
} | |
} | |
} | |
} | |
$commodoties=array(); | |
foreach ($final_product as $key => $value) { | |
if(!isset($old_commodities[$key])) | |
{ | |
continue; | |
} | |
$product = $this->wf_load_product($key); | |
$commodoties[ $key ] = array( | |
'Name' => html_entity_decode( $product->get_title() ), | |
'NumberOfPieces' => 1, | |
'Description' => html_entity_decode( $product->get_title()), | |
'CountryOfManufacture' => ( $country = get_post_meta( $product->id, '_wf_manufacture_country', true ) ) ? $country : $old_commodities[$key]['CountryOfManufacture'], | |
'Weight' => array( | |
'Units' => $old_commodities[$key]['Weight']['Units'], | |
'Value' => round( Ph_Fedex_Woocommerce_Shipping_Common::ph_get_converted_weight( $value['final_weight'], $old_commodities[$key]['Weight']['Units'] ), 2 ) , | |
), | |
'Quantity' => $value['quantity'], | |
'UnitPrice' => array( | |
'Amount' => $old_commodities[$key]['UnitPrice']['Amount'], | |
'Currency' => $old_commodities[$key]['UnitPrice']['Currency'] | |
), | |
'CustomsValue' => array( | |
'Amount' => $old_commodities[$key]['CustomsValue']['Amount'], | |
'Currency' => $old_commodities[$key]['CustomsValue']['Currency'] | |
), | |
'QuantityUnits' => 'EA' | |
); | |
$product_id = $product->get_type() == 'simple' ? $product->get_id() : $product->get_parent_id(); | |
$wf_hs_code = get_post_meta( $product_id , '_wf_hs_code', 1 ); | |
// for backword compatiblity | |
if(!$wf_hs_code){ | |
$product_data = wc_get_product( $product->id ); | |
$wf_hs_code = $product_data->get_attribute( 'wf_hs_code' ); | |
} | |
if( !empty($wf_hs_code) ){ | |
$commodoties[ $product->id ]['HarmonizedCode'] = $wf_hs_code; | |
} | |
} | |
} | |
return $commodoties; | |
} | |
function wf_load_product( $product ){ | |
if( !$product ){ | |
return false; | |
} | |
if( !class_exists('wf_product') ){ | |
include_once('class-wf-legacy.php'); | |
} | |
if($product instanceof wf_product){ | |
return $product; | |
} | |
return new wf_product( $product ); | |
} | |
function wf_get_insurance_amount( $product ) { | |
global $woocommerce; | |
if( $woocommerce->version > 2.7 ) { | |
$parent_id = $product->get_parent_id(); | |
$product_id = ! empty( $parent_id ) ? $parent_id : $product->get_id(); | |
} | |
else { | |
$product_id = ($product instanceof WC_Product_Variable) ? $product->parent->id : $product->id ; | |
} | |
$insured_price = get_post_meta( $product_id, '_wf_fedex_custom_declared_value', true ); | |
return ( ! empty( $insured_price ) ? $insured_price : $product->get_price() ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment