| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WpssoIntegEcomWooCommerce' ) ) { |
| 14 |
|
| 15 |
class WpssoIntegEcomWooCommerce { |
| 16 |
|
| 17 |
private $p; // Wpsso class object. |
| 18 |
|
| 19 |
private $og_type = 'product'; |
| 20 |
private $rating_meta = 'rating'; |
| 21 |
private $worst_rating = 1; |
| 22 |
private $best_rating = 5; |
| 23 |
private $prod_post_type = 'product'; |
| 24 |
private $var_post_type = 'product_variation'; |
| 25 |
private $cat_taxonomy = 'product_cat'; |
| 26 |
private $tag_taxonomy = 'product_tag'; |
| 27 |
private $page_ids = array( |
| 28 |
'account' => -1, |
| 29 |
'cart' => -1, |
| 30 |
'checkout' => -1, |
| 31 |
'transaction' => -1, |
| 32 |
'shop' => -1, |
| 33 |
); |
| 34 |
private $reviews_enabled = null; |
| 35 |
private $rating_enabled = null; |
| 36 |
|
| 37 |
public function __construct( &$plugin ) { // Pass by reference is OK. |
| 38 |
|
| 39 |
$this->p =& $plugin; |
| 40 |
|
| 41 |
if ( $this->p->debug->enabled ) { |
| 42 |
|
| 43 |
$this->p->debug->mark(); |
| 44 |
} |
| 45 |
|
| 46 |
$this->page_ids[ 'account' ] = wc_get_page_id( 'myaccount' ); // Returns -1 if no page selected. |
| 47 |
$this->page_ids[ 'cart' ] = wc_get_page_id( 'cart' ); // Returns -1 if no page selected. |
| 48 |
$this->page_ids[ 'checkout' ] = wc_get_page_id( 'checkout' ); // Returns -1 if no page selected. |
| 49 |
$this->page_ids[ 'shop' ] = wc_get_page_id( 'shop' ); // Returns -1 if no page selected. |
| 50 |
|
| 51 |
$this->reviews_enabled = 'yes' === get_option( 'woocommerce_enable_reviews' ) ? true : false; |
| 52 |
$this->rating_enabled = 'yes' === get_option( 'woocommerce_enable_review_rating' ) ? true : false; |
| 53 |
|
| 54 |
if ( $this->p->debug->enabled ) { |
| 55 |
|
| 56 |
$this->p->debug->log_arr( 'page_ids', $this->page_ids ); |
| 57 |
} |
| 58 |
|
| 59 |
if ( is_admin() ) { |
| 60 |
|
| 61 |
/* |
| 62 |
* Check for possible missing page ID selections. |
| 63 |
* |
| 64 |
* This hook is fired once WordPress, plugins, and the theme are fully loaded and instantiated. |
| 65 |
*/ |
| 66 |
add_action( 'wp_loaded', array( $this, 'check_woocommerce_pages' ), 10, 0 ); |
| 67 |
|
| 68 |
/* |
| 69 |
* Show a suggested list of product attributes. |
| 70 |
*/ |
| 71 |
add_action( 'woocommerce_product_options_attributes', array( $this, 'show_product_attributes_footer' ), -1000, 0 ); |
| 72 |
|
| 73 |
/* |
| 74 |
* Update the Document SSO metabox and toobar notices after saving product variations. |
| 75 |
* |
| 76 |
* See WC_AJAX->save_variations() in woocommerce/includes/class-wc-ajax.php. |
| 77 |
*/ |
| 78 |
add_action( 'woocommerce_ajax_save_product_variations', array( $this, 'ajax_save_product_variations' ), 1000, 1 ); |
| 79 |
|
| 80 |
/* |
| 81 |
* Prevent WooCommerce from creating duplicate entries in the metadata table. |
| 82 |
*/ |
| 83 |
add_filter( 'woocommerce_duplicate_product_exclude_meta', array( $this, 'duplicate_product_exclude_meta' ), 1000, 2 ); |
| 84 |
|
| 85 |
/* |
| 86 |
* Add WPSSO RAR add-on filters. |
| 87 |
* |
| 88 |
* See WpssoIntegEcomWooCommerce->disable_options_keys(). |
| 89 |
*/ |
| 90 |
if ( ! empty( $this->p->avail[ 'p_ext' ][ 'rar' ] ) ) { |
| 91 |
|
| 92 |
$this->p->util->add_plugin_filters( $this, array( |
| 93 |
'post_column_rating_value' => 3, |
| 94 |
), 10, 'wpssorar' ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* Clear the post ID cache after WooCommerce updates the product metadata. |
| 100 |
*/ |
| 101 |
add_action( 'woocommerce_product_object_updated_props', array( $this, 'clear_product_cache' ), 1000, 2 ); |
| 102 |
|
| 103 |
/* |
| 104 |
* Refresh the post ID cache after WooCommerce updates the product object. |
| 105 |
*/ |
| 106 |
add_action( 'woocommerce_after_product_object_save', array( $this, 'refresh_product_cache' ), 1000, 2 ); |
| 107 |
|
| 108 |
/* |
| 109 |
* Return the primary category term for WooCommerce product breadcrumbs. |
| 110 |
*/ |
| 111 |
add_filter( 'woocommerce_breadcrumb_main_term', array( $this, 'woocommerce_breadcrumb_main_term' ), 100, 2 ); |
| 112 |
|
| 113 |
/* |
| 114 |
* Disable the default WooCommerce Schema markup. |
| 115 |
*/ |
| 116 |
add_filter( 'woocommerce_structured_data_product', '__return_empty_array', PHP_INT_MAX ); |
| 117 |
add_filter( 'woocommerce_structured_data_review', '__return_empty_array', PHP_INT_MAX ); |
| 118 |
add_filter( 'woocommerce_structured_data_website', '__return_empty_array', PHP_INT_MAX ); |
| 119 |
|
| 120 |
/* |
| 121 |
* Maybe load missing WooCommerce front-end libraries for 'the_content' filter. |
| 122 |
*/ |
| 123 |
$this->p->util->add_plugin_actions( $this, array( |
| 124 |
'admin_post_head' => 1, |
| 125 |
'scheduled_task_started' => 1, |
| 126 |
) ); |
| 127 |
|
| 128 |
$this->p->util->add_plugin_filters( $this, array( |
| 129 |
'request_url_query_cache_disable' => 4, |
| 130 |
'head_cache_index' => 1, |
| 131 |
'body_class' => 2, |
| 132 |
'use_post' => 1, |
| 133 |
'get_post_type' => 2, |
| 134 |
'schema_type' => 3, |
| 135 |
'schema_type_post_type_labels' => 1, |
| 136 |
'primary_tax_slug' => 2, // See WpssoPost->get_primary_terms(). |
| 137 |
'the_content_seed' => 2, |
| 138 |
'description_seed' => 4, |
| 139 |
'attached_image_ids' => 2, |
| 140 |
'term_image_ids' => 3, |
| 141 |
'get_md_defaults' => 2, |
| 142 |
'get_post_options' => 3, |
| 143 |
'og_seed' => 2, |
| 144 |
'tag_names_seed' => 2, |
| 145 |
'import_product_attributes' => 3, |
| 146 |
) ); |
| 147 |
|
| 148 |
/* |
| 149 |
* Maybe add the Pinterest image to the WooCommerce template for displaying product archives - including |
| 150 |
* the main shop page, which is a post type archive. |
| 151 |
*/ |
| 152 |
if ( ! empty( $this->p->options[ 'pin_add_img_html' ] ) ) { |
| 153 |
|
| 154 |
add_action( 'woocommerce_archive_description', array( $this->p->pinterest, 'show_image_html' ) ); |
| 155 |
} |
| 156 |
|
| 157 |
$this->disable_options_keys(); |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* Return the primary category term for WooCommerce product breadcrumbs. |
| 162 |
* |
| 163 |
* See WC_Breadcrumb->add_crumbs_attachment(). |
| 164 |
* See WC_Breadcrumb->add_crumbs_single(). |
| 165 |
*/ |
| 166 |
public function woocommerce_breadcrumb_main_term( $term, $terms ) { |
| 167 |
|
| 168 |
if ( $this->p->debug->enabled ) { |
| 169 |
|
| 170 |
$this->p->debug->mark(); |
| 171 |
} |
| 172 |
|
| 173 |
global $post; |
| 174 |
|
| 175 |
if ( ! empty( $post->ID ) ) { // Just in case. |
| 176 |
|
| 177 |
$mod = $this->p->post->get_mod( $post->ID ); |
| 178 |
|
| 179 |
if ( ! empty( $mod[ 'id' ] ) ) { // Just in case. |
| 180 |
|
| 181 |
$primary_id = $this->p->post->get_primary_term_id( $mod, $this->cat_taxonomy ); |
| 182 |
|
| 183 |
if ( ! empty( $primary_id ) ) { // Just in case. |
| 184 |
|
| 185 |
$primary_term = get_term( $primary_id, $this->cat_taxonomy ); |
| 186 |
|
| 187 |
if ( $primary_term instanceof WP_Term ) { // Just in case. |
| 188 |
|
| 189 |
return $primary_term; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return $term; |
| 196 |
} |
| 197 |
|
| 198 |
/* |
| 199 |
* Define and disable some plugin options. |
| 200 |
* |
| 201 |
* Since WPSSO Core v14.0.0. |
| 202 |
*/ |
| 203 |
public function disable_options_keys() { |
| 204 |
|
| 205 |
if ( $this->p->debug->enabled ) { |
| 206 |
|
| 207 |
$this->p->debug->mark(); |
| 208 |
} |
| 209 |
|
| 210 |
$dimension_unit_text = get_option( 'woocommerce_dimension_unit', $default = 'cm' ); |
| 211 |
$weight_unit_text = get_option( 'woocommerce_weight_unit', $default = 'kg' ); |
| 212 |
$weight_unit_text = 'lbs' === $weight_unit_text ? 'lb' : $weight_unit_text; // WooCommerce uses 'lbs' and WPSSO uses 'lb'. |
| 213 |
|
| 214 |
foreach ( array( |
| 215 |
'og_def_dimension_units' => $dimension_unit_text, // Default Dimension Units. |
| 216 |
'og_def_weight_units' => $weight_unit_text, // Default Weight Units. |
| 217 |
'plugin_cf_product_retailer_part_no' => '', |
| 218 |
'plugin_cf_product_shipping_length_value' => '', |
| 219 |
'plugin_cf_product_shipping_length_units' => '', |
| 220 |
'plugin_cf_product_shipping_width_value' => '', |
| 221 |
'plugin_cf_product_shipping_width_units' => '', |
| 222 |
'plugin_cf_product_shipping_height_value' => '', |
| 223 |
'plugin_cf_product_shipping_height_units' => '', |
| 224 |
'plugin_cf_product_shipping_weight_value' => '', |
| 225 |
'plugin_cf_product_shipping_weight_units' => '', |
| 226 |
'rar_add_to_product' => 0, // Disable WPSSO Ratings and Reviews for WC Products. |
| 227 |
) as $opt_key => $opt_val ) { |
| 228 |
|
| 229 |
$this->p->options[ $opt_key ] = $opt_val; |
| 230 |
|
| 231 |
$this->p->options[ $opt_key . ':disabled' ] = true; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
public function check_woocommerce_pages() { |
| 236 |
|
| 237 |
if ( $this->p->debug->enabled ) { |
| 238 |
|
| 239 |
$this->p->debug->mark(); |
| 240 |
} |
| 241 |
|
| 242 |
$wc_advanced_msg = sprintf( __( 'Please select a page in the <a href="%s">WooCommerce Settings > Advanced > Page setup</a> section.', |
| 243 |
'wpsso' ), get_admin_url( $blog_id = null, 'admin.php?page=wc-settings&tab=advanced§ion' ) ); |
| 244 |
|
| 245 |
$wc_products_msg = sprintf( __( 'Please select a page in the <a href="%s">WooCommerce Settings > Products > General</a> section.', |
| 246 |
'wpsso' ), get_admin_url( null, 'admin.php?page=wc-settings&tab=products§ion' ) ); |
| 247 |
|
| 248 |
foreach ( array( |
| 249 |
'account' => __( 'My account page', 'woocommerce' ), |
| 250 |
'cart' => __( 'Cart page', 'woocommerce' ), |
| 251 |
'checkout' => __( 'Checkout page', 'woocommerce' ), |
| 252 |
'shop' => __( 'Shop page', 'woocommerce' ), |
| 253 |
) as $page_type => $label_transl ) { |
| 254 |
|
| 255 |
if ( ! is_int( $this->page_ids[ $page_type ] ) || $this->page_ids[ $page_type ] < 1 || |
| 256 |
! SucomUtilWP::post_exists( $this->page_ids[ $page_type ] ) ) { |
| 257 |
|
| 258 |
$notice_msg = sprintf( __( 'The WooCommerce "%1$s" option value is missing or invalid.', 'wpsso' ), $label_transl ) . ' '; |
| 259 |
|
| 260 |
if ( $this->p->debug->enabled ) { |
| 261 |
|
| 262 |
$this->p->debug->log( $notice_msg ); |
| 263 |
} |
| 264 |
|
| 265 |
$notice_msg .= 'shop' === $page_type ? $wc_products_msg : $wc_advanced_msg; |
| 266 |
|
| 267 |
$notice_key = 'wc-' . $page_type . '-page-id-invalid'; |
| 268 |
|
| 269 |
$this->p->notice->warn( $notice_msg, $user_id = null, $notice_key ); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/* |
| 275 |
* Update the Document SSO metabox and toobar notices after saving product variations. |
| 276 |
* |
| 277 |
* See WC_AJAX->save_variations() in woocommerce/includes/class-wc-ajax.php. |
| 278 |
*/ |
| 279 |
public function ajax_save_product_variations( $product_id ) { |
| 280 |
|
| 281 |
$admin_l10n = $this->p->cf[ 'plugin' ][ 'wpsso' ][ 'admin_l10n' ]; |
| 282 |
|
| 283 |
echo '<script>'; |
| 284 |
echo 'window.allowScrollToHash = false;'; |
| 285 |
echo 'if ( \'function\' === typeof sucomEditorPostbox ) {'; |
| 286 |
echo ' sucomEditorPostbox( \'wpsso\', \'' . $admin_l10n . '\', \'' . $product_id . '\' );'; |
| 287 |
echo '}'; |
| 288 |
echo 'if ( \'function\' === typeof sucomToolbarNotices ) {'; |
| 289 |
echo ' sucomToolbarNotices( \'wpsso\', \'' . $admin_l10n . '\' );'; |
| 290 |
echo '}'; |
| 291 |
echo '</script>' . "\n"; |
| 292 |
} |
| 293 |
|
| 294 |
/* |
| 295 |
* Prevent WooCommerce from creating duplicate entries in the metadata table. |
| 296 |
*/ |
| 297 |
public function duplicate_product_exclude_meta( $exclude_keys, $meta_keys ) { |
| 298 |
|
| 299 |
return WpssoAbstractWpMeta::add_duplicate_exclude_meta_keys( $exclude_keys ); |
| 300 |
} |
| 301 |
|
| 302 |
/* |
| 303 |
* Show a suggested list of translated product attribute names. |
| 304 |
* |
| 305 |
* Hooked to the 'woocommerce_product_options_attributes' action. |
| 306 |
*/ |
| 307 |
public function show_product_attributes_footer() { |
| 308 |
|
| 309 |
if ( $this->p->debug->enabled ) { |
| 310 |
|
| 311 |
$this->p->debug->mark(); |
| 312 |
} |
| 313 |
|
| 314 |
global $post; |
| 315 |
|
| 316 |
$product = $this->p->util->wc->get_product( $post->ID ); |
| 317 |
$attr_md_index = WpssoConfig::get_attr_md_index(); |
| 318 |
$wc_attributes = $product->get_attributes(); |
| 319 |
$wc_attr_labels = array(); |
| 320 |
$suggest_names = array(); |
| 321 |
|
| 322 |
foreach ( $wc_attributes as $attribute ) { |
| 323 |
|
| 324 |
$attr_name = $attribute->get_name(); |
| 325 |
|
| 326 |
$wc_attr_names[ $attr_name ] = true; |
| 327 |
} |
| 328 |
|
| 329 |
foreach ( $attr_md_index as $opt_attr_key => $md_key ) { |
| 330 |
|
| 331 |
if ( empty( $md_key ) ) continue; |
| 332 |
|
| 333 |
if ( empty( $this->p->options[ $opt_attr_key ] ) ) continue; |
| 334 |
|
| 335 |
$attr_name = $this->p->options[ $opt_attr_key ]; // Untranslated name. |
| 336 |
|
| 337 |
/* |
| 338 |
* Check that this attribute name has not already been added to this product. |
| 339 |
*/ |
| 340 |
if ( empty( $wc_attr_names[ $attr_name ] ) ) { |
| 341 |
|
| 342 |
$suggest_names[] = SucomUtilOptions::get_key_value( $opt_attr_key, $this->p->options ); // Translated name. |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
$suggest_transl = __( 'Suggested attributes:', 'wpsso' ); |
| 347 |
$suggest_list = implode( ', ', $suggest_names ); |
| 348 |
|
| 349 |
if ( current_user_can( 'manage_options' ) ) { |
| 350 |
|
| 351 |
$suggest_transl = $this->p->util->get_admin_url( 'advanced#sucom-tabset_metadata-tab_product_attrs', $suggest_transl ); |
| 352 |
} |
| 353 |
|
| 354 |
echo '<div class="toolbar">'; |
| 355 |
echo '<strong>' . $suggest_transl . '</strong> ' . $suggest_list; |
| 356 |
echo '</div>'; |
| 357 |
} |
| 358 |
|
| 359 |
public function filter_post_column_rating_value( $value, $post_id, $rating_enabled ) { |
| 360 |
|
| 361 |
if ( $this->p->debug->enabled ) { |
| 362 |
|
| 363 |
$this->p->debug->mark(); |
| 364 |
} |
| 365 |
|
| 366 |
if ( '' === $value && ! $rating_enabled && $this->rating_enabled ) { |
| 367 |
|
| 368 |
if ( $this->prod_post_type === get_post_type( $post_id ) ) { |
| 369 |
|
| 370 |
if ( $product = $this->p->util->wc->get_product( $post_id ) ) { |
| 371 |
|
| 372 |
$average_rating = (float) $product->get_average_rating(); |
| 373 |
|
| 374 |
return number_format( $average_rating, 2, '.', '' ); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
return $value; |
| 380 |
} |
| 381 |
|
| 382 |
/* |
| 383 |
* Clear the post ID cache after WooCommerce updates the product metadata. |
| 384 |
* |
| 385 |
* The $updated_props array may include one or more of the following keys: |
| 386 |
* |
| 387 |
* $updated_props = array( |
| 388 |
* 'sku', |
| 389 |
* 'global_unique_id', |
| 390 |
* 'regular_price', |
| 391 |
* 'sale_price', |
| 392 |
* 'date_on_sale_from', |
| 393 |
* 'date_on_sale_to', |
| 394 |
* 'total_sales', |
| 395 |
* 'tax_status', |
| 396 |
* 'tax_class', |
| 397 |
* 'manage_stock', |
| 398 |
* 'backorders', |
| 399 |
* 'low_stock_amount', |
| 400 |
* 'sold_individually', |
| 401 |
* 'weight', |
| 402 |
* 'length', |
| 403 |
* 'width', |
| 404 |
* 'height', |
| 405 |
* 'upsell_ids', |
| 406 |
* 'cross_sell_ids', |
| 407 |
* 'purchase_note', |
| 408 |
* 'default_attributes', |
| 409 |
* 'virtual', |
| 410 |
* 'downloadable', |
| 411 |
* 'gallery_image_ids', |
| 412 |
* 'download_limit', |
| 413 |
* 'download_expiry', |
| 414 |
* 'image_id', |
| 415 |
* 'stock_quantity', |
| 416 |
* 'stock_status', |
| 417 |
* 'average_rating', |
| 418 |
* 'rating_counts', |
| 419 |
* 'review_count', |
| 420 |
* ); |
| 421 |
*/ |
| 422 |
public function clear_product_cache( $product, $updated_props ) { |
| 423 |
|
| 424 |
if ( $this->p->debug->enabled ) { |
| 425 |
|
| 426 |
$this->p->debug->log( 'product = ' . get_class( $product ) ); // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 427 |
} |
| 428 |
|
| 429 |
if ( $product instanceof WC_Product ) { // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 430 |
|
| 431 |
$product_id = $this->p->util->wc->get_product_id( $product ); // Returns product id from product object. |
| 432 |
|
| 433 |
if ( $product_id ) $this->p->post->clear_cache( $product_id ); // Refresh the cache for a single post ID. |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
/* |
| 438 |
* Refresh the post ID cache after WooCommerce updates the product object. |
| 439 |
*/ |
| 440 |
public function refresh_product_cache( $product, $data_store ) { |
| 441 |
|
| 442 |
if ( $this->p->debug->enabled ) { |
| 443 |
|
| 444 |
$this->p->debug->log( 'product = ' . get_class( $product ) ); // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 445 |
} |
| 446 |
|
| 447 |
if ( $product instanceof WC_Product ) { // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 448 |
|
| 449 |
$product_id = $this->p->util->wc->get_product_id( $product ); // Returns product id from product object. |
| 450 |
|
| 451 |
/* |
| 452 |
* Refresh the cache for a single post ID. |
| 453 |
* |
| 454 |
* This method will only execute once per post ID per page load. |
| 455 |
*/ |
| 456 |
if ( $product_id ) $this->p->post->refresh_cache( $product_id ); // Refresh the cache for a single post ID. |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
public function action_admin_post_head( $mod ) { |
| 461 |
|
| 462 |
if ( $this->p->debug->enabled ) { |
| 463 |
|
| 464 |
$this->p->debug->mark(); |
| 465 |
} |
| 466 |
|
| 467 |
$user_id = get_current_user_id(); |
| 468 |
|
| 469 |
$this->include_frontend_libs( $user_id ); |
| 470 |
} |
| 471 |
|
| 472 |
public function action_scheduled_task_started( $user_id ) { |
| 473 |
|
| 474 |
if ( $this->p->debug->enabled ) { |
| 475 |
|
| 476 |
$this->p->debug->mark(); |
| 477 |
} |
| 478 |
|
| 479 |
$this->include_frontend_libs( $user_id ); |
| 480 |
} |
| 481 |
|
| 482 |
/* |
| 483 |
* Maybe load missing WooCommerce front-end libraries for 'the_content' filter. |
| 484 |
*/ |
| 485 |
public function include_frontend_libs( $user_id = null ) { |
| 486 |
|
| 487 |
if ( $this->p->debug->enabled ) { |
| 488 |
|
| 489 |
$this->p->debug->mark(); |
| 490 |
} |
| 491 |
|
| 492 |
WC()->frontend_includes(); |
| 493 |
|
| 494 |
WC()->initialize_session(); |
| 495 |
} |
| 496 |
|
| 497 |
/* |
| 498 |
* WooCommerce product attributes do not have their own webpages - product attribute query strings are used to |
| 499 |
* pre-fill product selections on the front-end. The |
| 500 |
* WpssoIntegEcomWooCommerce->filter_request_url_query_cache_disable() method removes all product attributes from |
| 501 |
* the request URL, and if the $request_url and $canonical_url values match, the filter returns false (ie. do not |
| 502 |
* disable the cache). |
| 503 |
*/ |
| 504 |
public function filter_request_url_query_cache_disable( $cache_disable, $request_url, $canonical_url, $mod ) { |
| 505 |
|
| 506 |
if ( is_product() ) { |
| 507 |
|
| 508 |
if ( false !== strpos( $request_url, 'attribute_' ) ) { |
| 509 |
|
| 510 |
$request_url_no_attrs = preg_replace( '/[\?\&]attribute_[^=]+=[^\&]*/', '', $request_url ); |
| 511 |
|
| 512 |
if ( $request_url_no_attrs === $canonical_url ) { |
| 513 |
|
| 514 |
return false; |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
return $cache_disable; |
| 520 |
} |
| 521 |
|
| 522 |
public function filter_head_cache_index( $cache_index ) { |
| 523 |
|
| 524 |
if ( $this->p->debug->enabled ) { |
| 525 |
|
| 526 |
$this->p->debug->mark(); |
| 527 |
} |
| 528 |
|
| 529 |
return $cache_index . '_currency:' . $this->get_product_currency(); |
| 530 |
} |
| 531 |
|
| 532 |
/* |
| 533 |
* Add information about the product $mod to the body tag class. |
| 534 |
* |
| 535 |
* $classes = An array of body class names. |
| 536 |
*/ |
| 537 |
public function filter_body_class( array $classes, array $mod ) { |
| 538 |
|
| 539 |
if ( SucomUtilWP::is_mod_post_type( $mod, $this->prod_post_type ) ) { |
| 540 |
|
| 541 |
if ( $product = $this->p->util->wc->get_product( $mod[ 'id' ] ) ) { |
| 542 |
|
| 543 |
$product_type = $this->p->util->wc->get_product_type( $product ); |
| 544 |
|
| 545 |
$classes[] = 'wpsso-wc-product-type-' . $product_type; |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
return $classes; |
| 550 |
} |
| 551 |
|
| 552 |
public function filter_use_post( $use_post ) { |
| 553 |
|
| 554 |
if ( $this->p->debug->enabled ) { |
| 555 |
|
| 556 |
$this->p->debug->mark(); |
| 557 |
} |
| 558 |
|
| 559 |
/* |
| 560 |
* Note that in_the_loop() can be true in both archive and singular pages. |
| 561 |
*/ |
| 562 |
if ( in_the_loop() ) { |
| 563 |
|
| 564 |
if ( $this->p->debug->enabled ) { |
| 565 |
|
| 566 |
$this->p->debug->log( 'exiting early: in the loop' ); |
| 567 |
} |
| 568 |
|
| 569 |
return $use_post; |
| 570 |
|
| 571 |
} elseif ( is_account_page() ) { |
| 572 |
|
| 573 |
if ( $this->p->debug->enabled ) { |
| 574 |
|
| 575 |
$this->p->debug->log( 'is_account_page() is true' ); |
| 576 |
} |
| 577 |
|
| 578 |
$use_post = $this->page_ids[ 'account' ]; |
| 579 |
|
| 580 |
} elseif ( is_cart() ) { |
| 581 |
|
| 582 |
if ( $this->p->debug->enabled ) { |
| 583 |
|
| 584 |
$this->p->debug->log( 'is_cart() is true' ); |
| 585 |
} |
| 586 |
|
| 587 |
$use_post = $this->page_ids[ 'cart' ]; |
| 588 |
|
| 589 |
} elseif ( is_checkout() ) { |
| 590 |
|
| 591 |
if ( $this->p->debug->enabled ) { |
| 592 |
|
| 593 |
$this->p->debug->log( 'is_checkout() is true' ); |
| 594 |
} |
| 595 |
|
| 596 |
$use_post = $this->page_ids[ 'checkout' ]; |
| 597 |
|
| 598 |
} elseif ( is_shop() ) { |
| 599 |
|
| 600 |
if ( $this->p->debug->enabled ) { |
| 601 |
|
| 602 |
$this->p->debug->log( 'is_shop() is true' ); |
| 603 |
} |
| 604 |
|
| 605 |
$use_post = $this->page_ids[ 'shop' ]; |
| 606 |
|
| 607 |
} elseif ( is_product() ) { |
| 608 |
|
| 609 |
if ( $this->p->debug->enabled ) { |
| 610 |
|
| 611 |
$this->p->debug->log( 'is_product() is true' ); |
| 612 |
} |
| 613 |
|
| 614 |
$use_post = true; |
| 615 |
|
| 616 |
} else { |
| 617 |
|
| 618 |
if ( $this->p->debug->enabled ) { |
| 619 |
|
| 620 |
$this->p->debug->log( 'exiting early: not a woocommerce page' ); |
| 621 |
} |
| 622 |
|
| 623 |
return $use_post; |
| 624 |
} |
| 625 |
|
| 626 |
if ( $this->p->debug->enabled ) { |
| 627 |
|
| 628 |
$this->p->debug->log( 'woocommerce use_post is ' . SucomUtil::get_use_post_string( $use_post ) ); |
| 629 |
} |
| 630 |
|
| 631 |
return $use_post; |
| 632 |
} |
| 633 |
|
| 634 |
public function filter_get_post_type( $post_type, $post_id ) { |
| 635 |
|
| 636 |
if ( $this->p->debug->enabled ) { |
| 637 |
|
| 638 |
$this->p->debug->mark(); |
| 639 |
} |
| 640 |
|
| 641 |
if ( $post_id === $this->page_ids[ 'shop' ] ) { |
| 642 |
|
| 643 |
if ( $this->p->debug->enabled ) { |
| 644 |
|
| 645 |
$this->p->debug->log( 'post is shop page' ); |
| 646 |
} |
| 647 |
|
| 648 |
return $this->prod_post_type; |
| 649 |
} |
| 650 |
|
| 651 |
return $post_type; |
| 652 |
} |
| 653 |
|
| 654 |
public function filter_schema_type( $type_id, array $mod, $is_custom ) { |
| 655 |
|
| 656 |
if ( $this->p->debug->enabled ) { |
| 657 |
|
| 658 |
$this->p->debug->mark(); |
| 659 |
} |
| 660 |
|
| 661 |
if ( $is_custom ) { // Skip if we have a custom type from the post meta. |
| 662 |
|
| 663 |
if ( $this->p->debug->enabled ) { |
| 664 |
|
| 665 |
$this->p->debug->log( 'exiting early: custom schema type is ' . $type_id ); |
| 666 |
} |
| 667 |
|
| 668 |
return $type_id; |
| 669 |
} |
| 670 |
|
| 671 |
if ( $mod[ 'is_post' ] ) { |
| 672 |
|
| 673 |
if ( $mod[ 'id' ] === $this->page_ids[ 'account' ] ) { |
| 674 |
|
| 675 |
$type_id = 'webpage.profile'; |
| 676 |
|
| 677 |
} elseif ( $mod[ 'id' ] === $this->page_ids[ 'cart' ] ) { |
| 678 |
|
| 679 |
$type_id = 'webpage.checkout'; |
| 680 |
|
| 681 |
} elseif ( $mod[ 'id' ] === $this->page_ids[ 'checkout' ] ) { |
| 682 |
|
| 683 |
$type_id = 'webpage.checkout'; |
| 684 |
|
| 685 |
} elseif ( $mod[ 'id' ] === $this->page_ids[ 'shop' ] ) { |
| 686 |
|
| 687 |
$type_id = $this->p->schema->get_schema_type_id( 'pta_' . $this->prod_post_type ); |
| 688 |
|
| 689 |
} elseif ( $this->p->util->wc->is_mod_variable( $mod ) ) { |
| 690 |
|
| 691 |
if ( $this->prod_post_type === $mod[ 'post_type' ] ) { |
| 692 |
|
| 693 |
$type_id = $this->p->schema->get_schema_type_id( 'product_group' ); |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
return $type_id; |
| 699 |
} |
| 700 |
|
| 701 |
public function filter_schema_type_post_type_labels( array $type_labels ) { |
| 702 |
|
| 703 |
if ( $this->p->debug->enabled ) { |
| 704 |
|
| 705 |
$this->p->debug->mark(); |
| 706 |
} |
| 707 |
|
| 708 |
$type_labels[ 'schema_type_for_product_group' ] = __( 'Products Group', 'wpsso' ); |
| 709 |
|
| 710 |
asort( $type_labels ); |
| 711 |
|
| 712 |
return $type_labels; |
| 713 |
} |
| 714 |
|
| 715 |
public function filter_primary_tax_slug( $tax_slug, $mod ) { |
| 716 |
|
| 717 |
if ( $this->p->debug->enabled ) { |
| 718 |
|
| 719 |
$this->p->debug->mark(); |
| 720 |
} |
| 721 |
|
| 722 |
if ( $this->prod_post_type === $mod[ 'post_type' ] ) { |
| 723 |
|
| 724 |
if ( 'category' === $tax_slug ) { |
| 725 |
|
| 726 |
return $this->cat_taxonomy; |
| 727 |
|
| 728 |
} elseif ( 'tag' === $tax_slug ) { |
| 729 |
|
| 730 |
return $this->tag_taxonomy; |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
return $tax_slug; |
| 735 |
} |
| 736 |
|
| 737 |
/* |
| 738 |
* Return false to prevent the comment or post content from being used. |
| 739 |
*/ |
| 740 |
public function filter_the_content_seed( $content, $mod ) { |
| 741 |
|
| 742 |
if ( $this->p->debug->enabled ) { |
| 743 |
|
| 744 |
$this->p->debug->mark(); |
| 745 |
} |
| 746 |
|
| 747 |
if ( $mod[ 'is_post' ] ) { |
| 748 |
|
| 749 |
if ( $mod[ 'id' ] === $this->page_ids[ 'account' ] ) { |
| 750 |
|
| 751 |
$content = false; |
| 752 |
|
| 753 |
} elseif ( $mod[ 'id' ] === $this->page_ids[ 'cart' ] ) { |
| 754 |
|
| 755 |
$content = false; |
| 756 |
|
| 757 |
} elseif ( $mod[ 'id' ] === $this->page_ids[ 'checkout' ] ) { |
| 758 |
|
| 759 |
$content = false; |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
return $content; |
| 764 |
} |
| 765 |
|
| 766 |
public function filter_description_seed( $desc_text, $mod, $num_hashtags, $md_key ) { |
| 767 |
|
| 768 |
if ( $this->p->debug->enabled ) { |
| 769 |
|
| 770 |
$this->p->debug->mark(); |
| 771 |
} |
| 772 |
|
| 773 |
if ( $mod[ 'is_post' ] ) { |
| 774 |
|
| 775 |
if ( $mod[ 'id' ] === $this->page_ids[ 'account' ] ) { |
| 776 |
|
| 777 |
$desc_text = __( 'My account page', 'woocommerce' ); |
| 778 |
|
| 779 |
} elseif ( $mod[ 'id' ] === $this->page_ids[ 'cart' ] ) { |
| 780 |
|
| 781 |
$desc_text = __( 'Cart page', 'woocommerce' ); |
| 782 |
|
| 783 |
} elseif ( $mod[ 'id' ] === $this->page_ids[ 'checkout' ] ) { |
| 784 |
|
| 785 |
$desc_text = __( 'Checkout page', 'woocommerce' ); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
return $desc_text; |
| 790 |
} |
| 791 |
|
| 792 |
/* |
| 793 |
* Note that images can only be attached to a post ID. |
| 794 |
* |
| 795 |
* See WpssoMedia->get_attached_images(). |
| 796 |
*/ |
| 797 |
public function filter_attached_image_ids( $image_ids, $post_id ) { |
| 798 |
|
| 799 |
if ( $this->p->debug->enabled ) { |
| 800 |
|
| 801 |
$this->p->debug->mark(); |
| 802 |
} |
| 803 |
|
| 804 |
$mod = $this->p->post->get_mod( $post_id ); |
| 805 |
|
| 806 |
if ( ! SucomUtilWP::is_mod_post_type( $mod, $this->prod_post_type ) ) { |
| 807 |
|
| 808 |
return $image_ids; |
| 809 |
|
| 810 |
} elseif ( false === ( $product = $this->p->util->wc->get_product( $post_id ) ) ) { |
| 811 |
|
| 812 |
return $image_ids; |
| 813 |
} |
| 814 |
|
| 815 |
$attach_ids = null; |
| 816 |
|
| 817 |
if ( is_callable( array( $product, 'get_gallery_image_ids' ) ) ) { |
| 818 |
|
| 819 |
$attach_ids = $product->get_gallery_image_ids(); |
| 820 |
|
| 821 |
} elseif ( is_callable( array( $product, 'get_gallery_attachment_ids' ) ) ) { |
| 822 |
|
| 823 |
$attach_ids = $product->get_gallery_attachment_ids(); |
| 824 |
} |
| 825 |
|
| 826 |
if ( is_array( $attach_ids ) ) { |
| 827 |
|
| 828 |
$image_ids = array_merge( $image_ids, $attach_ids ); |
| 829 |
} |
| 830 |
|
| 831 |
return $image_ids; // array_unique() is applied to the returned array. |
| 832 |
} |
| 833 |
|
| 834 |
public function filter_term_image_ids( $image_ids, $size_names, $term_id ) { |
| 835 |
|
| 836 |
if ( SucomUtilWP::is_term_tax_slug( $term_id, $this->cat_taxonomy ) || |
| 837 |
SucomUtilWP::is_term_tax_slug( $term_id, $this->tag_taxonomy ) ) { |
| 838 |
|
| 839 |
$pid = get_metadata( 'term', $term_id, $key = 'thumbnail_id', $single = true ); |
| 840 |
|
| 841 |
if ( ! empty( $pid ) ) { |
| 842 |
|
| 843 |
$image_ids[] = $pid; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
return $image_ids; |
| 848 |
} |
| 849 |
|
| 850 |
/* |
| 851 |
* See WpssoIntegEcomAbstractWooCommerceBrands->filter_get_md_defaults_woocommerce(). |
| 852 |
*/ |
| 853 |
public function filter_get_md_defaults( array $md_defs, array $mod ) { |
| 854 |
|
| 855 |
if ( $this->p->debug->enabled ) { |
| 856 |
|
| 857 |
$this->p->debug->mark(); |
| 858 |
} |
| 859 |
|
| 860 |
if ( ! SucomUtilWP::is_mod_post_type( $mod, $this->prod_post_type ) ) { |
| 861 |
|
| 862 |
/* |
| 863 |
* Maybe force the schema type for the account, cart, and checkout pages. |
| 864 |
*/ |
| 865 |
if ( $schema_type = $this->filter_schema_type( null, $mod, $is_custom = false ) ) { |
| 866 |
|
| 867 |
$md_defs[ 'schema_type' ] = $schema_type; |
| 868 |
} |
| 869 |
|
| 870 |
return $md_defs; |
| 871 |
|
| 872 |
} elseif ( false === ( $product = $this->p->util->wc->get_product( $mod[ 'id' ] ) ) ) { |
| 873 |
|
| 874 |
return $md_defs; |
| 875 |
} |
| 876 |
|
| 877 |
$md_defs[ 'og_type' ] = 'product'; |
| 878 |
|
| 879 |
if ( function_exists( 'is_sitemap' ) && is_sitemap() ) { |
| 880 |
|
| 881 |
if ( $this->p->debug->enabled ) { |
| 882 |
|
| 883 |
$this->p->debug->log( 'skipping getting md defaults for sitemap' ); |
| 884 |
} |
| 885 |
|
| 886 |
return $md_defs; |
| 887 |
} |
| 888 |
|
| 889 |
if ( $this->p->debug->enabled ) { |
| 890 |
|
| 891 |
$this->p->debug->mark( 'getting product defaults' ); // Begin timer. |
| 892 |
|
| 893 |
$this->p->debug->log( 'product = ' . get_class( $product ) ); // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 894 |
} |
| 895 |
|
| 896 |
$product_incl_vat = $this->p->options[ 'plugin_product_include_vat' ] ? true : false; |
| 897 |
$product_price = $this->get_product_price( $product ); |
| 898 |
$product_price_fmtd = $this->get_product_price_formatted( $product, $product_price, $product_incl_vat ); |
| 899 |
$product_currency = $this->get_product_currency(); |
| 900 |
$product_avail = $this->get_product_avail( $product ); |
| 901 |
|
| 902 |
if ( $this->p->debug->enabled ) { |
| 903 |
|
| 904 |
$this->p->debug->log( 'product_incl_vat = ' . ( $product_incl_vat ? 'true' : 'false' ) ); |
| 905 |
$this->p->debug->log( 'product_price = ' . $product_price ); |
| 906 |
$this->p->debug->log( 'product_price_fmtd = ' . $product_price_fmtd ); |
| 907 |
$this->p->debug->log( 'product_currency = ' . $product_currency ); |
| 908 |
$this->p->debug->log( 'product_avail = ' . $product_avail ); |
| 909 |
} |
| 910 |
|
| 911 |
if ( $product->is_on_sale() ) { |
| 912 |
|
| 913 |
$md_defs[ 'product_price_type' ]= 'https://schema.org/SalePrice'; |
| 914 |
|
| 915 |
} else $md_defs[ 'product_price_type' ] = 'https://schema.org/ListPrice'; |
| 916 |
|
| 917 |
$md_defs[ 'product_price' ] = $product_price_fmtd; |
| 918 |
$md_defs[ 'product_currency' ] = $product_currency; |
| 919 |
$md_defs[ 'product_avail' ] = $product_avail; |
| 920 |
$md_defs[ 'product_retailer_part_no' ] = $product->get_sku(); // Product SKU. |
| 921 |
|
| 922 |
/* |
| 923 |
* Get product shipping dimensions and weight. |
| 924 |
*/ |
| 925 |
if ( $this->p->debug->enabled ) { |
| 926 |
|
| 927 |
$this->p->debug->log( 'getting product shipping dimensions' ); |
| 928 |
} |
| 929 |
|
| 930 |
list( |
| 931 |
$md_defs[ 'product_shipping_length_value' ], |
| 932 |
$md_defs[ 'product_shipping_length_units' ], |
| 933 |
$md_defs[ 'product_shipping_width_value' ], |
| 934 |
$md_defs[ 'product_shipping_width_units' ], |
| 935 |
$md_defs[ 'product_shipping_height_value' ], |
| 936 |
$md_defs[ 'product_shipping_height_units' ], |
| 937 |
$md_defs[ 'product_shipping_weight_value' ], |
| 938 |
$md_defs[ 'product_shipping_weight_units' ], |
| 939 |
) = $this->get_shipping_length_width_height_weight( $product ); |
| 940 |
|
| 941 |
/* |
| 942 |
* Add book, event, and service offer defaults. |
| 943 |
*/ |
| 944 |
foreach ( array( 'schema_book_offer', 'schema_event_offer', 'schema_service_offer' ) as $md_pre ) { |
| 945 |
|
| 946 |
$md_defs = SucomUtil::preg_grep_keys( '/^' . $md_pre . '_/', $md_defs, $invert = true ); // Remove any pre-existing offers. |
| 947 |
|
| 948 |
$avail_variations = $this->p->util->wc->get_available_variations( $product ); // Always returns an array. |
| 949 |
|
| 950 |
if ( ! empty( $avail_variations ) ) { |
| 951 |
|
| 952 |
foreach( $avail_variations as $num => $variation ) { |
| 953 |
|
| 954 |
if ( $var_product = $this->p->util->wc->get_variation_product( $variation ) ) { // Get the product for the variation. |
| 955 |
|
| 956 |
$var_product_price = $this->get_product_price( $var_product ); |
| 957 |
$var_product_price_fmtd = $this->get_product_price_formatted( $var_product, $var_product_price, $product_incl_vat ); |
| 958 |
|
| 959 |
$md_defs[ $md_pre . '_name_' . $num ] = $this->get_product_variation_title( $mod, $var_product, $variation ); |
| 960 |
$md_defs[ $md_pre . '_url_' . $num ] = $this->get_product_url( $var_product ); |
| 961 |
$md_defs[ $md_pre . '_price_' . $num ] = $var_product_price_fmtd; |
| 962 |
$md_defs[ $md_pre . '_currency_' . $num ] = $product_currency; |
| 963 |
$md_defs[ $md_pre . '_avail_' . $num ] = $this->get_product_avail( $var_product ); |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
} else { |
| 968 |
|
| 969 |
$md_defs[ $md_pre . '_name_0' ] = $this->get_product_title( $product ); |
| 970 |
$md_defs[ $md_pre . '_url_0' ] = $this->get_product_url( $product ); |
| 971 |
$md_defs[ $md_pre . '_price_0' ] = $product_price_fmtd; |
| 972 |
$md_defs[ $md_pre . '_currency_0' ] = $product_currency; |
| 973 |
$md_defs[ $md_pre . '_avail_0' ] = $product_avail; |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
$md_defs = apply_filters( 'wpsso_get_md_defaults_woocommerce', $md_defs, $mod ); |
| 978 |
|
| 979 |
if ( $this->p->debug->enabled ) { |
| 980 |
|
| 981 |
$this->p->debug->mark( 'getting product defaults' ); // End timer. |
| 982 |
} |
| 983 |
|
| 984 |
return $md_defs; |
| 985 |
} |
| 986 |
|
| 987 |
/* |
| 988 |
* Disable options where the value comes from the e-commerce plugin. |
| 989 |
*/ |
| 990 |
public function filter_get_post_options( array $md_opts, $post_id, array $mod ) { |
| 991 |
|
| 992 |
if ( $this->p->debug->enabled ) { |
| 993 |
|
| 994 |
$this->p->debug->mark(); |
| 995 |
} |
| 996 |
|
| 997 |
$prod_opts = $this->filter_get_md_defaults( array(), $mod ); |
| 998 |
|
| 999 |
foreach ( $prod_opts as $opt_key => $opt_val ) { |
| 1000 |
|
| 1001 |
$md_opts[ $opt_key ] = $opt_val; |
| 1002 |
|
| 1003 |
$md_opts[ $opt_key . ':disabled' ] = true; |
| 1004 |
} |
| 1005 |
|
| 1006 |
return $md_opts; |
| 1007 |
} |
| 1008 |
|
| 1009 |
public function filter_og_seed( array $mt_og, array $mod ) { |
| 1010 |
|
| 1011 |
if ( $this->p->debug->enabled ) { |
| 1012 |
|
| 1013 |
$this->p->debug->mark(); |
| 1014 |
} |
| 1015 |
|
| 1016 |
if ( ! SucomUtilWP::is_mod_post_type( $mod, $this->prod_post_type ) ) { |
| 1017 |
|
| 1018 |
return $mt_og; |
| 1019 |
|
| 1020 |
} elseif ( false === ( $product = $this->p->util->wc->get_product( $mod[ 'id' ] ) ) ) { |
| 1021 |
|
| 1022 |
return $mt_og; |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* |
| 1026 |
* Get the pre-sorted product meta tags, with the og:type meta tag top-most in the array. |
| 1027 |
*/ |
| 1028 |
$mt_ecom = SucomUtil::get_mt_product_seed( $this->og_type, array( 'og:type' => $this->og_type ) ); |
| 1029 |
|
| 1030 |
$this->add_mt_product( $mt_ecom, $mod, $product ); |
| 1031 |
|
| 1032 |
$this->add_mt_ratings( $mt_ecom, $mod, $product ); |
| 1033 |
|
| 1034 |
if ( $this->p->avail[ 'p' ][ 'schema' ] ) { |
| 1035 |
|
| 1036 |
$this->add_mt_reviews( $mt_ecom, $mod, $product ); |
| 1037 |
|
| 1038 |
if ( $this->p->util->wc->is_product_variable( $product ) ) { |
| 1039 |
|
| 1040 |
$schema_type = $this->p->schema->get_mod_schema_type_id( $mod, $use_md_opts = true ); |
| 1041 |
|
| 1042 |
$og_mt_suffix = 'product.group' === $schema_type ? 'variants' : 'offers'; |
| 1043 |
|
| 1044 |
/* |
| 1045 |
* Add product variants or offers. |
| 1046 |
*/ |
| 1047 |
if ( apply_filters( 'wpsso_og_add_mt_' . $og_mt_suffix, true, $mod ) ) { |
| 1048 |
|
| 1049 |
if ( $this->p->debug->enabled ) { |
| 1050 |
|
| 1051 |
$this->p->debug->log( 'add ' . $og_mt_suffix . ' meta tags is true' ); |
| 1052 |
} |
| 1053 |
|
| 1054 |
/* |
| 1055 |
* Similar to the WooCommerce method, except it does not exclude out of stock variations. |
| 1056 |
*/ |
| 1057 |
$avail_variations = $this->p->util->wc->get_available_variations( $product ); // Always returns an array. |
| 1058 |
|
| 1059 |
if ( $this->p->debug->enabled ) { |
| 1060 |
|
| 1061 |
$this->p->debug->log( count( $avail_variations ) . ' variations returned' ); |
| 1062 |
} |
| 1063 |
|
| 1064 |
foreach( $avail_variations as $num => $variation ) { |
| 1065 |
|
| 1066 |
/* |
| 1067 |
* Get the pre-sorted product meta tags, with the og:type meta tag top-most in the array. |
| 1068 |
*/ |
| 1069 |
$mt_ecom_var = SucomUtil::get_mt_product_seed( $this->og_type ); |
| 1070 |
|
| 1071 |
$this->add_mt_product( $mt_ecom_var, $mod, $variation ); |
| 1072 |
|
| 1073 |
if ( ! empty( $mt_ecom_var ) ) { |
| 1074 |
|
| 1075 |
$mt_ecom[ $this->og_type . ':' . $og_mt_suffix ][] = $mt_ecom_var; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
} elseif ( $this->p->debug->enabled ) { |
| 1080 |
|
| 1081 |
$this->p->debug->log( 'add variants meta tags is false' ); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
$mt_ecom = apply_filters( 'wpsso_og_ecom_woocommerce', $mt_ecom, $mod ); |
| 1087 |
|
| 1088 |
return array_merge( $mt_og, $mt_ecom ); |
| 1089 |
} |
| 1090 |
|
| 1091 |
public function filter_tag_names_seed( $tags, array $mod ) { |
| 1092 |
|
| 1093 |
if ( $this->p->debug->enabled ) { |
| 1094 |
|
| 1095 |
$this->p->debug->mark(); |
| 1096 |
} |
| 1097 |
|
| 1098 |
if ( ! SucomUtilWP::is_mod_post_type( $mod, $this->prod_post_type ) ) { |
| 1099 |
|
| 1100 |
return $tags; |
| 1101 |
} |
| 1102 |
|
| 1103 |
return wp_get_post_terms( $mod[ 'id' ], $this->tag_taxonomy, $args = array( 'fields' => 'names' ) ); |
| 1104 |
} |
| 1105 |
|
| 1106 |
/* |
| 1107 |
* $mixed must be a post object, product object, or variation array. |
| 1108 |
*/ |
| 1109 |
public function filter_import_product_attributes( array $md_opts, array $mod, $mixed ) { |
| 1110 |
|
| 1111 |
if ( $this->p->debug->enabled ) { |
| 1112 |
|
| 1113 |
$this->p->debug->mark(); |
| 1114 |
} |
| 1115 |
|
| 1116 |
$is_variation = false; // Default value. |
| 1117 |
|
| 1118 |
if ( function_exists( 'is_sitemap' ) && is_sitemap() ) { // Nothing to do - no attribute values in sitemaps. |
| 1119 |
|
| 1120 |
return $md_opts; |
| 1121 |
|
| 1122 |
} elseif ( $product = $this->p->util->wc->get_variation_product( $mixed ) ) { // Get the product for the variation. |
| 1123 |
|
| 1124 |
if ( $this->p->debug->enabled ) { |
| 1125 |
|
| 1126 |
$this->p->debug->log( 'using variation array' ); |
| 1127 |
} |
| 1128 |
|
| 1129 |
$is_variation = true; |
| 1130 |
$variation = $mixed; |
| 1131 |
|
| 1132 |
} elseif ( is_object( $mixed ) ) { |
| 1133 |
|
| 1134 |
if ( $mixed instanceof WC_Product ) { // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 1135 |
|
| 1136 |
$product = $mixed; |
| 1137 |
|
| 1138 |
} elseif ( $mixed instanceof WP_Post ) { |
| 1139 |
|
| 1140 |
if ( SucomUtilWP::is_post_type( $mixed, $this->prod_post_type ) ) { |
| 1141 |
|
| 1142 |
$product = $this->p->util->wc->get_product( $mixed->ID ); |
| 1143 |
|
| 1144 |
} else return $md_opts; |
| 1145 |
|
| 1146 |
} else return $md_opts; |
| 1147 |
|
| 1148 |
} else return $md_opts; // $mixed is not a variation array, product or post object. |
| 1149 |
|
| 1150 |
if ( $this->p->debug->enabled ) { |
| 1151 |
|
| 1152 |
$this->p->debug->mark( 'importing product attributes' ); // Begin timer. |
| 1153 |
} |
| 1154 |
|
| 1155 |
$product_id = $this->p->util->wc->get_product_id( $product ); // Returns product id from product object. |
| 1156 |
$product_parent_id = $is_variation ? $product->get_parent_id() : $product_id; |
| 1157 |
$product_parent = $is_variation ? $this->p->util->wc->get_product( $product_parent_id ) : $product; |
| 1158 |
$attr_md_index = WpssoConfig::get_attr_md_index(); |
| 1159 |
$md_keys_multi = WpssoConfig::get_md_keys_multi(); |
| 1160 |
|
| 1161 |
foreach ( $attr_md_index as $opt_attr_key => $md_key ) { |
| 1162 |
|
| 1163 |
if ( empty( $md_key ) ) continue; |
| 1164 |
|
| 1165 |
if ( empty( $this->p->options[ $opt_attr_key ] ) ) continue; |
| 1166 |
|
| 1167 |
$attr_name = $this->p->options[ $opt_attr_key ]; // Untranslated name. |
| 1168 |
$attr_value = false; |
| 1169 |
|
| 1170 |
if ( $this->p->debug->enabled ) { |
| 1171 |
|
| 1172 |
$this->p->debug->log( 'checking for ' . $attr_name . ' attribute values' ); |
| 1173 |
} |
| 1174 |
|
| 1175 |
$values = array(); |
| 1176 |
|
| 1177 |
/* |
| 1178 |
* Product variation. |
| 1179 |
*/ |
| 1180 |
if ( $is_variation ) { |
| 1181 |
|
| 1182 |
if ( '' !== ( $attr_value = $product->get_attribute( $attr_name ) ) ) { |
| 1183 |
|
| 1184 |
if ( $this->p->debug->enabled ) { |
| 1185 |
|
| 1186 |
$this->p->debug->log( 'assigning ' . $attr_name . ' value to ' . $md_key . ' = ' . $attr_value ); |
| 1187 |
} |
| 1188 |
|
| 1189 |
$values[] = $attr_value; |
| 1190 |
|
| 1191 |
/* |
| 1192 |
* Fallback to the default value. |
| 1193 |
*/ |
| 1194 |
} elseif ( '' !== ( $attr_value = $product_parent->get_variation_default_attribute( $attr_name ) ) ) { |
| 1195 |
|
| 1196 |
if ( $this->p->debug->enabled ) { |
| 1197 |
|
| 1198 |
$this->p->debug->log( 'assigning ' . $attr_name . ' default value to ' . $md_key . ' = ' . $attr_value ); |
| 1199 |
} |
| 1200 |
|
| 1201 |
$values[] = $attr_value; |
| 1202 |
} |
| 1203 |
|
| 1204 |
/* |
| 1205 |
* Main product or simple product. |
| 1206 |
*/ |
| 1207 |
} else { |
| 1208 |
|
| 1209 |
/* |
| 1210 |
* Skip attributes with select options (example: Small | Medium | Large). |
| 1211 |
*/ |
| 1212 |
if ( $this->is_variation_selectable_attribute( $product_id, $product, $attr_name ) ) { |
| 1213 |
|
| 1214 |
if ( $this->p->debug->enabled ) { |
| 1215 |
|
| 1216 |
$this->p->debug->log( 'skipping ' . $attr_name . ' selectable value = ' . $attr_value ); |
| 1217 |
} |
| 1218 |
|
| 1219 |
} elseif ( '' !== ( $attr_value = $product->get_attribute( $attr_name ) ) ) { |
| 1220 |
|
| 1221 |
if ( $this->p->debug->enabled ) { |
| 1222 |
|
| 1223 |
$this->p->debug->log( 'assigning ' . $attr_name . ' value to ' . $md_key . ' = ' . $attr_value ); |
| 1224 |
} |
| 1225 |
|
| 1226 |
$values[] = $attr_value; |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
/* |
| 1231 |
* Check if the value(s) should be split into multiple numeric options. |
| 1232 |
*/ |
| 1233 |
if ( ! empty( $values ) ) { // Just in case. |
| 1234 |
|
| 1235 |
if ( ! empty( $md_keys_multi[ $md_key ] ) ) { |
| 1236 |
|
| 1237 |
/* |
| 1238 |
* If $attr_value was not an array, then $values[ 0 ] will be a string - split that string into an array. |
| 1239 |
*/ |
| 1240 |
if ( ! is_array( $attr_value ) ) { |
| 1241 |
|
| 1242 |
$values = array_map( 'trim', explode( ',', reset( $values ) ) ); |
| 1243 |
|
| 1244 |
if ( $this->p->debug->enabled ) { |
| 1245 |
|
| 1246 |
$this->p->debug->log( 'exploded ' . $md_key . ' into array of ' . count( $values ) . ' elements' ); |
| 1247 |
} |
| 1248 |
} |
| 1249 |
|
| 1250 |
$this->p->util->maybe_renum_md_key( $md_opts, $md_key, $values, $is_disabled = true ); |
| 1251 |
|
| 1252 |
} else { |
| 1253 |
|
| 1254 |
$md_opts[ $md_key ] = reset( $values ); |
| 1255 |
|
| 1256 |
$md_opts[ $md_key . ':disabled' ] = true; |
| 1257 |
|
| 1258 |
if ( $this->p->debug->enabled ) { |
| 1259 |
|
| 1260 |
$this->p->debug->log( 'option ' . $md_key . ' = ' . print_r( $md_opts[ $md_key ], true ) ); |
| 1261 |
} |
| 1262 |
|
| 1263 |
/* |
| 1264 |
* If this is a '_value' option, add the '_units' option. |
| 1265 |
*/ |
| 1266 |
$this->p->util->maybe_add_md_key_units( $md_opts, $md_key ); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
} |
| 1270 |
|
| 1271 |
if ( $this->p->debug->enabled ) { |
| 1272 |
|
| 1273 |
$this->p->debug->mark( 'importing product attributes' ); // End timer. |
| 1274 |
} |
| 1275 |
|
| 1276 |
return $md_opts; |
| 1277 |
} |
| 1278 |
|
| 1279 |
/* |
| 1280 |
* This method does not return an array. |
| 1281 |
* |
| 1282 |
* $mt_ecom must be passed by reference to add the required meta tags. |
| 1283 |
* $mod contains the main product information (not the variant). |
| 1284 |
* $mixed is a product object or variation array. |
| 1285 |
*/ |
| 1286 |
private function add_mt_product( array &$mt_ecom, array $mod, $mixed ) { // Pass by reference is OK. |
| 1287 |
|
| 1288 |
if ( $this->p->debug->enabled ) { |
| 1289 |
|
| 1290 |
$this->p->debug->mark(); |
| 1291 |
} |
| 1292 |
|
| 1293 |
$is_variation = false; // Default value. |
| 1294 |
|
| 1295 |
if ( $product = $this->p->util->wc->get_variation_product( $mixed ) ) { // Get the product for the variation. |
| 1296 |
|
| 1297 |
$is_variation = true; |
| 1298 |
$variation = $mixed; |
| 1299 |
|
| 1300 |
} elseif ( is_object( $mixed ) ) { |
| 1301 |
|
| 1302 |
if ( $mixed instanceof WC_Product ) { // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 1303 |
|
| 1304 |
$product = $mixed; |
| 1305 |
|
| 1306 |
} elseif ( $mixed instanceof WP_Post ) { |
| 1307 |
|
| 1308 |
if ( SucomUtilWP::is_post_type( $mixed, $this->prod_post_type ) ) { |
| 1309 |
|
| 1310 |
$product = $this->p->util->wc->get_product( $mixed->ID ); |
| 1311 |
|
| 1312 |
} else return false; |
| 1313 |
|
| 1314 |
} else return false; |
| 1315 |
|
| 1316 |
} else return false; // $mixed is not a variation array, product or post object. |
| 1317 |
|
| 1318 |
if ( $this->p->debug->enabled ) { |
| 1319 |
|
| 1320 |
$this->p->debug->log( 'product = ' . get_class( $product ) ); // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 1321 |
} |
| 1322 |
|
| 1323 |
$product_id = $this->p->util->wc->get_product_id( $product ); // Returns product id from product object. |
| 1324 |
$product_parent_id = $is_variation ? $product->get_parent_id() : $product_id; |
| 1325 |
$product_parent = $is_variation ? $this->p->util->wc->get_product( $product_parent_id ) : $product; |
| 1326 |
$product_incl_vat = $this->p->options[ 'plugin_product_include_vat' ] ? true : false; |
| 1327 |
$product_price = $this->get_product_price( $product ); |
| 1328 |
$product_price_fmtd = $this->get_product_price_formatted( $product, $product_price, $product_incl_vat ); |
| 1329 |
$product_currency = $this->get_product_currency(); |
| 1330 |
|
| 1331 |
if ( $this->p->debug->enabled ) { |
| 1332 |
|
| 1333 |
$this->p->debug->log( 'product_id = ' . $product_id ); |
| 1334 |
$this->p->debug->log( 'product_parent_id = ' . $product_parent_id ); |
| 1335 |
$this->p->debug->log( 'product_parent = ' . get_class( $product_parent ) ); |
| 1336 |
$this->p->debug->log( 'product_incl_vat = ' . ( $product_incl_vat ? 'true' : 'false' ) ); |
| 1337 |
$this->p->debug->log( 'product_price = ' . $product_price ); |
| 1338 |
$this->p->debug->log( 'product_price_fmtd = ' . $product_price_fmtd ); |
| 1339 |
$this->p->debug->log( 'product_currency = ' . $product_currency ); |
| 1340 |
} |
| 1341 |
|
| 1342 |
if ( $is_variation ) { |
| 1343 |
|
| 1344 |
/* |
| 1345 |
* $mod contains the main product information (not the variant). |
| 1346 |
* $product contains the main product object (not the variant). |
| 1347 |
* $variation contains the variation information. |
| 1348 |
*/ |
| 1349 |
$this->add_product_variation_title( $mt_ecom, $mod, $product, $variation ); |
| 1350 |
$this->add_product_variation_description( $mt_ecom, $mod, $product, $variation ); |
| 1351 |
|
| 1352 |
} else { |
| 1353 |
|
| 1354 |
$mt_ecom[ 'product:title' ] = $this->p->page->get_title( $mod, $md_key = 'schema_title', $max_len = 'schema_title' ); |
| 1355 |
$mt_ecom[ 'product:description' ] = $this->p->page->get_description( $mod, $md_key = 'schema_desc', $max_len = 'schema_desc' ); |
| 1356 |
} |
| 1357 |
|
| 1358 |
/* |
| 1359 |
* Note that the 'product:retailer_item_id' value is important for Schema ProductGroup and hasVariant |
| 1360 |
* markup. The 'product:item_group_id' must be provided for variations, but not simple products (as they |
| 1361 |
* are not in a product group). |
| 1362 |
*/ |
| 1363 |
$mt_ecom[ 'product:updated_time' ] = $mod[ 'post_modified_time' ]; |
| 1364 |
$mt_ecom[ 'product:url' ] = $this->get_product_url( $product ); |
| 1365 |
$mt_ecom[ 'product:retailer_item_id' ] = $product_id; // Product ID. |
| 1366 |
$mt_ecom[ 'product:retailer_part_no' ] = $product->get_sku(); // Product SKU. |
| 1367 |
$mt_ecom[ 'product:item_group_id' ] = $is_variation ? $product_parent_id : ''; // Product variation group ID. |
| 1368 |
$mt_ecom[ 'product:is_virtual' ] = $product->is_virtual(); |
| 1369 |
|
| 1370 |
/* |
| 1371 |
* Add product availability. |
| 1372 |
* |
| 1373 |
* See https://woocommerce.github.io/code-reference/classes/WC-Product.html#method_get_manage_stock. |
| 1374 |
* See https://woocommerce.github.io/code-reference/classes/WC-Product.html#method_get_stock_quantity. |
| 1375 |
*/ |
| 1376 |
if ( $product->get_manage_stock() ) { // Returns trus, false, or 'parent'. |
| 1377 |
|
| 1378 |
if ( $this->p->debug->enabled ) { |
| 1379 |
|
| 1380 |
$this->p->debug->log( 'product stock is managed' ); |
| 1381 |
} |
| 1382 |
|
| 1383 |
$mt_ecom[ 'product:quantity' ] = $product->get_stock_quantity(); |
| 1384 |
} |
| 1385 |
|
| 1386 |
/* |
| 1387 |
* Add product availability. |
| 1388 |
* |
| 1389 |
* See https://woocommerce.github.io/code-reference/classes/WC-Product.html#method_is_in_stock. |
| 1390 |
* See https://woocommerce.github.io/code-reference/classes/WC-Product.html#method_is_on_backorder. |
| 1391 |
* |
| 1392 |
* Hook 'woocommerce_product_is_in_stock' (returns true or false) to customize the "in stock" status. |
| 1393 |
*/ |
| 1394 |
if ( $product->is_in_stock() ) { |
| 1395 |
|
| 1396 |
if ( $this->p->debug->enabled ) { |
| 1397 |
|
| 1398 |
$this->p->debug->log( 'product is in stock' ); |
| 1399 |
} |
| 1400 |
|
| 1401 |
$mt_ecom[ 'product:availability' ] = 'https://schema.org/InStock'; |
| 1402 |
|
| 1403 |
} elseif ( $product->is_on_backorder() ) { |
| 1404 |
|
| 1405 |
if ( $this->p->debug->enabled ) { |
| 1406 |
|
| 1407 |
$this->p->debug->log( 'product is on backorder' ); |
| 1408 |
} |
| 1409 |
|
| 1410 |
$mt_ecom[ 'product:availability' ] = 'https://schema.org/BackOrder'; |
| 1411 |
|
| 1412 |
} else { |
| 1413 |
|
| 1414 |
if ( $this->p->debug->enabled ) { |
| 1415 |
|
| 1416 |
$this->p->debug->log( 'product is out of stock' ); |
| 1417 |
} |
| 1418 |
|
| 1419 |
$mt_ecom[ 'product:availability' ] = 'https://schema.org/OutOfStock'; |
| 1420 |
} |
| 1421 |
|
| 1422 |
if ( $is_variation ) { |
| 1423 |
|
| 1424 |
$var_mod = $this->p->page->get_mod( $product_id ); |
| 1425 |
|
| 1426 |
$var_opts = $var_mod[ 'obj' ]->get_options( $var_mod[ 'id' ] ); |
| 1427 |
|
| 1428 |
/* |
| 1429 |
* Variation product attributes. |
| 1430 |
*/ |
| 1431 |
if ( $this->p->debug->enabled ) { |
| 1432 |
|
| 1433 |
$this->p->debug->log( 'getting variation product attributes' ); |
| 1434 |
} |
| 1435 |
|
| 1436 |
/* |
| 1437 |
* Format the WooCommerce meta data as WordPress meta data. |
| 1438 |
*/ |
| 1439 |
$var_wp_meta = $this->p->util->wc->get_product_wp_meta( $product ); |
| 1440 |
|
| 1441 |
/* |
| 1442 |
* See WpssoIntegEcomWooAddGtin->filter_wc_variation_alt_options(). |
| 1443 |
*/ |
| 1444 |
$alt_opts = apply_filters( 'wpsso_wc_variation_alt_options', array() ); |
| 1445 |
|
| 1446 |
/* |
| 1447 |
* The 'import_custom_fields' filter is executed before the 'wpsso_get_md_options' and |
| 1448 |
* 'wpsso_get_post_options' filters, custom field values may get overwritten by these filters. |
| 1449 |
* |
| 1450 |
* The 'import_custom_fields' filter is also executed before the 'wpsso_get_md_defaults' and |
| 1451 |
* 'wpsso_get_post_defaults' filters, so submitted form values that are identical to their defaults |
| 1452 |
* can be removed before saving the options array. |
| 1453 |
* |
| 1454 |
* See WpssoPost->get_options(). |
| 1455 |
* See WpssoAbstractWpMeta->get_defaults(). |
| 1456 |
* See WpssoUtilCustomFields->filter_import_custom_fields(). |
| 1457 |
* See WpssoIntegEcomWooCommerce->add_mt_product(). |
| 1458 |
* See WpssoIntegEcomWooAddGtin->filter_wc_variation_alt_options(). |
| 1459 |
*/ |
| 1460 |
if ( $this->p->debug->enabled ) { |
| 1461 |
|
| 1462 |
$this->p->debug->log( 'applying filters "wpsso_import_custom_fields" for variation id ' . $product_id . ' metadata' ); |
| 1463 |
} |
| 1464 |
|
| 1465 |
$var_opts = apply_filters( 'wpsso_import_custom_fields', $var_opts, $mod, $var_wp_meta, $alt_opts ); |
| 1466 |
|
| 1467 |
unset( $var_wp_meta, $alt_opts ); |
| 1468 |
|
| 1469 |
/* |
| 1470 |
* Since WPSSO Core v14.2.0. |
| 1471 |
* |
| 1472 |
* See WpssoIntegEcomWooCommerce->add_mt_product(). |
| 1473 |
*/ |
| 1474 |
if ( $this->p->debug->enabled ) { |
| 1475 |
|
| 1476 |
$this->p->debug->log( 'applying filters "wpsso_import_product_attributes" for variation id ' . $product_id ); |
| 1477 |
} |
| 1478 |
|
| 1479 |
$var_opts = apply_filters( 'wpsso_import_product_attributes', $var_opts, $mod, $variation ); |
| 1480 |
|
| 1481 |
/* |
| 1482 |
* Since WPSSO Core v12.2.0. |
| 1483 |
* |
| 1484 |
* Overwrite parent options with those of the child, allowing only undefined child options to be |
| 1485 |
* inherited from the parent. |
| 1486 |
*/ |
| 1487 |
if ( $this->p->debug->enabled ) { |
| 1488 |
|
| 1489 |
$this->p->debug->log( 'inheriting variable parent metadata options' ); |
| 1490 |
} |
| 1491 |
|
| 1492 |
$product_parent_opts = $var_mod[ 'obj' ]->get_inherited_md_opts( $var_mod ); |
| 1493 |
|
| 1494 |
if ( ! empty( $product_parent_opts ) ) { |
| 1495 |
|
| 1496 |
$var_opts = SucomUtil::array_merge_recursive_distinct( $product_parent_opts, $var_opts ); |
| 1497 |
} |
| 1498 |
|
| 1499 |
unset( $product_parent_opts ); |
| 1500 |
|
| 1501 |
/* |
| 1502 |
* Add custom fields meta data to the Open Graph meta tags. |
| 1503 |
*/ |
| 1504 |
$this->p->og->add_data_og_type_md( $mt_ecom, 'product', $var_opts ); |
| 1505 |
|
| 1506 |
} else { // Not a variation. |
| 1507 |
|
| 1508 |
/* |
| 1509 |
* Product variations do not have terms (categories or tags) so skip this section for variations. |
| 1510 |
* |
| 1511 |
* Retrieve the terms of the taxonomy that are attached to the post ID. get_the_terms() returns an |
| 1512 |
* array of WP_Term objects, false if there are no terms (or the post does not exist), or a |
| 1513 |
* WP_Error object on failure. |
| 1514 |
*/ |
| 1515 |
$terms = get_the_terms( $product_id, $this->tag_taxonomy ); |
| 1516 |
|
| 1517 |
if ( is_array( $terms ) ) { // Not false or WP_Error object. |
| 1518 |
|
| 1519 |
foreach( $terms as $term ) { |
| 1520 |
|
| 1521 |
$mt_ecom[ 'product:tag' ][] = $term->name; |
| 1522 |
} |
| 1523 |
} |
| 1524 |
} |
| 1525 |
|
| 1526 |
if ( $this->p->debug->enabled ) { |
| 1527 |
|
| 1528 |
$this->p->debug->log( 'getting price and pretax price formatted' ); |
| 1529 |
} |
| 1530 |
|
| 1531 |
$mt_ecom[ 'product:pretax_price:amount' ] = $this->get_product_price_formatted( $product, $product_price, false ); // Exclude VAT. |
| 1532 |
$mt_ecom[ 'product:pretax_price:currency' ] = $product_currency; |
| 1533 |
$mt_ecom[ 'product:price:amount' ] = $product_price_fmtd; |
| 1534 |
$mt_ecom[ 'product:price:currency' ] = $product_currency; |
| 1535 |
$mt_ecom[ 'product:price:vat_included' ] = $product_incl_vat; |
| 1536 |
|
| 1537 |
if ( method_exists( $product, 'get_regular_price' ) ) { |
| 1538 |
|
| 1539 |
if ( $this->p->debug->enabled ) { |
| 1540 |
|
| 1541 |
$this->p->debug->log( 'getting original price formatted' ); |
| 1542 |
} |
| 1543 |
|
| 1544 |
$regular_price = $product->get_regular_price(); |
| 1545 |
$regular_price_fmtd = $this->get_product_price_formatted( $product, $regular_price, $product_incl_vat ); |
| 1546 |
|
| 1547 |
if ( $this->p->debug->enabled ) { |
| 1548 |
|
| 1549 |
$this->p->debug->log( 'get_regular_price() returned "' . $regular_price . '" (type is ' . gettype( $regular_price ) . ')' ); |
| 1550 |
} |
| 1551 |
|
| 1552 |
if ( '' !== $regular_price_fmtd && $regular_price_fmtd !== $product_price_fmtd ) { |
| 1553 |
|
| 1554 |
$mt_ecom[ 'product:original_price:type' ] = 'https://schema.org/ListPrice'; |
| 1555 |
$mt_ecom[ 'product:original_price:amount' ] = $regular_price_fmtd; |
| 1556 |
$mt_ecom[ 'product:original_price:currency' ] = $product_currency; |
| 1557 |
} |
| 1558 |
|
| 1559 |
} elseif ( $this->p->debug->enabled ) { |
| 1560 |
|
| 1561 |
$this->p->debug->log( 'product get_regular_price() method not found' ); |
| 1562 |
} |
| 1563 |
|
| 1564 |
if ( $product->is_on_sale() ) { |
| 1565 |
|
| 1566 |
if ( $this->p->debug->enabled ) { |
| 1567 |
|
| 1568 |
$this->p->debug->log( 'product is on sale' ); |
| 1569 |
} |
| 1570 |
|
| 1571 |
$mt_ecom[ 'product:price:type' ] = 'https://schema.org/SalePrice'; |
| 1572 |
$mt_ecom[ 'product:sale_price:type' ] = 'https://schema.org/SalePrice'; |
| 1573 |
|
| 1574 |
if ( ! empty( $mt_ecom[ 'product:original_price:type' ] ) ) { |
| 1575 |
|
| 1576 |
$mt_ecom[ 'product:original_price:type' ] = 'https://schema.org/StrikethroughPrice'; |
| 1577 |
} |
| 1578 |
|
| 1579 |
if ( method_exists( $product, 'get_sale_price' ) ) { |
| 1580 |
|
| 1581 |
$sale_price = $product->get_sale_price(); |
| 1582 |
$sale_price_fmtd = $this->get_product_price_formatted( $product, $sale_price, $product_incl_vat ); |
| 1583 |
|
| 1584 |
if ( $this->p->debug->enabled ) { |
| 1585 |
|
| 1586 |
$this->p->debug->log( 'get_sale_price() returned "' . $sale_price . '" (type is ' . gettype( $sale_price ) . ')' ); |
| 1587 |
} |
| 1588 |
|
| 1589 |
$mt_ecom[ 'product:sale_price:amount' ] = $sale_price_fmtd; |
| 1590 |
$mt_ecom[ 'product:sale_price:currency' ] = $product_currency; |
| 1591 |
|
| 1592 |
} elseif ( $this->p->debug->enabled ) { |
| 1593 |
|
| 1594 |
$this->p->debug->log( 'product get_sale_price() method not found' ); |
| 1595 |
} |
| 1596 |
|
| 1597 |
if ( method_exists( $product, 'get_date_on_sale_from' ) ) { |
| 1598 |
|
| 1599 |
if ( $product->get_date_on_sale_from() ) { // Since WC v3.0. |
| 1600 |
|
| 1601 |
$sale_start_ts = $product->get_date_on_sale_from()->getTimestamp(); |
| 1602 |
|
| 1603 |
$mt_ecom[ 'product:sale_price_dates:start' ] = date( 'c', $sale_start_ts ); |
| 1604 |
$mt_ecom[ 'product:sale_price_dates:start_date' ] = date( 'Y-m-d', $sale_start_ts ); |
| 1605 |
$mt_ecom[ 'product:sale_price_dates:start_time' ] = date( 'H:i:s', $sale_start_ts ); |
| 1606 |
$mt_ecom[ 'product:sale_price_dates:start_timezone' ] = wc_timezone_string(); |
| 1607 |
$mt_ecom[ 'product:sale_price_dates:start_iso' ] = date( 'c', $sale_start_ts ); |
| 1608 |
|
| 1609 |
} elseif ( $this->p->debug->enabled ) { |
| 1610 |
|
| 1611 |
$this->p->debug->log( 'product get_date_on_sale_from() returned an empty value' ); |
| 1612 |
} |
| 1613 |
|
| 1614 |
} elseif ( $this->p->debug->enabled ) { |
| 1615 |
|
| 1616 |
$this->p->debug->log( 'product get_date_on_sale_from() method not found' ); |
| 1617 |
} |
| 1618 |
|
| 1619 |
if ( method_exists( $product, 'get_date_on_sale_to' ) ) { |
| 1620 |
|
| 1621 |
if ( $product->get_date_on_sale_to() ) { // Since WC v3.0. |
| 1622 |
|
| 1623 |
$sale_end_ts = $product->get_date_on_sale_to()->getTimestamp(); |
| 1624 |
|
| 1625 |
$mt_ecom[ 'product:sale_price_dates:end' ] = date( 'c', $sale_end_ts ); |
| 1626 |
$mt_ecom[ 'product:sale_price_dates:end_date' ] = date( 'Y-m-d', $sale_end_ts ); |
| 1627 |
$mt_ecom[ 'product:sale_price_dates:end_time' ] = date( 'H:i:s', $sale_end_ts ); |
| 1628 |
$mt_ecom[ 'product:sale_price_dates:end_timezone' ] = wc_timezone_string(); |
| 1629 |
$mt_ecom[ 'product:sale_price_dates:end_iso' ] = date( 'c', $sale_end_ts ); |
| 1630 |
|
| 1631 |
} elseif ( $this->p->debug->enabled ) { |
| 1632 |
|
| 1633 |
$this->p->debug->log( 'product get_date_on_sale_to() returned an empty value' ); |
| 1634 |
} |
| 1635 |
|
| 1636 |
} elseif ( $this->p->debug->enabled ) { |
| 1637 |
|
| 1638 |
$this->p->debug->log( 'product get_date_on_sale_to() method not found' ); |
| 1639 |
} |
| 1640 |
|
| 1641 |
} elseif ( $this->p->debug->enabled ) { |
| 1642 |
|
| 1643 |
$this->p->debug->log( 'product is not on sale' ); |
| 1644 |
} |
| 1645 |
|
| 1646 |
/* |
| 1647 |
* Get product shipping dimensions and weight. |
| 1648 |
*/ |
| 1649 |
if ( $this->p->debug->enabled ) { |
| 1650 |
|
| 1651 |
$this->p->debug->log( 'getting product shipping dimensions' ); |
| 1652 |
} |
| 1653 |
|
| 1654 |
list( |
| 1655 |
$mt_ecom[ 'product:shipping_length:value' ], |
| 1656 |
$mt_ecom[ 'product:shipping_length:units' ], |
| 1657 |
$mt_ecom[ 'product:shipping_width:value' ], |
| 1658 |
$mt_ecom[ 'product:shipping_width:units' ], |
| 1659 |
$mt_ecom[ 'product:shipping_height:value' ], |
| 1660 |
$mt_ecom[ 'product:shipping_height:units' ], |
| 1661 |
$mt_ecom[ 'product:shipping_weight:value' ], |
| 1662 |
$mt_ecom[ 'product:shipping_weight:units' ], |
| 1663 |
) = $this->get_shipping_length_width_height_weight( $product ); |
| 1664 |
|
| 1665 |
/* |
| 1666 |
* Add shipping offers. |
| 1667 |
*/ |
| 1668 |
$this->add_mt_shipping_offers( $mt_ecom, $mod, $product, $product_parent ); |
| 1669 |
} |
| 1670 |
|
| 1671 |
/* |
| 1672 |
* Add shipping information. |
| 1673 |
* |
| 1674 |
* The $shipping_class_id corresponds to the "Shipping class" selected when editing a product. |
| 1675 |
* |
| 1676 |
* Unless $product is a WC_Product_Variation, $product and $product_parent will be the same. |
| 1677 |
*/ |
| 1678 |
private function add_mt_shipping_offers( array &$mt_ecom, $mod, WC_Product $product, WC_Product $product_parent ) { // Pass by reference is OK. |
| 1679 |
|
| 1680 |
if ( $this->p->debug->enabled ) { |
| 1681 |
|
| 1682 |
$this->p->debug->mark(); |
| 1683 |
} |
| 1684 |
|
| 1685 |
/* |
| 1686 |
* Define static variables only once to prevent duplicate WooCommerce queries. |
| 1687 |
*/ |
| 1688 |
static $shipping_zones = null; |
| 1689 |
static $shipping_continents = null; |
| 1690 |
static $shipping_countries = null; |
| 1691 |
static $shipping_states = null; |
| 1692 |
static $shipping_enabled = null; |
| 1693 |
static $world_zone_id = null; |
| 1694 |
static $world_zone_obj = null; |
| 1695 |
static $world_zone_name = null; |
| 1696 |
static $world_zone_methods = null; |
| 1697 |
|
| 1698 |
if ( null === $shipping_zones ) { |
| 1699 |
|
| 1700 |
$shipping_zones = WC_Shipping_Zones::get_zones( $context = 'admin' ); |
| 1701 |
$shipping_zones = apply_filters( 'wpsso_wc_shipping_zones', $shipping_zones ); |
| 1702 |
$shipping_continents = WC()->countries->get_shipping_continents(); |
| 1703 |
$shipping_countries = WC()->countries->get_shipping_countries(); |
| 1704 |
$shipping_states = WC()->countries->get_states(); |
| 1705 |
$shipping_enabled = $shipping_continents || $shipping_countries ? true : false; |
| 1706 |
|
| 1707 |
$world_zone_id = 0; |
| 1708 |
$world_zone_obj = WC_Shipping_Zones::get_zone( $world_zone_id ); // Locations not covered by your other zones. |
| 1709 |
$world_zone_name = __( 'World', 'wpsso' ); |
| 1710 |
$world_zone_methods = $world_zone_obj->get_shipping_methods(); |
| 1711 |
} |
| 1712 |
|
| 1713 |
$product_id = $this->p->util->wc->get_product_id( $product ); |
| 1714 |
$product_url = $this->get_product_url( $product ); |
| 1715 |
$product_can_ship = $product->needs_shipping(); |
| 1716 |
$product_parent_url = $this->get_product_url( $product_parent ); |
| 1717 |
$product_currency = $this->get_product_currency(); |
| 1718 |
|
| 1719 |
if ( $this->p->debug->enabled ) { |
| 1720 |
|
| 1721 |
$this->p->debug->log( 'shipping enabled = ' . ( $shipping_enabled ? 'true' : 'false' ) ); |
| 1722 |
$this->p->debug->log( 'product can ship = ' . ( $product_can_ship ? 'true' : 'false' ) ); |
| 1723 |
} |
| 1724 |
|
| 1725 |
/* |
| 1726 |
* The WPSSO WCSDT add-on returns true at priority -1000. |
| 1727 |
* |
| 1728 |
* The WPSSO GMF add-on returns true or false (depending on the 'gmf_add_shipping' option) at priority 1000. |
| 1729 |
* |
| 1730 |
* See WpssoGmfXml->get(). |
| 1731 |
* See WpssoWcsdtFilters->__construct(). |
| 1732 |
*/ |
| 1733 |
if ( $product_can_ship && $shipping_enabled && apply_filters( 'wpsso_og_add_mt_shipping_offers', false, $mod ) ) { |
| 1734 |
|
| 1735 |
if ( $this->p->debug->enabled ) { |
| 1736 |
|
| 1737 |
$this->p->debug->log( 'creating shipping offers' ); |
| 1738 |
} |
| 1739 |
|
| 1740 |
$shipping_class_id = $product->get_shipping_class_id(); // 0 or a selected product "Shipping class". |
| 1741 |
|
| 1742 |
$mt_ecom[ 'product:shipping_class_id' ] = $shipping_class_id; |
| 1743 |
$mt_ecom[ 'product:shipping_offers' ] = array(); |
| 1744 |
|
| 1745 |
if ( $this->p->debug->enabled ) { |
| 1746 |
|
| 1747 |
$this->p->debug->log( 'shipping class id = ' . $shipping_class_id ); |
| 1748 |
} |
| 1749 |
|
| 1750 |
/* |
| 1751 |
* Each zone consists of shipping locations and shipping methods. |
| 1752 |
*/ |
| 1753 |
static $local_fifo = array(); |
| 1754 |
|
| 1755 |
foreach ( $shipping_zones as $zone_id => $zone ) { |
| 1756 |
|
| 1757 |
if ( $this->p->debug->enabled ) { |
| 1758 |
|
| 1759 |
$this->p->debug->log( 'zone id = ' . $zone_id ); |
| 1760 |
} |
| 1761 |
|
| 1762 |
if ( isset( $local_fifo[ $zone_id ] ) ) { |
| 1763 |
|
| 1764 |
list( $zone_obj, $zone_name, $zone_locations, $zone_methods ) = $local_fifo[ $zone_id ]; |
| 1765 |
|
| 1766 |
} else { |
| 1767 |
|
| 1768 |
$zone_obj = WC_Shipping_Zones::get_zone( $zone_id ); |
| 1769 |
$zone_name = $zone_obj->get_zone_name( $context = 'admin' ); |
| 1770 |
$zone_locations = $zone_obj->get_zone_locations( $context = 'admin' ); |
| 1771 |
$zone_methods = $zone_obj->get_shipping_methods( $enabled_only = true, $context = 'admin' ); |
| 1772 |
$zone_methods = apply_filters( 'wpsso_wc_shipping_zone_methods', $zone_methods, $zone_id, $zone_obj ); |
| 1773 |
|
| 1774 |
/* |
| 1775 |
* Maybe limit the number of array elements. |
| 1776 |
*/ |
| 1777 |
$local_fifo = SucomUtil::array_slice_fifo( $local_fifo, WPSSO_CACHE_ARRAY_FIFO_MAX ); |
| 1778 |
|
| 1779 |
$local_fifo[ $zone_id ] = array( $zone_obj, $zone_name, $zone_locations, $zone_methods ); |
| 1780 |
|
| 1781 |
if ( $this->p->debug->enabled ) { |
| 1782 |
|
| 1783 |
$this->p->debug->log_size( 'local_fifo', $local_fifo ); |
| 1784 |
} |
| 1785 |
} |
| 1786 |
|
| 1787 |
$zone_ship_dest = $this->get_zone_shipping_destinations( $zone_id, $zone_obj, $zone_locations, |
| 1788 |
$shipping_continents, $shipping_countries, $shipping_states, $product_parent_url ); |
| 1789 |
|
| 1790 |
/* |
| 1791 |
* Get shipping methods and rates for this zone. |
| 1792 |
*/ |
| 1793 |
foreach ( $zone_methods as $method_inst_id => $method_obj ) { |
| 1794 |
|
| 1795 |
/* |
| 1796 |
* Returns false or a shipping offer options array. |
| 1797 |
*/ |
| 1798 |
$shipping_offer = $this->get_zone_method_shipping_offer( $zone_id, $zone_name, |
| 1799 |
$method_inst_id, $method_obj, $shipping_class_id, $product, $product_parent ); |
| 1800 |
|
| 1801 |
if ( $shipping_offer ) { |
| 1802 |
|
| 1803 |
if ( empty( $zone_ship_dest ) ) { // Ships to the World. |
| 1804 |
|
| 1805 |
if ( $this->p->debug->enabled ) { |
| 1806 |
|
| 1807 |
$this->p->debug->log( 'shipping offer for zone ' . $zone_name . ' ships to world' ); |
| 1808 |
} |
| 1809 |
|
| 1810 |
$shipping_offer[ 'shipping_destinations' ] = $this->get_world_shipping_destinations(); |
| 1811 |
|
| 1812 |
} else { |
| 1813 |
|
| 1814 |
if ( $this->p->debug->enabled ) { |
| 1815 |
|
| 1816 |
$this->p->debug->log( 'shipping offer for zone ' . $zone_name . ' ships to destinations' ); |
| 1817 |
} |
| 1818 |
|
| 1819 |
$shipping_offer[ 'shipping_destinations' ] = $zone_ship_dest; |
| 1820 |
} |
| 1821 |
|
| 1822 |
$mt_ecom[ 'product:shipping_offers' ][] = $shipping_offer; |
| 1823 |
|
| 1824 |
} elseif ( $this->p->debug->enabled ) { |
| 1825 |
|
| 1826 |
$this->p->debug->log( 'no shipping offer for zone ' . $zone_name ); |
| 1827 |
} |
| 1828 |
|
| 1829 |
} // End of $zone_methods loop. |
| 1830 |
|
| 1831 |
unset( $method_inst_id, $method_obj, $shipping_offer ); |
| 1832 |
|
| 1833 |
} // End of $shipping_zones loop. |
| 1834 |
|
| 1835 |
unset( $zone_id, $zone, $zone_obj, $zone_name, $zone_locations, $zone_methods, $zone_ship_dest ); |
| 1836 |
|
| 1837 |
/* |
| 1838 |
* Get shipping methods and rates for the world zone. |
| 1839 |
*/ |
| 1840 |
if ( ! empty( $world_zone_methods ) ) { |
| 1841 |
|
| 1842 |
foreach ( $world_zone_methods as $method_inst_id => $method_obj ) { |
| 1843 |
|
| 1844 |
/* |
| 1845 |
* Returns false or a shipping offer options array. |
| 1846 |
*/ |
| 1847 |
$shipping_offer = $this->get_zone_method_shipping_offer( $world_zone_id, $world_zone_name, |
| 1848 |
$method_inst_id, $method_obj, $shipping_class_id, $product, $product_parent ); |
| 1849 |
|
| 1850 |
if ( $shipping_offer ) { |
| 1851 |
|
| 1852 |
$shipping_offer[ 'shipping_destinations' ] = $this->get_world_shipping_destinations(); |
| 1853 |
|
| 1854 |
$mt_ecom[ 'product:shipping_offers' ][] = $shipping_offer; |
| 1855 |
|
| 1856 |
} elseif ( $this->p->debug->enabled ) { |
| 1857 |
|
| 1858 |
$this->p->debug->log( 'no shipping offer for zone World' ); |
| 1859 |
} |
| 1860 |
|
| 1861 |
} // End of $world_zone_methods loop. |
| 1862 |
|
| 1863 |
unset ( $method_inst_id, $method_obj, $shipping_offer ); |
| 1864 |
|
| 1865 |
} elseif ( $this->p->debug->enabled ) { |
| 1866 |
|
| 1867 |
$this->p->debug->log( 'no shipping methods for zone World' ); |
| 1868 |
} |
| 1869 |
} |
| 1870 |
} |
| 1871 |
|
| 1872 |
private function add_mt_ratings( array &$mt_ecom, $mod, WC_Product $product ) { // Pass by reference is OK. |
| 1873 |
|
| 1874 |
if ( $this->p->debug->enabled ) { |
| 1875 |
|
| 1876 |
$this->p->debug->mark(); |
| 1877 |
} |
| 1878 |
|
| 1879 |
$wc_reviews_enabled = apply_filters( 'wpsso_og_add_wc_mt_reviews', $this->reviews_enabled ); |
| 1880 |
$wc_rating_enabled = apply_filters( 'wpsso_og_add_wc_mt_rating', $this->rating_enabled ); |
| 1881 |
|
| 1882 |
if ( apply_filters( 'wpsso_og_add_mt_rating', true, $mod ) ) { // Enabled by default. |
| 1883 |
|
| 1884 |
if ( $this->p->debug->enabled ) { |
| 1885 |
|
| 1886 |
$this->p->debug->log( 'add rating meta tags is true' ); |
| 1887 |
} |
| 1888 |
|
| 1889 |
/* |
| 1890 |
* Add rating meta tags if WooCommerce product reviews and review ratings are enabled. |
| 1891 |
*/ |
| 1892 |
if ( $wc_reviews_enabled && $wc_rating_enabled ) { |
| 1893 |
|
| 1894 |
if ( $this->p->debug->enabled ) { |
| 1895 |
|
| 1896 |
$this->p->debug->log( 'woocommerce reviews and ratings are enabled' ); |
| 1897 |
} |
| 1898 |
|
| 1899 |
$average_rating = (float) $product->get_average_rating(); |
| 1900 |
$rating_count = (int) $product->get_rating_count(); |
| 1901 |
$review_count = (int) $product->get_review_count(); |
| 1902 |
|
| 1903 |
/* |
| 1904 |
* An average rating value must be greater than 0. |
| 1905 |
*/ |
| 1906 |
if ( $average_rating > 0 ) { |
| 1907 |
|
| 1908 |
/* |
| 1909 |
* At least one rating or review is required. |
| 1910 |
*/ |
| 1911 |
if ( $rating_count > 0 || $review_count > 0 ) { |
| 1912 |
|
| 1913 |
if ( $this->p->debug->enabled ) { |
| 1914 |
|
| 1915 |
$this->p->debug->log( 'adding rating meta tags for product id ' . $mod[ 'id' ] ); |
| 1916 |
} |
| 1917 |
|
| 1918 |
$mt_ecom[ $this->og_type . ':rating:average' ] = $average_rating; |
| 1919 |
$mt_ecom[ $this->og_type . ':rating:count' ] = $rating_count; |
| 1920 |
$mt_ecom[ $this->og_type . ':rating:worst' ] = $this->worst_rating; |
| 1921 |
$mt_ecom[ $this->og_type . ':rating:best' ] = $this->best_rating; |
| 1922 |
$mt_ecom[ $this->og_type . ':review:count' ] = $review_count; |
| 1923 |
|
| 1924 |
if ( $this->p->debug->enabled ) { |
| 1925 |
|
| 1926 |
$this->p->debug->log( SucomUtil::preg_grep_keys( '/:(rating|review):/', $mt_ecom ) ); |
| 1927 |
} |
| 1928 |
|
| 1929 |
} else { |
| 1930 |
|
| 1931 |
if ( $this->p->debug->enabled ) { |
| 1932 |
|
| 1933 |
$this->p->debug->log( 'rating and review count is invalid (must be greater than 0)' ); |
| 1934 |
} |
| 1935 |
|
| 1936 |
$notice_msg = sprintf( __( 'The rating and review counts provided by WooCommerce for product ID %d are invalid.', |
| 1937 |
'wpsso' ), $mod[ 'id' ] ) . ' '; |
| 1938 |
|
| 1939 |
$notice_msg .= sprintf( __( 'The average rating is %.2f, but the rating count is %d and the review count is %d.', |
| 1940 |
'wpsso' ), $average_rating, $rating_count, $review_count ) . ' '; |
| 1941 |
|
| 1942 |
$notice_msg .= __( 'The rating count or the review count must be greater than 0.', 'wpsso' ); |
| 1943 |
|
| 1944 |
$this->p->notice->warn( $notice_msg ); |
| 1945 |
} |
| 1946 |
|
| 1947 |
} elseif ( $this->p->debug->enabled ) { |
| 1948 |
|
| 1949 |
$this->p->debug->log( 'average rating is invalid (must be greater than 0)' ); |
| 1950 |
} |
| 1951 |
|
| 1952 |
} elseif ( $this->p->debug->enabled ) { |
| 1953 |
|
| 1954 |
$this->p->debug->log( 'woocommerce ratings are disabled' ); |
| 1955 |
} |
| 1956 |
|
| 1957 |
} elseif ( $this->p->debug->enabled ) { |
| 1958 |
|
| 1959 |
$this->p->debug->log( 'add rating meta tags is false' ); |
| 1960 |
} |
| 1961 |
|
| 1962 |
} |
| 1963 |
|
| 1964 |
private function add_mt_reviews( array &$mt_ecom, $mod, WC_Product $product ) { // Pass by reference is OK. |
| 1965 |
|
| 1966 |
if ( $this->p->debug->enabled ) { |
| 1967 |
|
| 1968 |
$this->p->debug->mark(); |
| 1969 |
} |
| 1970 |
|
| 1971 |
$wc_reviews_enabled = apply_filters( 'wpsso_og_add_wc_mt_reviews', $this->reviews_enabled ); |
| 1972 |
$wc_rating_enabled = apply_filters( 'wpsso_og_add_wc_mt_rating', $this->rating_enabled ); |
| 1973 |
|
| 1974 |
if ( apply_filters( 'wpsso_og_add_mt_reviews', true, $mod ) ) { |
| 1975 |
|
| 1976 |
if ( $this->p->debug->enabled ) { |
| 1977 |
|
| 1978 |
$this->p->debug->log( 'add reviews meta tags is true' ); |
| 1979 |
} |
| 1980 |
|
| 1981 |
/* |
| 1982 |
* Add reviews array if WooCommerce product reviews are enabled. |
| 1983 |
*/ |
| 1984 |
if ( $wc_reviews_enabled ) { |
| 1985 |
|
| 1986 |
if ( $this->p->debug->enabled ) { |
| 1987 |
|
| 1988 |
$this->p->debug->log( 'woocommerce reviews are enabled' ); |
| 1989 |
} |
| 1990 |
|
| 1991 |
$mt_ecom[ $this->og_type . ':reviews' ] = $mod[ 'obj' ]->get_mt_reviews( $mod[ 'id' ], |
| 1992 |
$this->rating_meta, $this->worst_rating, $this->best_rating ); |
| 1993 |
|
| 1994 |
} elseif ( $this->p->debug->enabled ) { |
| 1995 |
|
| 1996 |
$this->p->debug->log( 'woocommerce reviews are disabled' ); |
| 1997 |
} |
| 1998 |
|
| 1999 |
} elseif ( $this->p->debug->enabled ) { |
| 2000 |
|
| 2001 |
$this->p->debug->log( 'add reviews meta tags is false' ); |
| 2002 |
} |
| 2003 |
|
| 2004 |
} |
| 2005 |
|
| 2006 |
private function get_world_shipping_destinations() { |
| 2007 |
|
| 2008 |
if ( $this->p->debug->enabled ) { |
| 2009 |
|
| 2010 |
$this->p->debug->mark(); |
| 2011 |
} |
| 2012 |
|
| 2013 |
static $local_cache = null; |
| 2014 |
|
| 2015 |
if ( null === $local_cache ) { |
| 2016 |
|
| 2017 |
$all_countries = SucomUtil::get_alpha2_countries(); |
| 2018 |
|
| 2019 |
foreach ( $all_countries as $country_code => $country_name ) { |
| 2020 |
|
| 2021 |
$local_cache[] = array( |
| 2022 |
'destination_id' => 'country-a2-' . $country_code, |
| 2023 |
'destination_rel' => '/', |
| 2024 |
'country_code' => $country_code, |
| 2025 |
); |
| 2026 |
} |
| 2027 |
} |
| 2028 |
|
| 2029 |
return $local_cache; |
| 2030 |
} |
| 2031 |
|
| 2032 |
private function get_zone_shipping_destinations( $zone_id, $zone_obj, $zone_locations, |
| 2033 |
$shipping_continents, $shipping_countries, $shipping_states, $product_parent_url ) { |
| 2034 |
|
| 2035 |
if ( $this->p->debug->enabled ) { |
| 2036 |
|
| 2037 |
$this->p->debug->mark(); |
| 2038 |
} |
| 2039 |
|
| 2040 |
static $local_fifo = array(); |
| 2041 |
|
| 2042 |
if ( isset( $local_fifo[ $zone_id ][ $product_parent_url ] ) ) { |
| 2043 |
|
| 2044 |
return $local_fifo[ $zone_id ][ $product_parent_url ]; |
| 2045 |
|
| 2046 |
} elseif ( isset( $local_fifo[ $zone_id ] ) ) { |
| 2047 |
|
| 2048 |
/* |
| 2049 |
* Maybe limit the number of array elements. |
| 2050 |
*/ |
| 2051 |
$local_fifo[ $zone_id ] = SucomUtil::array_slice_fifo( $local_fifo[ $zone_id ], WPSSO_CACHE_ARRAY_FIFO_MAX ); |
| 2052 |
|
| 2053 |
} else $local_fifo[ $zone_id ] = array(); |
| 2054 |
|
| 2055 |
$base_location = wc_get_base_location(); // Example: array( [country] => US [state] => CA ). |
| 2056 |
$zone_ship_dest = array(); |
| 2057 |
|
| 2058 |
foreach ( $zone_locations as $location_key => $location_obj ) { |
| 2059 |
|
| 2060 |
if ( $this->p->debug->enabled ) { |
| 2061 |
|
| 2062 |
$this->p->debug->log( 'zone location ' . $location_key. ' ' . $location_obj->type . ' = ' . $location_obj->code ); |
| 2063 |
} |
| 2064 |
|
| 2065 |
$dest_opts = array(); |
| 2066 |
|
| 2067 |
if ( 'continent' === $location_obj->type ) { |
| 2068 |
|
| 2069 |
if ( isset( $shipping_continents[ $location_obj->code ][ 'countries' ] ) ) { |
| 2070 |
|
| 2071 |
$dest_opts[ 'country_code' ] = $shipping_continents[ $location_obj->code ][ 'countries' ]; |
| 2072 |
} |
| 2073 |
|
| 2074 |
} elseif ( 'country' === $location_obj->type ) { |
| 2075 |
|
| 2076 |
if ( isset( $shipping_countries[ $location_obj->code ] ) ) { // Just in case. |
| 2077 |
|
| 2078 |
$dest_opts[ 'country_code' ] = $location_obj->code; |
| 2079 |
} |
| 2080 |
|
| 2081 |
} elseif ( 'state' === $location_obj->type ) { |
| 2082 |
|
| 2083 |
$codes = explode( ':', $location_obj->code ); |
| 2084 |
|
| 2085 |
if ( isset( $shipping_countries[ $codes[ 0 ] ] ) ) { // Just in case. |
| 2086 |
|
| 2087 |
if ( isset( $shipping_states[ $codes[ 0 ] ][ $codes[ 1 ] ] ) ) { // Just in case. |
| 2088 |
|
| 2089 |
$dest_opts[ 'country_code' ] = $codes[ 0 ]; |
| 2090 |
$dest_opts[ 'region_code' ] = $codes[ 1 ]; |
| 2091 |
} |
| 2092 |
} |
| 2093 |
|
| 2094 |
} elseif ( 'postcode' === $location_obj->type ) { |
| 2095 |
|
| 2096 |
$dest_opts[ 'country_code' ] = apply_filters( 'wpsso_wc_shipping_zone_location_postal_code_country', |
| 2097 |
$base_location[ 'country' ], $zone_obj, $location_key, $location_obj ); |
| 2098 |
|
| 2099 |
$dest_opts[ 'postal_code' ] = $location_obj->code; |
| 2100 |
} |
| 2101 |
|
| 2102 |
if ( $this->p->debug->enabled ) { |
| 2103 |
|
| 2104 |
$this->p->debug->log_arr( 'dest_opts', $dest_opts ); |
| 2105 |
} |
| 2106 |
|
| 2107 |
if ( ! empty( $dest_opts ) ) { |
| 2108 |
|
| 2109 |
$dest_opts[ 'destination_id' ] = 'dest-z' . $zone_id . '-d' . $location_key; |
| 2110 |
$dest_opts[ 'destination_rel' ] = $product_parent_url; |
| 2111 |
|
| 2112 |
$zone_ship_dest[] = $dest_opts; |
| 2113 |
} |
| 2114 |
|
| 2115 |
} // End of $zone_locations. |
| 2116 |
|
| 2117 |
unset( $location_key, $location_obj, $dest_opts ); |
| 2118 |
|
| 2119 |
if ( $this->p->debug->enabled ) { |
| 2120 |
|
| 2121 |
$this->p->debug->log_arr( 'zone_ship_dest', $zone_ship_dest ); |
| 2122 |
} |
| 2123 |
|
| 2124 |
$local_fifo[ $zone_id ][ $product_parent_url ] = $zone_ship_dest; |
| 2125 |
|
| 2126 |
unset ( $zone_ship_dest ); |
| 2127 |
|
| 2128 |
if ( $this->p->debug->enabled ) { |
| 2129 |
|
| 2130 |
$this->p->debug->log_size( 'local_fifo', $local_fifo ); |
| 2131 |
} |
| 2132 |
|
| 2133 |
return $local_fifo[ $zone_id ][ $product_parent_url ]; |
| 2134 |
} |
| 2135 |
|
| 2136 |
/* |
| 2137 |
* Returns false or a shipping offer options array. |
| 2138 |
*/ |
| 2139 |
private function get_zone_method_shipping_offer( $zone_id, $zone_name, $method_inst_id, $method_obj, $shipping_class_id, |
| 2140 |
WC_Product $product, WC_Product $product_parent ) { |
| 2141 |
|
| 2142 |
if ( $this->p->debug->enabled ) { |
| 2143 |
|
| 2144 |
$this->p->debug->log( 'method instance id = ' . $method_inst_id ); |
| 2145 |
} |
| 2146 |
|
| 2147 |
$product_price = $this->get_product_price( $product ); |
| 2148 |
$product_currency = $this->get_product_currency(); |
| 2149 |
$product_parent_url = $this->get_product_url( $product_parent ); |
| 2150 |
|
| 2151 |
$shipping_offer = false; |
| 2152 |
$shipping_class_obj = $shipping_class_id ? get_term_by( 'id', $shipping_class_id, 'product_shipping_class' ) : false; |
| 2153 |
$shipping_class_name = isset( $shipping_class_obj->name ) ? $shipping_class_obj->name : ''; |
| 2154 |
|
| 2155 |
$method_rate_id = $method_obj->get_rate_id(); |
| 2156 |
$method_name = $method_obj->get_title(); |
| 2157 |
$method_data = $method_obj->instance_settings; |
| 2158 |
|
| 2159 |
$rate_ids = explode( ':', $method_rate_id ); |
| 2160 |
$rate_name = empty( $shipping_class_name ) ? $method_name : $method_name . ' (' . $shipping_class_name . ')'; |
| 2161 |
$rate_type = reset( $rate_ids ); |
| 2162 |
$rate_cost = null; |
| 2163 |
|
| 2164 |
if ( $this->p->debug->enabled ) { |
| 2165 |
|
| 2166 |
$this->p->debug->log( 'rate type = ' . $rate_type ); |
| 2167 |
} |
| 2168 |
|
| 2169 |
if ( 'local_pickup' === $rate_type ) { // Pickup is not a shipping method. |
| 2170 |
|
| 2171 |
return false; |
| 2172 |
|
| 2173 |
} elseif ( 'free_shipping' === $rate_type ) { // Optimize. |
| 2174 |
|
| 2175 |
$rate_cost = 0; |
| 2176 |
|
| 2177 |
/* |
| 2178 |
* A shipping class for this product is available. |
| 2179 |
*/ |
| 2180 |
} elseif ( ! empty( $shipping_class_id ) && |
| 2181 |
isset( $method_data[ 'class_cost_' . $shipping_class_id ] ) && |
| 2182 |
'' !== $method_data[ 'class_cost_' . $shipping_class_id ] ) { // Allow for 0 and shortcodes. |
| 2183 |
|
| 2184 |
$rate_cost = $method_data[ 'class_cost_' . $shipping_class_id ]; |
| 2185 |
|
| 2186 |
/* |
| 2187 |
* A shipping class for this product is not available but a "no class cost" value is available. |
| 2188 |
*/ |
| 2189 |
} elseif ( empty( $shipping_class_id ) && |
| 2190 |
isset( $method_data[ 'no_class_cost'] ) && |
| 2191 |
'' !== $method_data[ 'no_class_cost'] ) { // Allow for 0 and shortcodes. |
| 2192 |
|
| 2193 |
$rate_cost = $method_data[ 'no_class_cost' ]; |
| 2194 |
|
| 2195 |
/* |
| 2196 |
* A shipping class for this product is not available and a "no class cost" value is not available. |
| 2197 |
*/ |
| 2198 |
} elseif ( isset( $method_data[ 'cost' ] ) && |
| 2199 |
'' !== $method_data[ 'cost' ] ) { // Allow for 0 and shortcodes. |
| 2200 |
|
| 2201 |
$rate_cost = $method_data[ 'cost' ]; |
| 2202 |
|
| 2203 |
/* |
| 2204 |
* Free shipping. |
| 2205 |
*/ |
| 2206 |
} else $rate_cost = 0; |
| 2207 |
|
| 2208 |
if ( $this->p->debug->enabled ) { |
| 2209 |
|
| 2210 |
$this->p->debug->log( 'rate cost = ' . $rate_cost ); |
| 2211 |
} |
| 2212 |
|
| 2213 |
/* |
| 2214 |
* Maybe resolve the shipping [fee] shortcode. |
| 2215 |
* |
| 2216 |
* See woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php. |
| 2217 |
*/ |
| 2218 |
if ( ! empty( $rate_cost ) && ! is_numeric( $rate_cost ) ) { |
| 2219 |
|
| 2220 |
if ( $this->p->debug->enabled ) { |
| 2221 |
|
| 2222 |
$this->p->debug->log( 'resolving [fee] shortcode for product qty 1 x price ' . $product_price ); |
| 2223 |
} |
| 2224 |
|
| 2225 |
/* |
| 2226 |
* evaluate_cost() is protected, so make it accessible. |
| 2227 |
* |
| 2228 |
* See https://www.php.net/manual/en/class.reflectionmethod.php. |
| 2229 |
*/ |
| 2230 |
$reflect = new ReflectionMethod( $method_obj, 'evaluate_cost' ); // Since PHP v5.4. |
| 2231 |
|
| 2232 |
$reflect->setAccessible( true ); |
| 2233 |
|
| 2234 |
/* |
| 2235 |
* Call the protected evaluate_cost method. |
| 2236 |
* |
| 2237 |
* See woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php line 75. |
| 2238 |
*/ |
| 2239 |
$rate_cost = $reflect->invoke( $method_obj, $rate_cost, array( 'qty' => 1, 'cost' => $product_price ) ); |
| 2240 |
|
| 2241 |
if ( $this->p->debug->enabled ) { |
| 2242 |
|
| 2243 |
$this->p->debug->log( 'resolved rate cost = ' . $rate_cost ); |
| 2244 |
} |
| 2245 |
} |
| 2246 |
|
| 2247 |
if ( ! empty( $method_data[ 'requires' ] ) ) { |
| 2248 |
|
| 2249 |
switch ( $method_data[ 'requires' ] ) { |
| 2250 |
|
| 2251 |
case 'both': // Requires a coupon and minimum quantity. |
| 2252 |
case 'coupon': // Requires a coupon. |
| 2253 |
case 'either': // Requires a coupon OR a minimum amount. |
| 2254 |
case 'min_amount': // Requires a minimum amount. |
| 2255 |
|
| 2256 |
/* |
| 2257 |
* https://schema.org/OfferShippingDetails does not provide a way to specify |
| 2258 |
* conditions for shipping rates, like coupon or minimum amount. |
| 2259 |
*/ |
| 2260 |
if ( $this->p->debug->enabled ) { |
| 2261 |
|
| 2262 |
$this->p->debug->log( 'no markup for shipping requires = ' . $method_data[ 'requires' ] ); |
| 2263 |
} |
| 2264 |
|
| 2265 |
return false; |
| 2266 |
|
| 2267 |
default: // Unknown requirement. |
| 2268 |
|
| 2269 |
if ( $this->p->debug->enabled ) { |
| 2270 |
|
| 2271 |
$this->p->debug->log( 'unknown shipping requires = ' . $method_data[ 'requires' ] ); |
| 2272 |
} |
| 2273 |
|
| 2274 |
return false; |
| 2275 |
} |
| 2276 |
} |
| 2277 |
|
| 2278 |
if ( is_numeric( $rate_cost ) ) { // Just in case. |
| 2279 |
|
| 2280 |
$shipping_rate = array( |
| 2281 |
'shipping_rate_name' => $rate_name, |
| 2282 |
'shipping_rate_cost' => $rate_cost, |
| 2283 |
'shipping_rate_currency' => $product_currency, |
| 2284 |
); |
| 2285 |
|
| 2286 |
/* |
| 2287 |
* Returns shipping department, handling, and transit options for $shipping_class_id and $method_inst_id. |
| 2288 |
* |
| 2289 |
* Array ( |
| 2290 |
* [shipdept_rel] => http://adm.surniaulula.com/produit/a-variable-product/ |
| 2291 |
* [shipdept_timezone] => America/Vancouver |
| 2292 |
* [shipdept_midday_close] => 12:00 |
| 2293 |
* [shipdept_midday_open] => 13:00 |
| 2294 |
* [shipdept_cutoff] => 16:00 |
| 2295 |
* [shipdept_day_sunday_open] => none |
| 2296 |
* [shipdept_day_sunday_close] => none |
| 2297 |
* [shipdept_day_monday_open] => 09:00 |
| 2298 |
* [shipdept_day_monday_close] => 17:00 |
| 2299 |
* [shipdept_day_tuesday_open] => 09:00 |
| 2300 |
* [shipdept_day_tuesday_close] => 17:00 |
| 2301 |
* [shipdept_day_wednesday_open] => 09:00 |
| 2302 |
* [shipdept_day_wednesday_close] => 17:00 |
| 2303 |
* [shipdept_day_thursday_open] => 09:00 |
| 2304 |
* [shipdept_day_thursday_close] => 17:00 |
| 2305 |
* [shipdept_day_friday_open] => 09:00 |
| 2306 |
* [shipdept_day_friday_close] => 17:00 |
| 2307 |
* [shipdept_day_saturday_open] => none |
| 2308 |
* [shipdept_day_saturday_close] => none |
| 2309 |
* [shipdept_day_publicholidays_open] => 09:00 |
| 2310 |
* [shipdept_day_publicholidays_close] => 12:00 |
| 2311 |
* [handling_rel] => http://adm.surniaulula.com/produit/a-variable-product/ |
| 2312 |
* [handling_maximum] => 1.5 |
| 2313 |
* [handling_unit_code] => DAY |
| 2314 |
* [handling_unit_text] => d |
| 2315 |
* [handling_name] => Days |
| 2316 |
* [transit_rel] => http://adm.surniaulula.com/produit/a-variable-product/ |
| 2317 |
* [transit_minimum] => 5 |
| 2318 |
* [transit_maximum] => 7 |
| 2319 |
* [transit_unit_code] => DAY |
| 2320 |
* [transit_unit_text] => d |
| 2321 |
* [transit_name] => Days |
| 2322 |
* ) |
| 2323 |
* |
| 2324 |
* See WpssoWcsdtFilters->filter_wc_shipping_delivery_time(). |
| 2325 |
*/ |
| 2326 |
$delivery_time = apply_filters( 'wpsso_wc_shipping_delivery_time', array(), |
| 2327 |
$zone_id, $method_inst_id, $shipping_class_id, $product_parent_url ); |
| 2328 |
|
| 2329 |
$shipping_offer = array( |
| 2330 |
'shipping_id' => 'shipping-z' . $zone_id . '-m' . $method_inst_id . '-c' . $shipping_class_id, |
| 2331 |
'shipping_rel' => $product_parent_url, |
| 2332 |
'shipping_name' => $zone_name, |
| 2333 |
'shipping_rate' => $shipping_rate, |
| 2334 |
'delivery_time' => $delivery_time, |
| 2335 |
); |
| 2336 |
|
| 2337 |
} elseif ( $this->p->debug->enabled ) { |
| 2338 |
|
| 2339 |
$this->p->debug->log( 'invalid rate cost = ' . $rate_cost ); |
| 2340 |
} |
| 2341 |
|
| 2342 |
$shipping_offer = apply_filters( 'wpsso_wc_shipping_zone_offer', $shipping_offer, |
| 2343 |
$zone_id, $zone_name, $method_inst_id, $method_obj, $shipping_class_id, $product, $product_parent ); |
| 2344 |
|
| 2345 |
if ( $this->p->debug->enabled ) { |
| 2346 |
|
| 2347 |
$this->p->debug->log_arr( 'shipping_offer', $shipping_offer ); |
| 2348 |
} |
| 2349 |
|
| 2350 |
return $shipping_offer; |
| 2351 |
} |
| 2352 |
|
| 2353 |
/* |
| 2354 |
* Example: |
| 2355 |
* |
| 2356 |
* list( |
| 2357 |
* $mt_ecom[ 'product:shipping_length:value' ], |
| 2358 |
* $mt_ecom[ 'product:shipping_length:units' ], |
| 2359 |
* $mt_ecom[ 'product:shipping_width:value' ], |
| 2360 |
* $mt_ecom[ 'product:shipping_width:units' ], |
| 2361 |
* $mt_ecom[ 'product:shipping_height:value' ], |
| 2362 |
* $mt_ecom[ 'product:shipping_height:units' ], |
| 2363 |
* $mt_ecom[ 'product:shipping_weight:value' ], |
| 2364 |
* $mt_ecom[ 'product:shipping_weight:units' ], |
| 2365 |
* ) = $this->get_shipping_length_width_height_weight( $product ); |
| 2366 |
* |
| 2367 |
* See WpssoIntegEcomWooCommerce->add_mt_product(). |
| 2368 |
* See WpssoIntegEcomWooCommerce->filter_get_md_defaults(). |
| 2369 |
*/ |
| 2370 |
private function get_shipping_length_width_height_weight( WC_Product $product ) { // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 2371 |
|
| 2372 |
if ( $this->p->debug->enabled ) { |
| 2373 |
|
| 2374 |
$this->p->debug->log( 'product = ' . get_class( $product ) ); // WC_Product, WC_Product_Variable, or WC_Product_Grouped. |
| 2375 |
} |
| 2376 |
|
| 2377 |
$has_dimensions = $product->has_dimensions(); |
| 2378 |
$has_weight = $product->has_weight(); |
| 2379 |
$dimension_unit_text = WpssoUtilUnits::get_dimension_text(); // Returns 'og_def_dimension_units' value. |
| 2380 |
$weight_unit_text = WpssoUtilUnits::get_weight_text(); // Returns 'og_def_weight_units' value. |
| 2381 |
|
| 2382 |
if ( $this->p->debug->enabled ) { |
| 2383 |
|
| 2384 |
$this->p->debug->log( 'has_dimensions = ' . ( $has_dimensions ? 'true' : 'false' ) ); |
| 2385 |
$this->p->debug->log( 'has_weight = ' . ( $has_weight ? 'true' : 'false' ) ); |
| 2386 |
$this->p->debug->log( 'dimension_unit_text = ' . $dimension_unit_text ); |
| 2387 |
$this->p->debug->log( 'weight_unit_text = ' . $weight_unit_text ); |
| 2388 |
} |
| 2389 |
|
| 2390 |
$ship_dims_weight = array( |
| 2391 |
0 => null, // Shipping length value. |
| 2392 |
1 => $dimension_unit_text, // Shipping lenth units. |
| 2393 |
2 => null, // Shipping width value. |
| 2394 |
3 => $dimension_unit_text, // Shipping width units. |
| 2395 |
4 => null, // Shipping height value. |
| 2396 |
5 => $dimension_unit_text, // Shipping height units. |
| 2397 |
6 => null, // Shipping weight value. |
| 2398 |
7 => $weight_unit_text, // Shipping weight units. |
| 2399 |
); |
| 2400 |
|
| 2401 |
if ( $this->p->debug->enabled ) { |
| 2402 |
|
| 2403 |
$this->p->debug->log( 'getting product shipping dimensions' ); |
| 2404 |
} |
| 2405 |
|
| 2406 |
if ( is_callable( array( $product, 'get_length' ) ) ) { |
| 2407 |
|
| 2408 |
$length = $product->get_length(); // Shipping length. |
| 2409 |
|
| 2410 |
if ( is_numeric( $length ) ) { // Required to ignore undefined values. |
| 2411 |
|
| 2412 |
$ship_dims_weight[ 0 ] = $length; |
| 2413 |
|
| 2414 |
} elseif ( $this->p->debug->enabled ) { |
| 2415 |
|
| 2416 |
$this->p->debug->log( 'product shipping length is not numeric' ); |
| 2417 |
} |
| 2418 |
} |
| 2419 |
|
| 2420 |
if ( is_callable( array( $product, 'get_width' ) ) ) { |
| 2421 |
|
| 2422 |
$width = $product->get_width(); // Shipping width. |
| 2423 |
|
| 2424 |
if ( is_numeric( $width ) ) { // Required to ignore undefined values. |
| 2425 |
|
| 2426 |
$ship_dims_weight[ 2 ] = $width; |
| 2427 |
|
| 2428 |
} elseif ( $this->p->debug->enabled ) { |
| 2429 |
|
| 2430 |
$this->p->debug->log( 'product shipping width is not numeric' ); |
| 2431 |
} |
| 2432 |
} |
| 2433 |
|
| 2434 |
if ( is_callable( array( $product, 'get_height' ) ) ) { |
| 2435 |
|
| 2436 |
$height = $product->get_height(); // Shipping height. |
| 2437 |
|
| 2438 |
if ( is_numeric( $height ) ) { // Required to ignore undefined values. |
| 2439 |
|
| 2440 |
$ship_dims_weight[ 4 ] = $height; |
| 2441 |
|
| 2442 |
} elseif ( $this->p->debug->enabled ) { |
| 2443 |
|
| 2444 |
$this->p->debug->log( 'product shipping height is not numeric' ); |
| 2445 |
} |
| 2446 |
} |
| 2447 |
|
| 2448 |
if ( $this->p->debug->enabled ) { |
| 2449 |
|
| 2450 |
$this->p->debug->log( 'getting product shipping weight' ); |
| 2451 |
} |
| 2452 |
|
| 2453 |
if ( is_callable( array( $product, 'get_weight' ) ) ) { // Just in case. |
| 2454 |
|
| 2455 |
$weight = $product->get_weight(); // Shipping weight. |
| 2456 |
|
| 2457 |
if ( is_numeric( $weight ) ) { // Required to ignore undefined values. |
| 2458 |
|
| 2459 |
$ship_dims_weight[ 6 ] = $weight; |
| 2460 |
|
| 2461 |
} elseif ( $this->p->debug->enabled ) { |
| 2462 |
|
| 2463 |
$this->p->debug->log( 'product shipping weight is not numeric' ); |
| 2464 |
} |
| 2465 |
} |
| 2466 |
|
| 2467 |
if ( $this->p->debug->enabled ) { |
| 2468 |
|
| 2469 |
$this->p->debug->log_arr( 'ship_dims_weight', $ship_dims_weight ); |
| 2470 |
} |
| 2471 |
|
| 2472 |
return $ship_dims_weight; |
| 2473 |
} |
| 2474 |
|
| 2475 |
/* |
| 2476 |
* See WpssoIntegEcomWooCommerce->filter_head_cache_index(). |
| 2477 |
*/ |
| 2478 |
private function get_product_currency() { |
| 2479 |
|
| 2480 |
if ( $this->p->debug->enabled ) { |
| 2481 |
|
| 2482 |
$this->p->debug->mark(); |
| 2483 |
} |
| 2484 |
|
| 2485 |
static $local_cache = null; |
| 2486 |
|
| 2487 |
if ( null === $local_cache ) { // Get value only once. |
| 2488 |
|
| 2489 |
$local_cache = get_woocommerce_currency(); |
| 2490 |
|
| 2491 |
if ( $this->p->debug->enabled ) { |
| 2492 |
|
| 2493 |
$this->p->debug->log( 'get_woocommerce_currency() returned ' . $local_cache ); |
| 2494 |
} |
| 2495 |
|
| 2496 |
$local_cache = apply_filters( 'wpsso_product_currency', $local_cache ); |
| 2497 |
} |
| 2498 |
|
| 2499 |
return $local_cache; |
| 2500 |
} |
| 2501 |
|
| 2502 |
/* |
| 2503 |
* Get product availability. |
| 2504 |
* |
| 2505 |
* See https://woocommerce.github.io/code-reference/classes/WC-Product.html#method_is_in_stock |
| 2506 |
* |
| 2507 |
* Hook 'woocommerce_product_is_in_stock' (returns true or false) to customize the "in stock" status. |
| 2508 |
*/ |
| 2509 |
private function get_product_avail( $product ) { |
| 2510 |
|
| 2511 |
if ( $this->p->debug->enabled ) { |
| 2512 |
|
| 2513 |
$this->p->debug->mark(); |
| 2514 |
} |
| 2515 |
|
| 2516 |
$product_avail = null; |
| 2517 |
|
| 2518 |
if ( $product->is_in_stock() ) { |
| 2519 |
|
| 2520 |
if ( $this->p->debug->enabled ) { |
| 2521 |
|
| 2522 |
$this->p->debug->log( 'product is in stock' ); |
| 2523 |
} |
| 2524 |
|
| 2525 |
$product_avail = 'https://schema.org/InStock'; |
| 2526 |
|
| 2527 |
} elseif ( $product->is_on_backorder() ) { |
| 2528 |
|
| 2529 |
if ( $this->p->debug->enabled ) { |
| 2530 |
|
| 2531 |
$this->p->debug->log( 'product is on backorder' ); |
| 2532 |
} |
| 2533 |
|
| 2534 |
$product_avail = 'https://schema.org/BackOrder'; |
| 2535 |
|
| 2536 |
} else { |
| 2537 |
|
| 2538 |
if ( $this->p->debug->enabled ) { |
| 2539 |
|
| 2540 |
$this->p->debug->log( 'product is out of stock' ); |
| 2541 |
} |
| 2542 |
|
| 2543 |
$product_avail = 'https://schema.org/OutOfStock'; |
| 2544 |
} |
| 2545 |
|
| 2546 |
return $product_avail; |
| 2547 |
} |
| 2548 |
|
| 2549 |
private function get_product_price( $product ) { |
| 2550 |
|
| 2551 |
$product_price = $product->get_price(); |
| 2552 |
|
| 2553 |
if ( $this->p->debug->enabled ) { |
| 2554 |
|
| 2555 |
$this->p->debug->log( 'get_price() returned "' . $product_price . '" (type is ' . gettype( $product_price ) . ')' ); |
| 2556 |
} |
| 2557 |
|
| 2558 |
$product_price = apply_filters( 'wpsso_product_price', $product_price, $product ); |
| 2559 |
|
| 2560 |
/* |
| 2561 |
* Check that the product price returned by WooCommerce is not an empty string, to avoid an error in: |
| 2562 |
* |
| 2563 |
* PHP Fatal error: Uncaught TypeError: Unsupported operand types: string * float in |
| 2564 |
* woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php:141 |
| 2565 |
*/ |
| 2566 |
if ( empty( $product_price ) ) { |
| 2567 |
|
| 2568 |
if ( $this->p->debug->enabled ) { |
| 2569 |
|
| 2570 |
$this->p->debug->log( 'returning product price 0' ); |
| 2571 |
} |
| 2572 |
|
| 2573 |
$product_price = 0; |
| 2574 |
} |
| 2575 |
|
| 2576 |
return $product_price; |
| 2577 |
} |
| 2578 |
|
| 2579 |
private function get_product_price_formatted( $product, $product_price, $product_incl_vat = false ) { |
| 2580 |
|
| 2581 |
if ( is_numeric( $product_price ) ) { // Just in case. |
| 2582 |
|
| 2583 |
if ( $product_incl_vat ) { |
| 2584 |
|
| 2585 |
if ( $this->p->debug->enabled ) { |
| 2586 |
|
| 2587 |
$this->p->debug->log( 'calling wc_get_price_including_tax() for ' . $product_price ); |
| 2588 |
} |
| 2589 |
|
| 2590 |
$product_price = wc_get_price_including_tax( $product, array( 'price' => $product_price ) ); // Since WC v3.0. |
| 2591 |
|
| 2592 |
if ( $this->p->debug->enabled ) { |
| 2593 |
|
| 2594 |
$this->p->debug->log( 'wc_get_price_including_tax() returned ' . $product_price ); |
| 2595 |
} |
| 2596 |
} |
| 2597 |
|
| 2598 |
/* |
| 2599 |
* $decimals = Number of decimal points, blank to use woocommerce_price_num_decimals, or false to |
| 2600 |
* avoid all rounding. |
| 2601 |
*/ |
| 2602 |
$product_price = wc_format_decimal( $product_price, $decimals = '', $trim_zeros = false ); |
| 2603 |
|
| 2604 |
} else $product_price = ''; |
| 2605 |
|
| 2606 |
if ( $this->p->debug->enabled ) { |
| 2607 |
|
| 2608 |
$this->p->debug->log( 'product price formatted = ' . $product_price ); |
| 2609 |
} |
| 2610 |
|
| 2611 |
return $product_price; |
| 2612 |
} |
| 2613 |
|
| 2614 |
private function get_product_title( WC_Product $product ) { |
| 2615 |
|
| 2616 |
if ( $this->p->debug->enabled ) { |
| 2617 |
|
| 2618 |
$this->p->debug->mark(); |
| 2619 |
} |
| 2620 |
|
| 2621 |
$title_text = $product->get_title(); |
| 2622 |
|
| 2623 |
return apply_filters( 'wpsso_product_title', $title_text, $product ); |
| 2624 |
} |
| 2625 |
|
| 2626 |
private function get_product_url( WC_Product $product ) { |
| 2627 |
|
| 2628 |
if ( $this->p->debug->enabled ) { |
| 2629 |
|
| 2630 |
$this->p->debug->mark(); |
| 2631 |
} |
| 2632 |
|
| 2633 |
$url = $product->get_permalink(); |
| 2634 |
|
| 2635 |
return apply_filters( 'wpsso_product_url', $url, $product ); |
| 2636 |
} |
| 2637 |
|
| 2638 |
private function add_product_variation_title( &$mt_ecom, $mod, WC_Product $product, $variation ) { // Pass by reference is OK. |
| 2639 |
|
| 2640 |
if ( $this->p->debug->enabled ) { |
| 2641 |
|
| 2642 |
$this->p->debug->mark(); |
| 2643 |
} |
| 2644 |
|
| 2645 |
$mt_ecom[ 'product:title' ] = $this->get_product_variation_title( $mod, $product, $variation ); |
| 2646 |
} |
| 2647 |
|
| 2648 |
private function get_product_variation_title( $mod, WC_Product $product, $variation ) { |
| 2649 |
|
| 2650 |
if ( $this->p->debug->enabled ) { |
| 2651 |
|
| 2652 |
$this->p->debug->mark(); |
| 2653 |
} |
| 2654 |
|
| 2655 |
$title_text = $this->p->opt->get_text( 'plugin_product_var_title' ); |
| 2656 |
|
| 2657 |
$var_attrs = array_filter( array_values( $product->get_variation_attributes() ) ); |
| 2658 |
|
| 2659 |
$title_atts = array( |
| 2660 |
'var_title' => $this->get_product_title( $product ), |
| 2661 |
'var_sku' => $product->get_sku(), |
| 2662 |
'var_attrs' => implode( ' %%sep%% ', $var_attrs ), |
| 2663 |
); |
| 2664 |
|
| 2665 |
$title_text = $this->p->util->inline->replace_variables( $title_text, $mod, $title_atts ); |
| 2666 |
|
| 2667 |
return apply_filters( 'wpsso_product_variation_title', $title_text, $product, $variation ); |
| 2668 |
} |
| 2669 |
|
| 2670 |
/* |
| 2671 |
* Empty variation descriptions are fixed in WpssoOpenGraphNS->filter_og_data_https_ogp_me_ns_product(). |
| 2672 |
*/ |
| 2673 |
private function add_product_variation_description( &$mt_ecom, $mod, WC_Product $product, $variation ) { // Pass by reference is OK. |
| 2674 |
|
| 2675 |
if ( $this->p->debug->enabled ) { |
| 2676 |
|
| 2677 |
$this->p->debug->mark(); |
| 2678 |
} |
| 2679 |
|
| 2680 |
$mt_ecom[ 'product:description' ] = $this->get_product_variation_description( $mod, $product, $variation ); |
| 2681 |
} |
| 2682 |
|
| 2683 |
private function get_product_variation_description( $mod, WC_Product $product, $variation ) { |
| 2684 |
|
| 2685 |
if ( $this->p->debug->enabled ) { |
| 2686 |
|
| 2687 |
$this->p->debug->mark(); |
| 2688 |
} |
| 2689 |
|
| 2690 |
$desc_text = empty( $variation[ 'variation_description' ] ) ? |
| 2691 |
null : $this->p->util->cleanup_html_tags( $variation[ 'variation_description' ] ); |
| 2692 |
|
| 2693 |
$desc_text = apply_filters( 'wpsso_the_description', $desc_text, $mod ); |
| 2694 |
|
| 2695 |
return apply_filters( 'wpsso_product_variation_description', $desc_text, $product, $variation ); |
| 2696 |
} |
| 2697 |
|
| 2698 |
private function is_variation_selectable_attribute( $product_id, $product, $attr_name ) { |
| 2699 |
|
| 2700 |
if ( $this->p->debug->enabled ) { |
| 2701 |
|
| 2702 |
$this->p->debug->log_args( array( |
| 2703 |
'product_id' => $product_id, |
| 2704 |
'product' => $product, |
| 2705 |
'attr_name' => $attr_name, |
| 2706 |
) ); |
| 2707 |
} |
| 2708 |
|
| 2709 |
if ( method_exists( $product, 'get_variation_attributes' ) ) { // Just in case. |
| 2710 |
|
| 2711 |
static $local_fifo = array(); |
| 2712 |
|
| 2713 |
if ( ! isset( $local_fifo[ $product_id ] ) ) { |
| 2714 |
|
| 2715 |
/* |
| 2716 |
* Maybe limit the number of array elements. |
| 2717 |
*/ |
| 2718 |
$local_fifo = SucomUtil::array_slice_fifo( $local_fifo, WPSSO_CACHE_ARRAY_FIFO_MAX ); |
| 2719 |
|
| 2720 |
$local_fifo[ $product_id ] = $product->get_variation_attributes(); |
| 2721 |
} |
| 2722 |
|
| 2723 |
if ( isset( $local_fifo[ $product_id ][ $attr_name ] ) && is_array( $local_fifo[ $product_id ][ $attr_name ] ) ) { |
| 2724 |
|
| 2725 |
return true; |
| 2726 |
} |
| 2727 |
} |
| 2728 |
|
| 2729 |
return false; |
| 2730 |
} |
| 2731 |
} |
| 2732 |
} |
| 2733 |
|