| 1 |
<?php |
| 2 |
/** |
| 3 |
* Derived from SkyVerge WooCommerce Plugin Framework https://github.com/skyverge/wc-plugin-framework/ |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WPO\WC\PostNL\Compatibility; |
| 7 |
|
| 8 |
use WC_Product; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} // Exit if accessed directly |
| 13 |
|
| 14 |
if (class_exists('\\WPO\\WC\\PostNL\\Compatibility\\Product')) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* WooCommerce product compatibility class. |
| 20 |
* |
| 21 |
* @since 4.6.0-dev |
| 22 |
*/ |
| 23 |
class Product extends Data |
| 24 |
{ |
| 25 |
|
| 26 |
/** @var array mapped compatibility properties, as `$new_prop => $old_prop` */ |
| 27 |
protected static $compat_props = [ |
| 28 |
'catalog_visibility' => 'visibility', |
| 29 |
'date_on_sale_from' => 'sale_price_dates_from', |
| 30 |
'date_on_sale_to' => 'sale_price_dates_to', |
| 31 |
'gallery_image_ids' => 'product_image_gallery', |
| 32 |
'cross_sell_ids' => 'crosssell_ids', |
| 33 |
]; |
| 34 |
|
| 35 |
/** |
| 36 |
* Backports WC_Product::get_id() method to 2.4.x |
| 37 |
* |
| 38 |
* @link https://github.com/woothemes/woocommerce/pull/9765 |
| 39 |
* @since 4.2.0 |
| 40 |
* |
| 41 |
* @param WC_Product $product product object |
| 42 |
* |
| 43 |
* @return string|int product ID |
| 44 |
*/ |
| 45 |
public static function get_id(WC_Product $product) |
| 46 |
{ |
| 47 |
if (method_exists($product, 'get_id')) { |
| 48 |
return $product->get_id(); |
| 49 |
} else { |
| 50 |
return $product->is_type('variation') ? $product->variation_id : $product->id; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Gets a product property. |
| 56 |
* |
| 57 |
* @param WC_Product $object the product object |
| 58 |
* @param string $prop the property name |
| 59 |
* @param string $context if 'view' then the value will be filtered |
| 60 |
* |
| 61 |
* @return mixed |
| 62 |
* @since 4.6.0-dev |
| 63 |
*/ |
| 64 |
public static function get_prop($object, $prop, $context = 'edit', $compat_props = []) |
| 65 |
{ |
| 66 |
// backport 'WC_Product::get_parent_id()' to pre-3.0 |
| 67 |
if (WC_Core::is_wc_version_lt_3_0() && 'parent_id' === $prop) { |
| 68 |
$prop = 'id'; |
| 69 |
$context = $object->is_type('variation') ? 'raw' : $context; |
| 70 |
} |
| 71 |
|
| 72 |
return parent::get_prop($object, $prop, $context, self::$compat_props); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Sets an products's properties. |
| 77 |
* Note that this does not save any data to the database. |
| 78 |
* |
| 79 |
* @param WC_Product $object the product object |
| 80 |
* @param array $props the new properties as $key => $value |
| 81 |
* |
| 82 |
* @return WC_Product |
| 83 |
* @since 4.6.0-dev |
| 84 |
*/ |
| 85 |
public static function set_props($object, $props, $compat_props = []) |
| 86 |
{ |
| 87 |
return parent::set_props($object, $props, self::$compat_props); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Makes WC_Product::get_parent() available for WC 3.0+ |
| 92 |
* |
| 93 |
* @param WC_Product $product the product object |
| 94 |
* |
| 95 |
* @return WC_Product |
| 96 |
* @since 4.6.0-dev |
| 97 |
*/ |
| 98 |
public static function get_parent(WC_Product $product) |
| 99 |
{ |
| 100 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 101 |
$parent = wc_get_product($product->get_parent_id()); |
| 102 |
} else { |
| 103 |
$parent = $product->get_parent(); |
| 104 |
} |
| 105 |
|
| 106 |
return $parent; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Makes WC_Product::get_dimensions() available for WC 3.0+ |
| 111 |
* |
| 112 |
* @param WC_Product $product the product object |
| 113 |
* @param string $rating Optional. The product rating |
| 114 |
* |
| 115 |
* @return string |
| 116 |
* @since 4.6.0-dev |
| 117 |
*/ |
| 118 |
public static function get_dimensions(WC_Product $product) |
| 119 |
{ |
| 120 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 121 |
$dimensions = [ |
| 122 |
'length' => $product->get_length(), |
| 123 |
'width' => $product->get_width(), |
| 124 |
'height' => $product->get_height(), |
| 125 |
]; |
| 126 |
return apply_filters('woocommerce_product_dimensions', wc_format_dimensions($dimensions), $product); |
| 127 |
} else { |
| 128 |
return $product->get_dimensions(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Backports wc_update_product_stock() to pre-3.0.0 |
| 134 |
* |
| 135 |
* @param WC_Product $product the product object |
| 136 |
* @param int $amount Optional. The new stock quantity |
| 137 |
* @param string $mode Optional. Can be set, add, or subtract |
| 138 |
* |
| 139 |
* @return int |
| 140 |
* @since 4.6.0-dev |
| 141 |
*/ |
| 142 |
public static function wc_update_product_stock(WC_Product $product, $amount = null, $mode = 'set') |
| 143 |
{ |
| 144 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 145 |
return wc_update_product_stock($product, $amount, $mode); |
| 146 |
} else { |
| 147 |
return $product->set_stock($amount, $mode); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Backports wc_get_price_html_from_text() to pre-3.0.0 |
| 153 |
* |
| 154 |
* @param WC_Product $product the product object |
| 155 |
* |
| 156 |
* @return string |
| 157 |
* @since 4.6.0-dev |
| 158 |
*/ |
| 159 |
public static function wc_get_price_html_from_text(WC_Product $product) |
| 160 |
{ |
| 161 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 162 |
return wc_get_price_html_from_text(); |
| 163 |
} else { |
| 164 |
return $product->get_price_html_from_text(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Backports wc_get_price_including_tax() to pre-3.0.0 |
| 170 |
* |
| 171 |
* @param WC_Product $product the product object |
| 172 |
* @param int $qty Optional. The quantity |
| 173 |
* @param string $price Optional. The product price |
| 174 |
* |
| 175 |
* @return string |
| 176 |
* @since 4.6.0-dev |
| 177 |
*/ |
| 178 |
public static function wc_get_price_including_tax(WC_Product $product, $qty = 1, $price = '') |
| 179 |
{ |
| 180 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 181 |
return wc_get_price_including_tax( |
| 182 |
$product, |
| 183 |
[ |
| 184 |
'qty' => $qty, |
| 185 |
'price' => $price, |
| 186 |
] |
| 187 |
); |
| 188 |
} else { |
| 189 |
return $product->get_price_including_tax($qty, $price); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Backports wc_get_price_excluding_tax() to pre-3.0.0 |
| 195 |
* |
| 196 |
* @param WC_Product $product the product object |
| 197 |
* @param int $qty Optional. The quantity |
| 198 |
* @param string $price Optional. The product price |
| 199 |
* |
| 200 |
* @return string |
| 201 |
* @since 4.6.0-dev |
| 202 |
*/ |
| 203 |
public static function wc_get_price_excluding_tax(WC_Product $product, $qty = 1, $price = '') |
| 204 |
{ |
| 205 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 206 |
return wc_get_price_excluding_tax( |
| 207 |
$product, |
| 208 |
[ |
| 209 |
'qty' => $qty, |
| 210 |
'price' => $price, |
| 211 |
] |
| 212 |
); |
| 213 |
} else { |
| 214 |
return $product->get_price_excluding_tax($qty, $price); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Backports wc_get_price_to_display() to pre-3.0.0 |
| 220 |
* |
| 221 |
* @param WC_Product $product the product object |
| 222 |
* @param string $price Optional. The product price |
| 223 |
* @param int $qty Optional. The quantity |
| 224 |
* |
| 225 |
* @return string |
| 226 |
* @since 4.6.0-dev |
| 227 |
*/ |
| 228 |
public static function wc_get_price_to_display(WC_Product $product, $price = '', $qty = 1) |
| 229 |
{ |
| 230 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 231 |
return wc_get_price_to_display( |
| 232 |
$product, |
| 233 |
[ |
| 234 |
'qty' => $qty, |
| 235 |
'price' => $price, |
| 236 |
] |
| 237 |
); |
| 238 |
} else { |
| 239 |
return $product->get_display_price($price, $qty); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Backports wc_get_product_category_list() to pre-3.0.0 |
| 245 |
* |
| 246 |
* @param WC_Product $product the product object |
| 247 |
* @param string $sep Optional. The list separator |
| 248 |
* @param string $before Optional. To display before the list |
| 249 |
* @param string $after Optional. To display after the list |
| 250 |
* |
| 251 |
* @return string |
| 252 |
* @since 4.6.0-dev |
| 253 |
*/ |
| 254 |
public static function wc_get_product_category_list(WC_Product $product, $sep = ', ', $before = '', $after = '') |
| 255 |
{ |
| 256 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 257 |
$id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id(); |
| 258 |
|
| 259 |
return wc_get_product_category_list($id, $sep, $before, $after); |
| 260 |
} else { |
| 261 |
return $product->get_categories($sep, $before, $after); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Backports wc_get_rating_html() to pre-3.0.0 |
| 267 |
* |
| 268 |
* @param WC_Product $product the product object |
| 269 |
* @param string $rating Optional. The product rating |
| 270 |
* |
| 271 |
* @return string |
| 272 |
* @since 4.6.0-dev |
| 273 |
*/ |
| 274 |
public static function wc_get_rating_html(WC_Product $product, $rating = null) |
| 275 |
{ |
| 276 |
if (WC_Core::is_wc_version_gte_3_0()) { |
| 277 |
return wc_get_rating_html($rating); |
| 278 |
} else { |
| 279 |
return $product->get_rating_html($rating); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|