Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saimonh3/5e5c5a38bbfe764ccbbe887c3ed03805 to your computer and use it in GitHub Desktop.
Save saimonh3/5e5c5a38bbfe764ccbbe887c3ed03805 to your computer and use it in GitHub Desktop.
class_dokan_update_2_9_21_variation_product_vendor.hp
<?php
defined( 'ABSPATH' ) || exit;
/**
* Dokan 2.9.4 updater class
*
* @since 2.9.4
*/
class Dokan_Update_2_9_21_Variation_Product_Vendor extends Abstract_Dokan_Background_Processes {
/**
* Action
*
* @since 2.9.4
*
* @var string
*/
protected $action = 'dokan_update_2_9_21_variation_product_vendor';
/**
* Perform updates
*
* @since 2.9.4
*
* @param mixed $item
*
* @return mixed
*/
public function task( $item ) {
if ( empty( $item ) ) {
return false;
}
if ( 'update_vendor' === $item['updating'] ) {
return $this->update_variation_product_vendor( $item['paged'] );
}
return false;
}
/**
* Update shop_order post author
*
* @since 2.9.21
*
* @return void
*/
private function update_variation_product_vendor( $paged ) {
$limit = 50;
$count = $limit * $paged;
$products = wc_get_products( [
'posts_per_page' => $limit,
'offset' => $count
] );
if ( ! $products ) {
return;
}
foreach ( $products as $product ) {
if ( ‘variable’ !== $product->get_type() ) {
continue;
}
$vendor = dokan_get_vendor_by_product( $product );
foreach ( $product->get_children() as $variation ) {
$variation_product_vendor_id = get_post_field( 'post_author', $variation );
if ( absint( $vendor->get_id() ) !== absint( $variation_product_vendor_id ) ) {
wp_update_post( array(
'ID' => $variation,
'post_author' => $vendor->get_id()
) );
}
}
}
return array(
'updating' => 'update_vendor',
'paged' => ++$paged
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment