WC_Cart::get_item_data PHP Méthode

get_item_data() public méthode

Gets and formats a list of cart item data + variations for display on the frontend.
public get_item_data ( array $cart_item, boolean $flat = false ) : string
$cart_item array
$flat boolean (default: false)
Résultat string
    public function get_item_data($cart_item, $flat = false)
    {
        $item_data = array();
        // Variation values are shown only if they are not found in the title as of 2.7.
        // This is because variation titles display the attributes.
        if ($cart_item['data']->is_type('variation') && is_array($cart_item['variation'])) {
            foreach ($cart_item['variation'] as $name => $value) {
                if ('' === $value || stristr($cart_item['data']->get_name(), $value)) {
                    continue;
                }
                $taxonomy = wc_attribute_taxonomy_name(str_replace('attribute_pa_', '', urldecode($name)));
                // If this is a term slug, get the term's nice name
                if (taxonomy_exists($taxonomy)) {
                    $term = get_term_by('slug', $value, $taxonomy);
                    if (!is_wp_error($term) && $term && $term->name) {
                        $value = $term->name;
                    }
                    $label = wc_attribute_label($taxonomy);
                    // If this is a custom option slug, get the options name
                } else {
                    $value = apply_filters('woocommerce_variation_option_name', $value);
                    $label = wc_attribute_label(str_replace('attribute_', '', $name), $cart_item['data']);
                }
                $item_data[] = array('key' => $label, 'value' => $value);
            }
        }
        // Filter item data to allow 3rd parties to add more to the array
        $item_data = apply_filters('woocommerce_get_item_data', $item_data, $cart_item);
        // Format item data ready to display
        foreach ($item_data as $key => $data) {
            // Set hidden to true to not display meta on cart.
            if (!empty($data['hidden'])) {
                unset($item_data[$key]);
                continue;
            }
            $item_data[$key]['key'] = !empty($data['key']) ? $data['key'] : $data['name'];
            $item_data[$key]['display'] = !empty($data['display']) ? $data['display'] : $data['value'];
        }
        // Output flat or in list format
        if (sizeof($item_data) > 0) {
            ob_start();
            if ($flat) {
                foreach ($item_data as $data) {
                    echo esc_html($data['key']) . ': ' . wp_kses_post($data['display']) . "\n";
                }
            } else {
                wc_get_template('cart/cart-item-data.php', array('item_data' => $item_data));
            }
            return ob_get_clean();
        }
        return '';
    }

Usage Example

 /**
  *
  */
 public function filter_woocommerce_add_to_cart_validation($valid, $product_id, $quantity, $variation_id = '', $variations = '')
 {
     global $woocommerce;
     $product = wc_get_product($product_id);
     if ($product->product_type === "variable") {
         $deductornot = get_post_meta($variation_id, '_deductornot', true);
         $deductamount = get_post_meta($variation_id, '_deductamount', true);
         $getvarclass = new WC_Product_Variation($variation_id);
         //reset($array);
         $aatrs = $getvarclass->get_variation_attributes();
         foreach ($aatrs as $key => $value) {
             $slug = $value;
             $cat = str_replace('attribute_', '', $key);
         }
         $titlevaria = get_term_by('slug', $slug, $cat);
         $backorder = get_post_meta($product->post->ID, '_backorders', true);
         $string = WC_Cart::get_item_data($cart_item, $flat);
         //var_dump($string);
         if ($backorder == 'no') {
             if ($deductornot == "yes") {
                 $currentstock = $product->get_stock_quantity();
                 $reduceamount = intval($quantity) * intval($deductamount);
                 $currentavail = intval($currentstock / $deductamount);
                 if ($reduceamount > $currentstock) {
                     $valid = false;
                     wc_add_notice('' . __('You that goes over our availble stock amount.', 'woocommerce') . __('We have: ', 'woocommerce') . $currentavail . ' ' . $product->post->post_title . ' ' . $titlevaria->name . '\'s ' . __(' available.', 'woocommerce'), 'error');
                     return $valid;
                 } else {
                     $valid = true;
                     return $valid;
                 }
             } else {
                 return true;
             }
         }
     }
     return true;
 }
All Usage Examples Of WC_Cart::get_item_data
WC_Cart