| 1 |
<?php |
| 2 |
/** |
| 3 |
* Product_Helpers. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\API\Traits; |
| 9 |
|
| 10 |
use WC_Product; |
| 11 |
use WC_Product_Variation; |
| 12 |
use WCPOS\WooCommercePOS\Logger; |
| 13 |
|
| 14 |
trait Product_Helpers { |
| 15 |
/** |
| 16 |
* Get custom barcode postmeta. |
| 17 |
* |
| 18 |
* @param WC_Product|WC_Product_Variation $object The product object. |
| 19 |
* |
| 20 |
* @return string |
| 21 |
*/ |
| 22 |
public function wcpos_get_barcode( $object ) { |
| 23 |
$barcode_field = $this->wcpos_get_barcode_field(); |
| 24 |
|
| 25 |
if ( '_sku' === $barcode_field ) { |
| 26 |
return $object->get_sku(); |
| 27 |
} |
| 28 |
if ( '_global_unique_id' === $barcode_field ) { |
| 29 |
return $object->get_global_unique_id(); |
| 30 |
} |
| 31 |
|
| 32 |
return $object->get_meta( $barcode_field ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get barcode field from settings. |
| 37 |
* |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public function wcpos_get_barcode_field() { |
| 41 |
$barcode_field = woocommerce_pos_get_settings( 'general', 'barcode_field' ); |
| 42 |
|
| 43 |
// Check for WP_Error. |
| 44 |
if ( is_wp_error( $barcode_field ) ) { |
| 45 |
Logger::log( 'Error retrieving barcode_field: ' . $barcode_field->get_error_message() ); |
| 46 |
|
| 47 |
return ''; |
| 48 |
} |
| 49 |
|
| 50 |
// Check for non-string values. |
| 51 |
if ( ! \is_string( $barcode_field ) ) { |
| 52 |
Logger::log( 'Unexpected data type for barcode_field. Expected string, got: ' . \gettype( $barcode_field ) ); |
| 53 |
|
| 54 |
return ''; |
| 55 |
} |
| 56 |
|
| 57 |
return $barcode_field; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get barcode field from settings. |
| 62 |
* |
| 63 |
* @return bool |
| 64 |
*/ |
| 65 |
public function wcpos_pos_only_products_enabled() { |
| 66 |
$pos_only_products_enabled = woocommerce_pos_get_settings( 'general', 'pos_only_products' ); |
| 67 |
|
| 68 |
// Check for WP_Error. |
| 69 |
if ( is_wp_error( $pos_only_products_enabled ) ) { |
| 70 |
Logger::log( 'Error retrieving pos_only_products: ' . $pos_only_products_enabled->get_error_message() ); |
| 71 |
|
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
// Make sure it's true, just in case there's a corrupt setting. |
| 76 |
return true === $pos_only_products_enabled; |
| 77 |
} |
| 78 |
} |
| 79 |
|