File "class-wc-product-variation.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/woocommerce/includes/class-wc-product-variation.php
File size: 10.68 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

defined('ABSPATH') || exit;
class WC_Product_Variation extends WC_Product_Simple
{
    protected $post_type = 'product_variation';
    protected $parent_data = array('title' => '', 'sku' => '', 'manage_stock' => '', 'backorders' => '', 'stock_quantity' => '', 'weight' => '', 'length' => '', 'width' => '', 'height' => '', 'tax_class' => '', 'shipping_class_id' => '', 'image_id' => '', 'purchase_note' => '');
    public function __construct($product = 0)
    {
        $this->data['tax_class'] = 'parent';
        $this->data['attribute_summary'] = '';
        parent::__construct($product);
    }
    protected function get_hook_prefix()
    {
        return 'woocommerce_product_variation_get_';
    }
    public function get_type()
    {
        return 'variation';
    }
    public function get_stock_managed_by_id()
    {
        return 'parent' === $this->get_manage_stock() ? $this->get_parent_id() : $this->get_id();
    }
    public function get_title()
    {
        return apply_filters('woocommerce_product_title', $this->parent_data['title'], $this);
    }
    public function get_formatted_name()
    {
        if ($this->get_sku()) {
            $identifier = $this->get_sku();
        } else {
            $identifier = '#' . $this->get_id();
        }
        $formatted_variation_list = wc_get_formatted_variation($this, true, true, true);
        return sprintf('%2$s (%1$s)', $identifier, $this->get_name()) . '<span class="description">' . $formatted_variation_list . '</span>';
    }
    public function get_variation_attributes($with_prefix = true)
    {
        $attributes = $this->get_attributes();
        $variation_attributes = array();
        $prefix = $with_prefix ? 'attribute_' : '';
        foreach ($attributes as $key => $value) {
            $variation_attributes[$prefix . $key] = $value;
        }
        return $variation_attributes;
    }
    public function get_attribute($attribute)
    {
        $attributes = $this->get_attributes();
        $attribute = sanitize_title($attribute);
        if (isset($attributes[$attribute])) {
            $value = $attributes[$attribute];
            $term = taxonomy_exists($attribute) ? get_term_by('slug', $value, $attribute) : false;
            return !is_wp_error($term) && $term ? $term->name : $value;
        }
        $att_str = 'pa_' . $attribute;
        if (isset($attributes[$att_str])) {
            $value = $attributes[$att_str];
            $term = taxonomy_exists($att_str) ? get_term_by('slug', $value, $att_str) : false;
            return !is_wp_error($term) && $term ? $term->name : $value;
        }
        return '';
    }
    public function get_permalink($item_object = null)
    {
        $url = get_permalink($this->get_parent_id());
        if (!empty($item_object['variation'])) {
            $data = $item_object['variation'];
        } elseif (!empty($item_object['item_meta_array'])) {
            $data_keys = array_map('wc_variation_attribute_name', wp_list_pluck($item_object['item_meta_array'], 'key'));
            $data_values = wp_list_pluck($item_object['item_meta_array'], 'value');
            $data = array_intersect_key(array_combine($data_keys, $data_values), $this->get_variation_attributes());
        } else {
            $data = $this->get_variation_attributes();
        }
        $data = array_filter($data, 'wc_array_filter_default_attributes');
        if (empty($data)) {
            return $url;
        }
        $data = array_map('urlencode', $data);
        $keys = array_map('urlencode', array_keys($data));
        return add_query_arg(array_combine($keys, $data), $url);
    }
    public function add_to_cart_url()
    {
        $url = $this->is_purchasable() ? remove_query_arg('added-to-cart', add_query_arg(array('variation_id' => $this->get_id(), 'add-to-cart' => $this->get_parent_id()), $this->get_permalink())) : $this->get_permalink();
        return apply_filters('woocommerce_product_add_to_cart_url', $url, $this);
    }
    public function get_sku($context = 'view')
    {
        $value = $this->get_prop('sku', $context);
        if ('view' === $context && empty($value)) {
            $value = apply_filters($this->get_hook_prefix() . 'sku', $this->parent_data['sku'], $this);
        }
        return $value;
    }
    public function get_weight($context = 'view')
    {
        $value = $this->get_prop('weight', $context);
        if ('view' === $context && empty($value)) {
            $value = apply_filters($this->get_hook_prefix() . 'weight', $this->parent_data['weight'], $this);
        }
        return $value;
    }
    public function get_length($context = 'view')
    {
        $value = $this->get_prop('length', $context);
        if ('view' === $context && empty($value)) {
            $value = apply_filters($this->get_hook_prefix() . 'length', $this->parent_data['length'], $this);
        }
        return $value;
    }
    public function get_width($context = 'view')
    {
        $value = $this->get_prop('width', $context);
        if ('view' === $context && empty($value)) {
            $value = apply_filters($this->get_hook_prefix() . 'width', $this->parent_data['width'], $this);
        }
        return $value;
    }
    public function get_height($context = 'view')
    {
        $value = $this->get_prop('height', $context);
        if ('view' === $context && empty($value)) {
            $value = apply_filters($this->get_hook_prefix() . 'height', $this->parent_data['height'], $this);
        }
        return $value;
    }
    public function get_tax_class($context = 'view')
    {
        $value = null;
        if (array_key_exists('tax_class', $this->data)) {
            $value = array_key_exists('tax_class', $this->changes) ? $this->changes['tax_class'] : $this->data['tax_class'];
            if ('edit' !== $context && 'parent' === $value) {
                $value = $this->parent_data['tax_class'];
            }
            if ('view' === $context) {
                $value = apply_filters($this->get_hook_prefix() . 'tax_class', $value, $this);
            }
        }
        return $value;
    }
    public function get_manage_stock($context = 'view')
    {
        $value = $this->get_prop('manage_stock', $context);
        if ('view' === $context && false === $value && true === wc_string_to_bool($this->parent_data['manage_stock'])) {
            $value = 'parent';
        }
        return $value;
    }
    public function get_stock_quantity($context = 'view')
    {
        $value = $this->get_prop('stock_quantity', $context);
        if ('view' === $context && 'parent' === $this->get_manage_stock()) {
            $value = apply_filters($this->get_hook_prefix() . 'stock_quantity', $this->parent_data['stock_quantity'], $this);
        }
        return $value;
    }
    public function get_backorders($context = 'view')
    {
        $value = $this->get_prop('backorders', $context);
        if ('view' === $context && 'parent' === $this->get_manage_stock()) {
            $value = apply_filters($this->get_hook_prefix() . 'backorders', $this->parent_data['backorders'], $this);
        }
        return $value;
    }
    public function get_image_id($context = 'view')
    {
        $image_id = $this->get_prop('image_id', $context);
        if ('view' === $context && !$image_id) {
            $image_id = apply_filters($this->get_hook_prefix() . 'image_id', $this->parent_data['image_id'], $this);
        }
        return $image_id;
    }
    public function get_purchase_note($context = 'view')
    {
        $value = $this->get_prop('purchase_note', $context);
        if ('view' === $context && empty($value)) {
            $value = apply_filters($this->get_hook_prefix() . 'purchase_note', $this->parent_data['purchase_note'], $this);
        }
        return $value;
    }
    public function get_shipping_class_id($context = 'view')
    {
        $shipping_class_id = $this->get_prop('shipping_class_id', $context);
        if ('view' === $context && !$shipping_class_id) {
            $shipping_class_id = apply_filters($this->get_hook_prefix() . 'shipping_class_id', $this->parent_data['shipping_class_id'], $this);
        }
        return $shipping_class_id;
    }
    public function get_catalog_visibility($context = 'view')
    {
        return apply_filters($this->get_hook_prefix() . 'catalog_visibility', $this->parent_data['catalog_visibility'], $this);
    }
    public function get_attribute_summary($context = 'view')
    {
        return $this->get_prop('attribute_summary', $context);
    }
    public function set_attribute_summary($attribute_summary)
    {
        $this->set_prop('attribute_summary', $attribute_summary);
    }
    public function set_parent_data($parent_data)
    {
        $parent_data = wp_parse_args($parent_data, array('title' => '', 'status' => '', 'sku' => '', 'manage_stock' => 'no', 'backorders' => 'no', 'stock_quantity' => '', 'weight' => '', 'length' => '', 'width' => '', 'height' => '', 'tax_class' => '', 'shipping_class_id' => 0, 'image_id' => 0, 'purchase_note' => '', 'catalog_visibility' => 'visible'));
        $parent_data['tax_class'] = sanitize_title($parent_data['tax_class']);
        $parent_data['tax_class'] = 'standard' === $parent_data['tax_class'] ? '' : $parent_data['tax_class'];
        $valid_classes = $this->get_valid_tax_classes();
        if (!in_array($parent_data['tax_class'], $valid_classes, true)) {
            $parent_data['tax_class'] = '';
        }
        $this->parent_data = $parent_data;
    }
    public function get_parent_data()
    {
        return $this->parent_data;
    }
    public function set_attributes($raw_attributes)
    {
        $raw_attributes = (array) $raw_attributes;
        $attributes = array();
        foreach ($raw_attributes as $key => $value) {
            if (0 === strpos($key, 'attribute_')) {
                $key = substr($key, 10);
            }
            $attributes[$key] = $value;
        }
        $this->set_prop('attributes', $attributes);
    }
    public function has_attributes()
    {
        return false;
    }
    public function is_purchasable()
    {
        return apply_filters('woocommerce_variation_is_purchasable', $this->variation_is_visible() && parent::is_purchasable() && ('publish' === $this->parent_data['status'] || current_user_can('edit_post', $this->get_parent_id())), $this);
    }
    public function variation_is_active()
    {
        return apply_filters('woocommerce_variation_is_active', true, $this);
    }
    public function variation_is_visible()
    {
        return apply_filters('woocommerce_variation_is_visible', 'publish' === get_post_status($this->get_id()) && '' !== $this->get_price(), $this->get_id(), $this->get_parent_id(), $this);
    }
    protected function get_valid_tax_classes()
    {
        $valid_classes = WC_Tax::get_tax_class_slugs();
        $valid_classes[] = 'parent';
        return $valid_classes;
    }
}