/** * Link all variations via ajax function * * @access public * @return void */ function woocommerce_link_all_variations() { global $woocommerce; if (!defined('WC_MAX_LINKED_VARIATIONS')) { define('WC_MAX_LINKED_VARIATIONS', 49); } check_ajax_referer('link-variations', 'security'); @set_time_limit(0); $post_id = intval($_POST['post_id']); if (!$post_id) { die; } $variations = array(); $_product = get_product($post_id, array('product_type' => 'variable')); // Put variation attributes into an array foreach ($_product->get_attributes() as $attribute) { if (!$attribute['is_variation']) { continue; } $attribute_field_name = 'attribute_' . sanitize_title($attribute['name']); if ($attribute['is_taxonomy']) { $post_terms = wp_get_post_terms($post_id, $attribute['name']); $options = array(); foreach ($post_terms as $term) { $options[] = $term->slug; } } else { $options = explode('|', $attribute['value']); } $options = array_map('sanitize_title', array_map('trim', $options)); $variations[$attribute_field_name] = $options; } // Quit out if none were found if (sizeof($variations) == 0) { die; } // Get existing variations so we don't create duplicates $available_variations = array(); foreach ($_product->get_children() as $child_id) { $child = $_product->get_child($child_id); if (!empty($child->variation_id)) { $available_variations[] = $child->get_variation_attributes(); } } // Created posts will all have the following data $variation_post_data = array('post_title' => 'Product #' . $post_id . ' Variation', 'post_content' => '', 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'post_parent' => $post_id, 'post_type' => 'product_variation'); // Now find all combinations and create posts if (!function_exists('array_cartesian')) { function array_cartesian($input) { $result = array(); while (list($key, $values) = each($input)) { // If a sub-array is empty, it doesn't affect the cartesian product if (empty($values)) { continue; } // Special case: seeding the product array with the values from the first sub-array if (empty($result)) { foreach ($values as $value) { $result[] = array($key => $value); } } else { // Second and subsequent input sub-arrays work like this: // 1. In each existing array inside $product, add an item with // key == $key and value == first item in input sub-array // 2. Then, for each remaining item in current input sub-array, // add a copy of each existing array inside $product with // key == $key and value == first item in current input sub-array // Store all items to be added to $product here; adding them on the spot // inside the foreach will result in an infinite loop $append = array(); foreach ($result as &$product) { // Do step 1 above. array_shift is not the most efficient, but it // allows us to iterate over the rest of the items with a simple // foreach, making the code short and familiar. $product[$key] = array_shift($values); // $product is by reference (that's why the key we added above // will appear in the end result), so make a copy of it here $copy = $product; // Do step 2 above. foreach ($values as $item) { $copy[$key] = $item; $append[] = $copy; } // Undo the side effecst of array_shift array_unshift($values, $product[$key]); } // Out of the foreach, we can add to $results now $result = array_merge($result, $append); } } return $result; } } $variation_ids = array(); $added = 0; $possible_variations = array_cartesian($variations); foreach ($possible_variations as $variation) { // Check if variation already exists if (in_array($variation, $available_variations)) { continue; } $variation_id = wp_insert_post($variation_post_data); $variation_ids[] = $variation_id; foreach ($variation as $key => $value) { update_post_meta($variation_id, $key, $value); } $added++; do_action('product_variation_linked', $variation_id); if ($added > WC_MAX_LINKED_VARIATIONS) { break; } } $woocommerce->clear_product_transients($post_id); echo $added; die; }
function buscarSolucionInicial($tablero, &$sudoku, &$tipos) { $filas = range(0, 8); $columnas = range(0, 8); shuffle($filas); shuffle($columnas); $cross = array_cartesian($filas, $columnas); shuffle($cross); intentarSolucionar($tablero, CORRECTA); foreach ($cross as $pos) { $i = $pos[0]; $j = $pos[1]; if ($tablero[$i][$j]->valor === null) { if (sizeof($tablero[$i][$j]->posibles) > 0) { //imprimir($tablero); $valorRandom = $tablero[$i][$j]->posibles[array_rand($tablero[$i][$j]->posibles)]; if (!existeValorEnCuadrante($tablero, $valorRandom, $i, $j)) { $tablero[$i][$j]->valor = $valorRandom; $tablero[$i][$j]->tipo = RANDOM; intentarSolucionar($tablero, 3); } } else { $posibles = array_diff(range(1, 9), posiblesValoresDelCuadrante($tablero, $i, $j)); //print_r($posibles); foreach ($posibles as $p) { if (!existeValorEnCuadrante($tablero, $p, $i, $j)) { $tablero[$i][$j]->valor = $p; $tablero[$i][$j]->tipo = RELLENO; intentarSolucionar($tablero, 4); //echo "<br>$p $i $j"; } } } } } foreach ($cross as $pos) { $i = $pos[0]; $j = $pos[1]; //HAY QUE CONTROLAR UNA VEZ MAS SI SIGUE NULO Y LLENAR CON CUALQUIERA if ($tablero[$i][$j]->valor === null) { foreach ($filas as $f) { if (!existeValorEnCuadrante($tablero, $f + 1, $i, $j)) { $tablero[$i][$j]->valor = $f + 1; $tablero[$i][$j]->tipo = RELLENO; intentarSolucionar($tablero, 4); } } } $sudoku[$i][$j] = $tablero[$i][$j]->valor; $tipos[$i][$j] = $tablero[$i][$j]->tipo; } }
public function getResult($request) { $vendors = array(); if ($request->getAllItems()) { foreach ($request->getAllItems() as $item) { $product = Mage::getModel('catalog/product')->load($item->getProduct()->getId()); $vendorId = (int) $product->getData('sm_product_vendor_id'); if (empty($vendors[$vendorId])) { $vendor = Mage::getModel('smvendors/vendor')->load($vendorId); $vendors[$vendorId] = array('vendor' => $vendor, 'items' => array(), 'shipping_methods' => array()); } $vendors[$vendorId]['items'][] = $item; } } ksort($vendors); $shippingMethods = array(); $i = 0; foreach ($vendors as $vendor) { $availableShippingMethods = $vendor['vendor']->getAvaiableShippingMethods(); $requestVendorClone = clone $request; $requestVendorClone->setAllItems($vendor['items']); $requestVendorClone->setLimitCarrier(array($this->_code)); $requestVendorClone->setVendorId($vendor['vendor']->getId()); $packageQty = 0; $packageWeight = 0; foreach ($vendor['items'] as $item) { $packageQty += $item->getQty(); $packageWeight += $item->getWeight(); } $requestVendorClone->setPackageQty($packageQty); $requestVendorClone->setPackageWeight($packageWeight); $shipping = Mage::getModel('shipping/shipping')->collectRates2($requestVendorClone); foreach ($shipping->getResult()->getAllRates() as $rate) { if (in_array($rate->getCarrier(), $availableShippingMethods)) { $vendors[$vendor['vendor']->getId()]['shipping_methods'][] = $rate; if (empty($shippingMethods[$i])) { $shippingMethods[$i] = array(); } // if(!in_array($title,$shippingArr[$i])){ $shippingMethods[$i][] = array('vendor' => $vendor['vendor'], 'rate' => $rate); // } } } $i++; } if (!function_exists('array_cartesian')) { function array_cartesian($arrays) { $result = array(); $keys = array_keys($arrays); $reverse_keys = array_reverse($keys); $size = intval(count($arrays) > 0); foreach ($arrays as $array) { $size *= count($array); } for ($i = 0; $i < $size; $i++) { $result[$i] = array(); foreach ($keys as $j) { $result[$i][$j] = current($arrays[$j]); } foreach ($reverse_keys as $j) { if (next($arrays[$j])) { break; } elseif (isset($arrays[$j])) { reset($arrays[$j]); } } } return $result; } } $shippingMethodsCartesian = array_cartesian($shippingMethods); $methods = Mage::getModel('shipping/rate_result'); foreach ($shippingMethodsCartesian as $shippingRates) { $methodTitle = array(); $methodCode = array(); $methodCost = 0; $methodPrice = 0; $methodDetail = array(); foreach ($shippingRates as $rate) { $methodTitle[] = $rate['vendor']->getVendorName() . ' ' . $rate['rate']->getCarrierTitle() . ' - ' . $rate['rate']->getMethodTitle(); $methodCode[] = 'v' . $rate['vendor']->getId() . '_' . $rate['rate']->getCarrier() . '_' . $rate['rate']->getMethod(); $methodPrice += $rate['rate']->getPrice(); $methodCost += $rate['rate']->getCost(); $methodDetail[$rate['vendor']->getId()] = array('method' => $rate['rate']->getData(), 'vendor' => $rate['vendor']->getData()); } $methodTitle = implode('<br/>', $methodTitle); $methodCode = implode('|', $methodCode); $method = Mage::getModel('shipping/rate_result_method'); $method->setCarrier($this->_code); $method->setCarrierTitle(Mage::getStoreConfig('carriers/' . $this->_code . '/title')); $method->setMethod('dropshipping'); $method->setMethodTitle($methodTitle); $method->setMethod($methodCode); $method->setCost($methodCost); $method->setPrice($methodPrice); $method->setData('shipping_rates_by_vendors', $vendors); // set shipping method detail for each shipping rate $methodDetail = serialize($methodDetail); $method->setMethodDetail($methodDetail); $methods->append($method); } return $methods; }
public function tag_group($tags = array()) { $tag_length = count($tags); function array_cartesian($arr = array()) { $_ = $arr; //func_get_args(); if (count($_) == 0) { return array(array()); } $a = array_shift($_); $c = call_user_func_array(__FUNCTION__, array($_)); $r = array(); if (is_array($a)) { foreach ($a as $v) { foreach ($c as $p) { $r[] = array_merge(array($v), $p); } } } return $r; } echo '<pre>'; //array_cartesian($tags, $tags); $cross = array_cartesian(array($tags, $tags)); /*$prep = array($tags); for($x = 0; $x < $tag_length; $x++) { $prep[] = $tags; //print_r($prep); $cross[] = array_cartesian($prep); }*/ foreach ($cross as $k => $v) { $len = count($cross[$k]); $cross[$k] = array_unique($cross[$k]); if (count($cross[$k]) < $len) { unset($cross[$k]); } } print_r($cross); return $cross; }