Merge pull request #19497 from woocommerce/fix/product-classes-phpcs

Fixed product classes PHPCS violations
This commit is contained in:
Claudiu Lodromanean 2018-03-22 13:22:34 -07:00 committed by GitHub
commit d5e98e8d08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 285 additions and 279 deletions

View File

@ -1,18 +1,19 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Represents a product attribute.
* Represents a product attribute
*
* Attributes can be global (taxonomy based) or local to the product itself.
* Uses ArrayAccess to be BW compatible with previous ways of reading attributes.
*
* @version 3.0.0
* @since 3.0.0
* @package WooCommerce/Classes
* @author WooThemes
* @package WooCommerce/Classes
* @version 3.0.0
* @since 3.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Product attribute class.
*/
class WC_Product_Attribute implements ArrayAccess {
@ -122,12 +123,14 @@ class WC_Product_Attribute implements ArrayAccess {
* @return array
*/
public function get_data() {
return array_merge( $this->data, array(
'is_visible' => $this->get_visible() ? 1 : 0,
'is_variation' => $this->get_variation() ? 1 : 0,
'is_taxonomy' => $this->is_taxonomy() ? 1 : 0,
'value' => $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() ),
) );
return array_merge(
$this->data, array(
'is_visible' => $this->get_visible() ? 1 : 0,
'is_variation' => $this->get_variation() ? 1 : 0,
'is_taxonomy' => $this->is_taxonomy() ? 1 : 0,
'value' => $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() ),
)
);
}
/*
@ -139,7 +142,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set ID (this is the attribute ID).
*
* @param int $value
* @param int $value Attribute ID.
*/
public function set_id( $value ) {
$this->data['id'] = absint( $value );
@ -148,7 +151,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set name (this is the attribute name or taxonomy).
*
* @param int $value
* @param int $value Attribute name.
*/
public function set_name( $value ) {
$this->data['name'] = $value;
@ -157,7 +160,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set options.
*
* @param array $value
* @param array $value Attribute options.
*/
public function set_options( $value ) {
$this->data['options'] = $value;
@ -166,7 +169,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set position.
*
* @param int $value
* @param int $value Attribute position.
*/
public function set_position( $value ) {
$this->data['position'] = absint( $value );
@ -175,7 +178,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set if visible.
*
* @param bool $value
* @param bool $value If is visible on Product's additional info tab.
*/
public function set_visible( $value ) {
$this->data['visible'] = wc_string_to_bool( $value );
@ -184,7 +187,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set if variation.
*
* @param bool $value
* @param bool $value If is used for variations.
*/
public function set_variation( $value ) {
$this->data['variation'] = wc_string_to_bool( $value );
@ -257,26 +260,22 @@ class WC_Product_Attribute implements ArrayAccess {
*/
/**
* offsetGet.
* OffsetGet.
*
* @param string $offset
* @param string $offset Offset.
* @return mixed
*/
public function offsetGet( $offset ) {
switch ( $offset ) {
case 'is_variation' :
case 'is_variation':
return $this->get_variation() ? 1 : 0;
break;
case 'is_visible' :
case 'is_visible':
return $this->get_visible() ? 1 : 0;
break;
case 'is_taxonomy' :
case 'is_taxonomy':
return $this->is_taxonomy() ? 1 : 0;
break;
case 'value' :
case 'value':
return $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() );
break;
default :
default:
if ( is_callable( array( $this, "get_$offset" ) ) ) {
return $this->{"get_$offset"}();
}
@ -286,23 +285,23 @@ class WC_Product_Attribute implements ArrayAccess {
}
/**
* offsetSet.
* OffsetSet.
*
* @param string $offset
* @param mixed $value
* @param string $offset Offset.
* @param mixed $value Value.
*/
public function offsetSet( $offset, $value ) {
switch ( $offset ) {
case 'is_variation' :
case 'is_variation':
$this->set_variation( $value );
break;
case 'is_visible' :
case 'is_visible':
$this->set_visible( $value );
break;
case 'value' :
case 'value':
$this->set_options( $value );
break;
default :
default:
if ( is_callable( array( $this, "set_$offset" ) ) ) {
return $this->{"set_$offset"}( $value );
}
@ -311,19 +310,19 @@ class WC_Product_Attribute implements ArrayAccess {
}
/**
* offsetUnset.
* OffsetUnset.
*
* @param string $offset
* @param string $offset Offset.
*/
public function offsetUnset( $offset ) {}
/**
* offsetExists.
* OffsetExists.
*
* @param string $offset
* @param string $offset Offset.
* @return bool
*/
public function offsetExists( $offset ) {
return in_array( $offset, array_merge( array( 'is_variation', 'is_visible', 'is_taxonomy', 'value' ), array_keys( $this->data ) ) );
return in_array( $offset, array_merge( array( 'is_variation', 'is_visible', 'is_taxonomy', 'value' ), array_keys( $this->data ) ), true );
}
}

View File

@ -1,31 +1,34 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Represents a file which can be downloaded.
*
* @version 3.0.0
* @since 3.0.0
* @package WooCommerce/Classes
* @author WooThemes
* @package WooCommerce/Classes
* @version 3.0.0
* @since 3.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Product download class.
*/
class WC_Product_Download implements ArrayAccess {
/**
* Data array.
*
* @since 3.0.0
* @var array
*/
protected $data = array(
'id' => '',
'name' => '',
'file' => '',
'id' => '',
'name' => '',
'file' => '',
);
/**
* Returns all data for this object.
*
* @return array
*/
public function get_data() {
@ -34,6 +37,7 @@ class WC_Product_Download implements ArrayAccess {
/**
* Get allowed mime types.
*
* @return array
*/
public function get_allowed_mime_types() {
@ -42,6 +46,7 @@ class WC_Product_Download implements ArrayAccess {
/**
* Get type of file path set.
*
* @param string $file_path optional.
* @return string absolute, relative, or shortcode.
*/
@ -58,6 +63,7 @@ class WC_Product_Download implements ArrayAccess {
/**
* Get file type.
*
* @return string
*/
public function get_file_type() {
@ -67,26 +73,29 @@ class WC_Product_Download implements ArrayAccess {
/**
* Get file extension.
*
* @return string
*/
public function get_file_extension() {
$parsed_url = parse_url( $this->get_file(), PHP_URL_PATH );
$parsed_url = wp_parse_url( $this->get_file(), PHP_URL_PATH );
return pathinfo( $parsed_url, PATHINFO_EXTENSION );
}
/**
* Check if file is allowed.
*
* @return boolean
*/
public function is_allowed_filetype() {
if ( 'relative' !== $this->get_type_of_file_path() ) {
return true;
}
return ! $this->get_file_extension() || in_array( $this->get_file_type(), $this->get_allowed_mime_types() );
return ! $this->get_file_extension() || in_array( $this->get_file_type(), $this->get_allowed_mime_types(), true );
}
/**
* Validate file exists.
*
* @return boolean
*/
public function file_exists() {
@ -110,7 +119,8 @@ class WC_Product_Download implements ArrayAccess {
/**
* Set ID.
* @param string $value
*
* @param string $value Download ID.
*/
public function set_id( $value ) {
$this->data['id'] = wc_clean( $value );
@ -118,7 +128,8 @@ class WC_Product_Download implements ArrayAccess {
/**
* Set name.
* @param string $value
*
* @param string $value Download name.
*/
public function set_name( $value ) {
$this->data['name'] = wc_clean( $value );
@ -126,8 +137,9 @@ class WC_Product_Download implements ArrayAccess {
/**
* Set previous_hash.
*
* @deprecated 3.3.0 No longer using filename based hashing to keep track of files.
* @param string $value
* @param string $value Previous hash.
*/
public function set_previous_hash( $value ) {
wc_deprecated_function( __FUNCTION__, '3.3' );
@ -136,11 +148,12 @@ class WC_Product_Download implements ArrayAccess {
/**
* Set file.
* @param string $value
*
* @param string $value File.
*/
public function set_file( $value ) {
switch ( $this->get_type_of_file_path( $value ) ) {
case 'absolute' :
case 'absolute':
$this->data['file'] = esc_url_raw( $value );
break;
default:
@ -157,6 +170,7 @@ class WC_Product_Download implements ArrayAccess {
/**
* Get id.
*
* @return string
*/
public function get_id() {
@ -165,6 +179,7 @@ class WC_Product_Download implements ArrayAccess {
/**
* Get name.
*
* @return string
*/
public function get_name() {
@ -173,6 +188,7 @@ class WC_Product_Download implements ArrayAccess {
/**
* Get previous_hash.
*
* @deprecated 3.3.0 No longer using filename based hashing to keep track of files.
* @return string
*/
@ -183,6 +199,7 @@ class WC_Product_Download implements ArrayAccess {
/**
* Get file.
*
* @return string
*/
public function get_file() {
@ -196,13 +213,14 @@ class WC_Product_Download implements ArrayAccess {
*/
/**
* offsetGet
* @param string $offset
* OffsetGet.
*
* @param string $offset Offset.
* @return mixed
*/
public function offsetGet( $offset ) {
switch ( $offset ) {
default :
default:
if ( is_callable( array( $this, "get_$offset" ) ) ) {
return $this->{"get_$offset"}();
}
@ -212,13 +230,14 @@ class WC_Product_Download implements ArrayAccess {
}
/**
* offsetSet
* @param string $offset
* @param mixed $value
* OffsetSet.
*
* @param string $offset Offset.
* @param mixed $value Value.
*/
public function offsetSet( $offset, $value ) {
switch ( $offset ) {
default :
default:
if ( is_callable( array( $this, "set_$offset" ) ) ) {
return $this->{"set_$offset"}( $value );
}
@ -227,17 +246,19 @@ class WC_Product_Download implements ArrayAccess {
}
/**
* offsetUnset
* @param string $offset
* OffsetUnset.
*
* @param string $offset Offset.
*/
public function offsetUnset( $offset ) {}
/**
* offsetExists
* @param string $offset
* OffsetExists.
*
* @param string $offset Offset.
* @return bool
*/
public function offsetExists( $offset ) {
return in_array( $offset, array_keys( $this->data ) );
return in_array( $offset, array_keys( $this->data ), true );
}
}

View File

@ -1,18 +1,17 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* External Product Class.
* External Product
*
* External products cannot be bought; they link offsite. Extends simple products.
*
* @class WC_Product_External
* @version 3.0.0
* @package WooCommerce/Classes/Products
* @category Class
* @author WooThemes
* @package WooCommerce/Classes/Products
* @version 3.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Product external class.
*/
class WC_Product_External extends WC_Product {
@ -28,6 +27,7 @@ class WC_Product_External extends WC_Product {
/**
* Get internal type.
*
* @return string
*/
public function get_type() {
@ -45,7 +45,7 @@ class WC_Product_External extends WC_Product {
/**
* Get product url.
*
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_product_url( $context = 'view' ) {
@ -55,7 +55,7 @@ class WC_Product_External extends WC_Product {
/**
* Get button text.
*
* @param string $context
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
* @return string
*/
public function get_button_text( $context = 'view' ) {
@ -96,7 +96,7 @@ class WC_Product_External extends WC_Product {
* External products cannot be stock managed.
*
* @since 3.0.0
* @param bool
* @param bool $manage_stock If manage stock.
*/
public function set_manage_stock( $manage_stock ) {
$this->set_prop( 'manage_stock', false );
@ -111,7 +111,7 @@ class WC_Product_External extends WC_Product {
*
* @since 3.0.0
*
* @param string $stock_status
* @param string $stock_status Stock status.
*/
public function set_stock_status( $stock_status = '' ) {
$this->set_prop( 'stock_status', 'instock' );

View File

@ -1,31 +1,31 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Product Factory Class
* Product Factory
*
* The WooCommerce product factory creating the right product object.
*
* @class WC_Product_Factory
* @version 3.0.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
* @package WooCommerce/Classes
* @version 3.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Product factory class.
*/
class WC_Product_Factory {
/**
* Get a product.
*
* @param mixed $product_id (default: false)
* @param mixed $product_id WC_Product|WP_Post|int|bool $product Product instance, post instance, numeric or false to use global $post.
* @param array $deprecated Previously used to pass arguments to the factory, e.g. to force a type.
* @return WC_Product|bool Product object or null if the product cannot be loaded.
*/
public function get_product( $product_id = false, $deprecated = array() ) {
if ( ! $product_id = $this->get_product_id( $product_id ) ) {
$product_id = $this->get_product_id( $product_id );
if ( ! $product_id ) {
return false;
}
@ -53,8 +53,8 @@ class WC_Product_Factory {
* Gets a product classname and allows filtering. Returns WC_Product_Simple if the class does not exist.
*
* @since 3.0.0
* @param int $product_id
* @param string $product_type
* @param int $product_id Product ID.
* @param string $product_type Product type.
* @return string
*/
public static function get_product_classname( $product_id, $product_type ) {
@ -71,7 +71,7 @@ class WC_Product_Factory {
* Get the product type for a product.
*
* @since 3.0.0
* @param int $product_id
* @param int $product_id Product ID.
* @return string|false
*/
public static function get_product_type( $product_id ) {
@ -87,7 +87,7 @@ class WC_Product_Factory {
/**
* Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class.
*
* @param string $product_type
* @param string $product_type Product type.
* @return string|false
*/
public static function get_classname_from_product_type( $product_type ) {
@ -97,8 +97,8 @@ class WC_Product_Factory {
/**
* Get the product ID depending on what was passed.
*
* @since 3.0.0
* @param mixed $product
* @since 3.0.0
* @param WC_Product|WP_Post|int|bool $product Product instance, post instance, numeric or false to use global $post.
* @return int|bool false on failure
*/
private function get_product_id( $product ) {

View File

@ -1,22 +1,17 @@
<?php
/**
* Class WC_Product_Grouped file.
*
* @package WooCommerce\Classes\Products
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Grouped Product Class.
* Grouped Product
*
* Grouped products cannot be purchased - they are wrappers for other products.
*
* @class WC_Product_Grouped
* @version 3.0.0
* @package WooCommerce/Classes/Products
* @package WooCommerce\Classes\Products
* @version 3.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Product grouped class.
*/
class WC_Product_Grouped extends WC_Product {
@ -41,7 +36,6 @@ class WC_Product_Grouped extends WC_Product {
/**
* Get the add to cart button text.
*
* @access public
* @return string
*/
public function add_to_cart_text() {
@ -91,7 +85,6 @@ class WC_Product_Grouped extends WC_Product {
/**
* Returns the price in html format.
*
* @access public
* @param string $price (default: '').
* @return string
*/

View File

@ -1,73 +1,77 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class for parameter-based Product querying.
* Class for parameter-based Product querying
*
* Args and usage: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
*
* @package WooCommerce/Classes
* @version 3.2.0
* @since 3.2.0
* @package WooCommerce/Classes
* @category Class
*/
defined( 'ABSPATH' ) || exit;
/**
* Product query class.
*/
class WC_Product_Query extends WC_Object_Query {
/**
* Valid query vars for products.
*
* @return array
*/
protected function get_default_query_vars() {
return array_merge(
parent::get_default_query_vars(),
array(
'status' => array( 'draft', 'pending', 'private', 'publish' ),
'type' => array_merge( array_keys( wc_get_product_types() ) ),
'limit' => get_option( 'posts_per_page' ),
'include' => array(),
'date_created' => '',
'date_modified' => '',
'featured' => '',
'visibility' => '',
'sku' => '',
'price' => '',
'regular_price' => '',
'sale_price' => '',
'date_on_sale_from' => '',
'date_on_sale_to' => '',
'total_sales' => '',
'tax_status' => '',
'tax_class' => '',
'manage_stock' => '',
'stock_quantity' => '',
'stock_status' => '',
'backorders' => '',
'sold_individually' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'reviews_allowed' => '',
'virtual' => '',
'downloadable' => '',
'category' => array(),
'tag' => array(),
'shipping_class' => array(),
'download_limit' => '',
'download_expiry' => '',
'average_rating' => '',
'review_count' => '',
'status' => array( 'draft', 'pending', 'private', 'publish' ),
'type' => array_merge( array_keys( wc_get_product_types() ) ),
'limit' => get_option( 'posts_per_page' ),
'include' => array(),
'date_created' => '',
'date_modified' => '',
'featured' => '',
'visibility' => '',
'sku' => '',
'price' => '',
'regular_price' => '',
'sale_price' => '',
'date_on_sale_from' => '',
'date_on_sale_to' => '',
'total_sales' => '',
'tax_status' => '',
'tax_class' => '',
'manage_stock' => '',
'stock_quantity' => '',
'stock_status' => '',
'backorders' => '',
'sold_individually' => '',
'weight' => '',
'length' => '',
'width' => '',
'height' => '',
'reviews_allowed' => '',
'virtual' => '',
'downloadable' => '',
'category' => array(),
'tag' => array(),
'shipping_class' => array(),
'download_limit' => '',
'download_expiry' => '',
'average_rating' => '',
'review_count' => '',
)
);
}
/**
* Get products matching the current query vars.
*
* @return array|object of WC_Product objects
*/
public function get_products() {
$args = apply_filters( 'woocommerce_product_object_query_args', $this->get_query_vars() );
$args = apply_filters( 'woocommerce_product_object_query_args', $this->get_query_vars() );
$results = WC_Data_Store::load( 'product' )->query( $args );
return apply_filters( 'woocommerce_product_object_query', $results, $args );
}

View File

@ -1,33 +1,32 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Simple Product Class.
*
* The default product type kinda product.
*
* @class WC_Product_Simple
* @package WooCommerce/Classes/Products
* @category Class
* @author WooThemes
* @package WooCommerce/Classes/Products
*/
defined( 'ABSPATH' ) || exit;
/**
* Simple product class.
*/
class WC_Product_Simple extends WC_Product {
/**
* Initialize simple product.
*
* @param mixed $product
* @param WC_Product|int $product Product instance or ID.
*/
public function __construct( $product = 0 ) {
$this->supports[] = 'ajax_add_to_cart';
$this->supports[] = 'ajax_add_to_cart';
parent::__construct( $product );
}
/**
* Get internal type.
*
* @return string
*/
public function get_type() {

View File

@ -1,17 +1,17 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Variable Product Class.
* Variable Product
*
* The WooCommerce product class handles individual product data.
*
* @version 3.0.0
* @package WooCommerce/Classes/Products
* @category Class
* @author WooThemes
* @version 3.0.0
* @package WooCommerce/Classes/Products
*/
defined( 'ABSPATH' ) || exit;
/**
* Variable product class.
*/
class WC_Product_Variable extends WC_Product {
@ -75,7 +75,6 @@ class WC_Product_Variable extends WC_Product {
* Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale.
*
* @param bool $for_display If true, prices will be adapted for display based on the `woocommerce_tax_display_shop` setting (including or excluding taxes).
*
* @return array Array of RAW prices, regular prices, and sale prices with keys set to variation ID.
*/
public function get_variation_prices( $for_display = false ) {
@ -91,9 +90,8 @@ class WC_Product_Variable extends WC_Product {
/**
* Get the min or max variation regular price.
*
* @param string $min_or_max Min or max price.
* @param string $min_or_max Min or max price.
* @param boolean $for_display If true, prices will be adapted for display based on the `woocommerce_tax_display_shop` setting (including or excluding taxes).
*
* @return string
*/
public function get_variation_regular_price( $min_or_max = 'min', $for_display = false ) {
@ -106,9 +104,8 @@ class WC_Product_Variable extends WC_Product {
/**
* Get the min or max variation sale price.
*
* @param string $min_or_max Min or max price.
* @param string $min_or_max Min or max price.
* @param boolean $for_display If true, prices will be adapted for display based on the `woocommerce_tax_display_shop` setting (including or excluding taxes).
*
* @return string
*/
public function get_variation_sale_price( $min_or_max = 'min', $for_display = false ) {
@ -121,9 +118,8 @@ class WC_Product_Variable extends WC_Product {
/**
* Get the min or max variation (active) price.
*
* @param string $min_or_max Min or max price.
* @param string $min_or_max Min or max price.
* @param boolean $for_display If true, prices will be adapted for display based on the `woocommerce_tax_display_shop` setting (including or excluding taxes).
*
* @return string
*/
public function get_variation_price( $min_or_max = 'min', $for_display = false ) {
@ -146,8 +142,7 @@ class WC_Product_Variable extends WC_Product {
* due to the strings being very long and unclear/confusing. A single range
* is shown instead.
*
* @param string $price (default: '')
*
* @param string $price Price (default: '').
* @return string
*/
public function get_price_html( $price = '' ) {
@ -180,11 +175,10 @@ class WC_Product_Variable extends WC_Product {
*
* This is skipped if the suffix
* has dynamic values such as {price_excluding_tax} for variable products.
*
* @see get_price_html for an explanation as to why.
*
* @param string $price to calculate, left blank to just use get_price()
* @param integer $qty passed on to get_price_including_tax() or get_price_excluding_tax()
*
* @param string $price Price to calculate, left blank to just use get_price().
* @param integer $qty Quantity passed on to get_price_including_tax() or get_price_excluding_tax().
* @return string
*/
public function get_price_suffix( $price = '', $qty = 1 ) {
@ -200,8 +194,7 @@ class WC_Product_Variable extends WC_Product {
/**
* Return a products child ids.
*
* @param bool|string $visible_only
*
* @param bool|string $visible_only Visible only.
* @return array Children ids
*/
public function get_children( $visible_only = '' ) {
@ -236,8 +229,7 @@ class WC_Product_Variable extends WC_Product {
/**
* If set, get the default attributes for a variable product.
*
* @param string $attribute_name
*
* @param string $attribute_name Attribute name.
* @return string
*/
public function get_variation_default_attribute( $attribute_name ) {
@ -250,8 +242,7 @@ class WC_Product_Variable extends WC_Product {
/**
* Variable products themselves cannot be downloadable.
*
* @param string $context
*
* @param string $context What the value is for. Valid values are view and edit.
* @return bool
*/
public function get_downloadable( $context = 'view' ) {
@ -261,8 +252,7 @@ class WC_Product_Variable extends WC_Product {
/**
* Variable products themselves cannot be virtual.
*
* @param string $context
*
* @param string $context What the value is for. Valid values are view and edit.
* @return bool
*/
public function get_virtual( $context = 'view' ) {
@ -280,7 +270,7 @@ class WC_Product_Variable extends WC_Product {
foreach ( $this->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
// Hide out of stock variations if 'Hide out of stock items from the catalog' is checked
// Hide out of stock variations if 'Hide out of stock items from the catalog' is checked.
if ( ! $variation || ! $variation->exists() || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) {
continue;
}
@ -299,10 +289,9 @@ class WC_Product_Variable extends WC_Product {
/**
* Returns an array of data for a variation. Used in the add to cart form.
*
* @since 2.4.0
*
* @param WC_Product $variation Variation product object or ID
*
* @param WC_Product $variation Variation product object or ID.
* @return array|bool
*/
public function get_available_variation( $variation ) {
@ -313,34 +302,36 @@ class WC_Product_Variable extends WC_Product {
return false;
}
// See if prices should be shown for each variation after selection.
$show_variation_price = apply_filters( 'woocommerce_show_variation_price', $variation->get_price() === "" || $this->get_variation_sale_price( 'min' ) !== $this->get_variation_sale_price( 'max' ) || $this->get_variation_regular_price( 'min' ) !== $this->get_variation_regular_price( 'max' ), $this, $variation );
$show_variation_price = apply_filters( 'woocommerce_show_variation_price', $variation->get_price() === '' || $this->get_variation_sale_price( 'min' ) !== $this->get_variation_sale_price( 'max' ) || $this->get_variation_regular_price( 'min' ) !== $this->get_variation_regular_price( 'max' ), $this, $variation );
return apply_filters( 'woocommerce_available_variation', array(
'attributes' => $variation->get_variation_attributes(),
'availability_html' => wc_get_stock_html( $variation ),
'backorders_allowed' => $variation->backorders_allowed(),
'dimensions' => $variation->get_dimensions( false ),
'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ),
'display_price' => wc_get_price_to_display( $variation ),
'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),
'image' => wc_get_product_attachment_props( $variation->get_image_id() ),
'image_id' => $variation->get_image_id(),
'is_downloadable' => $variation->is_downloadable(),
'is_in_stock' => $variation->is_in_stock(),
'is_purchasable' => $variation->is_purchasable(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
'is_virtual' => $variation->is_virtual(),
'max_qty' => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
'min_qty' => $variation->get_min_purchase_quantity(),
'price_html' => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'sku' => $variation->get_sku(),
'variation_description' => wc_format_content( $variation->get_description() ),
'variation_id' => $variation->get_id(),
'variation_is_active' => $variation->variation_is_active(),
'variation_is_visible' => $variation->variation_is_visible(),
'weight' => $variation->get_weight(),
'weight_html' => wc_format_weight( $variation->get_weight() ),
), $this, $variation );
return apply_filters(
'woocommerce_available_variation', array(
'attributes' => $variation->get_variation_attributes(),
'availability_html' => wc_get_stock_html( $variation ),
'backorders_allowed' => $variation->backorders_allowed(),
'dimensions' => $variation->get_dimensions( false ),
'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ),
'display_price' => wc_get_price_to_display( $variation ),
'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),
'image' => wc_get_product_attachment_props( $variation->get_image_id() ),
'image_id' => $variation->get_image_id(),
'is_downloadable' => $variation->is_downloadable(),
'is_in_stock' => $variation->is_in_stock(),
'is_purchasable' => $variation->is_purchasable(),
'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',
'is_virtual' => $variation->is_virtual(),
'max_qty' => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',
'min_qty' => $variation->get_min_purchase_quantity(),
'price_html' => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',
'sku' => $variation->get_sku(),
'variation_description' => wc_format_content( $variation->get_description() ),
'variation_id' => $variation->get_id(),
'variation_is_active' => $variation->variation_is_active(),
'variation_is_visible' => $variation->variation_is_visible(),
'weight' => $variation->get_weight(),
'weight_html' => wc_format_weight( $variation->get_weight() ),
), $this, $variation
);
}
/*
@ -353,8 +344,7 @@ class WC_Product_Variable extends WC_Product {
* Sets an array of variation attributes.
*
* @since 3.0.0
*
* @param array
* @param array $variation_attributes Attributes list.
*/
public function set_variation_attributes( $variation_attributes ) {
$this->variation_attributes = $variation_attributes;
@ -364,8 +354,7 @@ class WC_Product_Variable extends WC_Product {
* Sets an array of children for the product.
*
* @since 3.0.0
*
* @param array
* @param array $children Childre products.
*/
public function set_children( $children ) {
$this->children = array_filter( wp_parse_id_list( (array) $children ) );
@ -375,8 +364,7 @@ class WC_Product_Variable extends WC_Product {
* Sets an array of visible children only.
*
* @since 3.0.0
*
* @param array
* @param array $visible_children List of visible children products.
*/
public function set_visible_children( $visible_children ) {
$this->visible_children = array_filter( wp_parse_id_list( (array) $visible_children ) );
@ -390,6 +378,7 @@ class WC_Product_Variable extends WC_Product {
/**
* Ensure properties are set correctly before save.
*
* @since 3.0.0
*/
public function validate_props() {
@ -454,8 +443,7 @@ class WC_Product_Variable extends WC_Product {
/**
* Returns whether or not the product is on sale.
*
* @param string $context What the value is for. Valid values are view and edit.
*
* @param string $context What the value is for. Valid values are view and edit. What the value is for. Valid values are view and edit.
* @return bool
*/
public function is_on_sale( $context = 'view' ) {
@ -467,6 +455,7 @@ class WC_Product_Variable extends WC_Product {
/**
* Is a child in stock?
*
* @return boolean
*/
public function child_is_in_stock() {
@ -485,6 +474,7 @@ class WC_Product_Variable extends WC_Product {
/**
* Does a child have a weight set?
*
* @return boolean
*/
public function child_has_weight() {
@ -501,6 +491,7 @@ class WC_Product_Variable extends WC_Product {
/**
* Does a child have dimensions set?
*
* @return boolean
*/
public function child_has_dimensions() {
@ -555,8 +546,7 @@ class WC_Product_Variable extends WC_Product {
* upwards (from child to parent) when the variation is saved.
*
* @param WC_Product|int $product Product object or ID for which you wish to sync.
* @param bool $save If true, the product object will be saved to the DB before returning it.
*
* @param bool $save If true, the product object will be saved to the DB before returning it.
* @return WC_Product Synced product object.
*/
public static function sync( $product, $save = true ) {
@ -575,10 +565,12 @@ class WC_Product_Variable extends WC_Product {
$product->save();
}
wc_do_deprecated_action( 'woocommerce_variable_product_sync', array(
$product->get_id(),
$product->get_visible_children()
), '3.0', 'woocommerce_variable_product_sync_data, woocommerce_new_product or woocommerce_update_product' );
wc_do_deprecated_action(
'woocommerce_variable_product_sync', array(
$product->get_id(),
$product->get_visible_children(),
), '3.0', 'woocommerce_variable_product_sync_data, woocommerce_new_product or woocommerce_update_product'
);
}
return $product;
@ -588,8 +580,7 @@ class WC_Product_Variable extends WC_Product {
* Sync parent stock status with the status of all children and save.
*
* @param WC_Product|int $product Product object or ID for which you wish to sync.
* @param bool $save If true, the product object will be saved to the DB before returning it.
*
* @param bool $save If true, the product object will be saved to the DB before returning it.
* @return WC_Product Synced product object.
*/
public static function sync_stock_status( $product, $save = true ) {
@ -611,8 +602,7 @@ class WC_Product_Variable extends WC_Product {
/**
* Sort an associativate array of $variation_id => $price pairs in order of min and max prices.
*
* @param array $prices Associativate array of $variation_id => $price pairs
*
* @param array $prices Associativate array of $variation_id => $price pairs.
* @return array
*/
protected function sort_variation_prices( $prices ) {

View File

@ -1,24 +1,17 @@
<?php
/**
* WooCommerce product variation class.
*
* @package WooCommerce/Classes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Product Variation Class.
* Product Variation
*
* The WooCommerce product variation class handles product variation data.
*
* @class WC_Product_Variation
* @version 3.0.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
* @package WooCommerce/Classes
* @version 3.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Product variation class.
*/
class WC_Product_Variation extends WC_Product_Simple {
@ -166,11 +159,11 @@ class WC_Product_Variation extends WC_Product_Simple {
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() );
$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 = $this->get_variation_attributes();
}
return add_query_arg( array_map( 'urlencode', array_filter( $data ) ), $url );
@ -182,7 +175,14 @@ class WC_Product_Variation extends WC_Product_Simple {
* @return string
*/
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();
$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 );
}
@ -504,7 +504,7 @@ class WC_Product_Variation extends WC_Product_Simple {
* @return array valid tax classes
*/
protected function get_valid_tax_classes() {
$valid_classes = WC_Tax::get_tax_class_slugs();
$valid_classes = WC_Tax::get_tax_class_slugs();
$valid_classes[] = 'parent';
return $valid_classes;