Last active
December 2, 2020 10:17
-
-
Save hedqvist/30394cdc461a18e7f4d77bb3fbaecc06 to your computer and use it in GitHub Desktop.
Fortnox - Get Stock amount from special stockpoint
This file contains hidden or 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
| <?php | |
| /** | |
| * @snippet WooCommerce - Fortnox plugin by Redlight Media - Hämta lagersaldo från en specifik lagerplats | |
| * @author Redlight Media AB / Christopher Hedqvist | |
| * @compatible WooCommerce 3.5.8 | |
| */ | |
| function redlight_fortnox_warehouse_stock_point_amount ( $DisposableQuantity, $WC_Product, $fortnox_article ) { | |
| $articleNumber = $WC_Product->get_meta( '_obj_fortnox_article_number', true ); | |
| //Vår Lagerplats | |
| $stockpoint = 'LAGER'; | |
| //Ange vår url, där q= LAGER är vår StockPointCode | |
| $url = sprintf( | |
| 'https://api.fortnox.se/api/warehouse/stockpoints-v1/itemsummary/%1$s?q=%2$s', | |
| $articleNumber, | |
| $stockpoint | |
| ); | |
| $response = wp_remote_get( | |
| $url, | |
| [ | |
| 'headers' => [ | |
| 'Client-Secret' => OBJ_FORTNOX_CLIENT_SECRET, | |
| 'Access-Token' => esc_attr(get_option('obj_fortnox_access_token')), | |
| ], | |
| ] | |
| ); | |
| Obj_Fortnox_Product_Metabox::log(sprintf('Getting stock for %s (%s) from stockPointCode: %s',$WC_Product->get_name(), $articleNumber, $stockpoint)); | |
| //Wait here to we dont spam Fortnox | |
| usleep(100000); | |
| $response_code = wp_remote_retrieve_response_code( $response ); | |
| $body = json_decode(wp_remote_retrieve_body( $response ),true); | |
| //Obj_Fortnox_Product_Metabox::log(wc_print_r($response,true)); | |
| if ( $response_code == 200 ) { | |
| Obj_Fortnox_Product_Metabox::log('Getting stock from stockPointCode was successful our availableQuantity is: '.$body[0]['availableQuantity']); | |
| return $body[0]['availableQuantity']; | |
| }else{ | |
| Obj_Fortnox_Product_Metabox::log('Getting stock from stockPointCode FAILED: '.wp_json_encode($body)); | |
| return 0; | |
| } | |
| } | |
| add_filter('obj_fortnox_product_stock_amount', 'redlight_fortnox_warehouse_stock_point_amount', 10,3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment