Skip to content

Instantly share code, notes, and snippets.

@al5dy
Last active April 5, 2016 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save al5dy/30ee40e0c5823acfdddf7a1f0e81383b to your computer and use it in GitHub Desktop.
Save al5dy/30ee40e0c5823acfdddf7a1f0e81383b to your computer and use it in GitHub Desktop.
Проверка и преобразование/создание вариативного товара в WooCommerce.
if ( ! function_exists( 'create_gift_variation' ) ) {
function create_gift_variation( $product_id, $price ) {
$product_variation = get_posts(
array(
'post_parent' => $product_id,
'post_title' => 'wfg_gift_product',
'post_type' => 'product_variation',
'posts_per_page' => 1
)
);
if( !empty($product_variation) ) {
update_post_meta( $product_variation[0]->ID, '_price', $price);
update_post_meta( $product_variation[0]->ID, '_regular_price', $price);
update_post_meta( $product_variation[0]->ID, '_wfg_gift_product', 1);
return $product_variation[0]->ID;
}
$admin = get_users( 'orderby=nicename&role=administrator&number=1' );
$variation = array(
'post_author' => $admin[0]->ID,
'post_status' => 'publish',
'post_name' => 'product-' . $product_id . '-variation',
'post_parent' => $product_id,
'post_title' => 'wfg_gift_product',
'post_type' => 'product_variation',
'comment_status' => 'closed',
'ping_status' => 'closed'
);
$post_id = wp_insert_post( $variation );
update_post_meta( $post_id, '_price', $price);
update_post_meta( $post_id, '_regular_price', $price);
update_post_meta( $post_id, '_wfg_gift_product', 1);
return $post_id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment