'required:if_empty,productsIds|array', // required only if productsIds empty // 'productsIds' => 'required:if_empty,productsCategories|array', // required only if productsCategories empty 'productsCategories' => 'requiredItem:if_empty,productsIds|array|bail', // required only if productsIds empty 'productsIds' => 'requiredItem:if_empty,productsCategories|array|bail', // required only if productsCategories empty 'fieldCode' => 'required|array', 'fieldName' => 'array', 'fieldText' => 'array', 'fieldText2' => 'array', 'format' => 'required', 'hideCode' => 'boolean', 'withVariations' => 'boolean', ); protected $getItemsMethods = array( true => array( true => 'getItemsForCategoriesWithVariations', false => 'getItemsForCategories', ), false => array( true => 'getItemsForProductsWithVariations', false => 'getItemsForProducts', ), ); /** * WoocommercePostsA4BarcodesMaker constructor. */ public function __construct() { parent::__construct(); // Check if woocommerce plugin active if (is_plugin_active('woocommerce/woocommerce.php')) { $this->currency = html_entity_decode(get_woocommerce_currency_symbol()); } } /** * {@inheritdoc}. */ protected function getItems() { // Get params to determine which get items method should be used $forCategories = isset(PostData::$postData['productsCategories']); $withVariations = !empty(PostData::$postData['withVariations']); $getItemsMethod = $this->getItemsMethods[$forCategories][$withVariations]; $this->items = $this->$getItemsMethod(); } /** * Get items for categories ids with variations. * * @return array */ protected function getItemsForCategoriesWithVariations() { // Get data from request $productsCategories = isset(PostData::$postData['productsCategories']) ? PostData::$postData['productsCategories'] : null; // Get all variations and products without variations in given categories $products = a4bGetPostsByCategories($productsCategories); $variations = a4bGetPosts(array('post_type' => 'product_variation', 'post_parent__in' => a4bObjectsFieldToArray($products, 'ID'))); $productsWithoutVariations = a4bExcludePostsByIds($products, a4bObjectsFieldToArray($variations, 'post_parent')); return array_merge($productsWithoutVariations, $variations); } /** * Get items for categories ids. * * @return array */ protected function getItemsForCategories() { // Get data from request $productsCategories = isset(PostData::$postData['productsCategories']) ? PostData::$postData['productsCategories'] : null; // Get only products in given categories return a4bGetPostsByCategories($productsCategories); } /** * Get items for products ids with variations. * * @return array */ protected function getItemsForProductsWithVariations() { // Get data from request $productsIds = isset(PostData::$postData['productsIds']) ? PostData::$postData['productsIds'] : null; $products = a4bGetPosts(array('post__in' => $productsIds)); $variations = a4bGetPosts(array('post_type' => 'product_variation', 'post_parent__in' => $productsIds)); $productsWithoutVariations = a4bExcludePostsByIds($products, a4bObjectsFieldToArray($variations, 'post_parent')); return array_merge($productsWithoutVariations, $variations); } /** * Get items for products. * * @return array */ protected function getItemsForProducts() { // Get data from request $productsIds = isset(PostData::$postData['productsIds']) ? PostData::$postData['productsIds'] : null; return a4bGetPosts(array('post__in' => $productsIds)); } /** * {@inheritdoc}. */ protected function getFileOptions($post, $algorithm, $showCode) { // Check if we need generate barcodes for each item in stock. if ( isset($_POST['useStockQuantity']) && 'true' === $_POST['useStockQuantity'] && 'yes' === get_post_meta($post->ID, '_manage_stock', true) // Woocommerce 'Manage stock' option checkeds ) { $quantity = get_post_meta($post->ID, '_stock', true); // If quantity is not set(null or ''), then use 1. $quantity = (empty($quantity) || '0' === $quantity) ? 0 : (int) $quantity; } elseif ( 'outofstock' === get_post_meta($post->ID, '_stock_status', true) && 'true' === $_POST['useStockQuantity'] ) { // set quantity for out of stock $quantity = 0; } else { // Default, generate one barcode. $quantity = 1; } return array( 'quantity' => $quantity, 'field_code' => $this->getField($post, PostData::$postData['fieldCode']), // the field from which the barcode will be created 'field_name' => $this->getField($post, PostData::$postData['fieldName']), // additional field 'field_text' => $this->getField($post, PostData::$postData['fieldText']), // additional field 'field_text_2' => $this->getField($post, PostData::$postData['fieldText2']), // additional field 'algorithm' => $algorithm, // barcode algorithm 'showCode' => $showCode, // show/hide the code value on the image 'showName' => $this->showName, // show/hide the name value on the image 'showText' => $this->showText, // show/hide the text value on the image 'showText2' => $this->showText2, ); } /** * Get filed value from array field. * * @param array $data * @param array $field * * @return string */ protected function getField($post, $field) { // Check and return 'value' by 'type' switch ($field['type']) { case 'standart': $value = isset($post->{$field['value']}) ? $post->{$field['value']} : ''; break; case 'static': $value = $field['value']; break; case 'permalink': $value = get_post_permalink($post->ID); break; case 'permalink_admin': $value = get_edit_post_link($post->ID, ''); break; case 'wc_taxonomy': $value = $this->getWoocommerceProductTerms($post, $field['value']); break; case 'custom': $value = $this->shouldAddCurrency($field) ? $this->currency.get_post_meta($post->ID, $field['value'], true) : get_post_meta($post->ID, $field['value'], true); break; default: $value = ''; } return (string) $value; } /** * Check if currency should be added. * * @param $field * * @return bool */ protected function shouldAddCurrency($field) { return '_price' === $field['value']; } /** * Get woocommerce product terms by taxonomy. * * @param $post * @param $taxonomy * * @return mixed|string */ protected function getWoocommerceProductTerms($post, $taxonomy) { // If it is 'product', return product taxonomy terms. if ('product' === $post->post_type) { return $this->termsObjectsToString(get_the_terms($post, 'pa_'.$taxonomy)); } // If it is 'product_variation' return taxonomy term if not empty, otherwise list of parent product taxonomy terms. if ('product_variation' === $post->post_type) { return get_term_by( 'slug', get_post_meta($post->ID, 'attribute_pa_'.$taxonomy, true), 'pa_'.$taxonomy )->name ?: $this->termsObjectsToString(get_the_terms($post->post_parent, 'pa_'.$taxonomy)); // All terms: Any value of attribute. } } /** * Creates string of terms names from get terms result. * * @param $terms * * @return string */ protected function termsObjectsToString($terms) { // If terms not empty and not error. if ($terms && !is_wp_error($terms)) { // Get terms names array. $terms = array_map(function ($term) { return $term->name; }, $terms); // Join terms names with ','. return implode(', ', $terms); } // Default value. return ''; } }