Created
December 30, 2024 11:38
-
-
Save webtoffee-git/4b5616dc885e5443b498d6a3f6d372fc to your computer and use it in GitHub Desktop.
Create a new gift card product if there are no existing gift card product in the store - By WebTOffee(WebToffee Gift Cards (Basic))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //Do not copy this line of code | |
add_action('admin_init', 'wt_gc_add_demo_giftcard_product'); | |
function wt_gc_add_demo_giftcard_product() { | |
$existing_product = get_posts(array( | |
'post_type' => 'product', | |
'post_status' => array('publish', 'draft', 'pending', 'private', 'future', 'trash'), | |
'meta_query' => array( | |
'relation' => 'AND', | |
array( | |
'key' => '_wt_gc_gift_card_product', | |
'compare' => 'EXISTS', | |
), | |
array( | |
'key' => '_wt_gc_gift_card_product', | |
'value' => '', | |
'compare' => '!=', | |
), | |
), | |
)); | |
if (empty($existing_product)) { | |
if (class_exists('Wbte_Woocommerce_Gift_Cards_Free_Common') | |
&& method_exists('Wbte_Woocommerce_Gift_Cards_Free_Common', 'update_option') | |
&& method_exists('Wbte_Woocommerce_Gift_Cards_Free_Common', 'get_module_id')) { | |
$product = new WC_Product(); | |
$product->set_name(__('Gift Card', 'wt-gift-cards-woocommerce')); | |
$product->set_regular_price('0'); | |
$product->set_description(__('This is an automatically created product for Gift card.', 'wt-gift-cards-woocommerce')); | |
$product_id = $product->save(); | |
update_post_meta($product_id, '_wt_gc_purchase_options', array('predefined')); | |
update_post_meta($product_id, '_wt_gc_amounts', '100,200,300'); | |
update_post_meta($product_id, '_wt_gc_enable_template', 'yes'); | |
update_post_meta($product_id, '_wt_gc_gift_card_product', $product_id); // set product ID as meta value | |
if (!class_exists('Wbte_Gc_Gift_Card_Free_Admin')) { | |
include_once plugin_dir_path(__FILE__) . 'path/to/class-wbte-gc-gift-card-free-admin.php'; | |
} | |
Wbte_Woocommerce_Gift_Cards_Free_Common::update_option( | |
'gift_card_products', | |
array($product_id), | |
Wbte_Woocommerce_Gift_Cards_Free_Common::get_module_id('gift_card') | |
); | |
if (!has_post_thumbnail($product_id)) { | |
add_product_default_image($product_id); | |
} | |
wp_update_post( | |
array( | |
'ID' => $product_id, | |
'post_status' => 'draft', | |
) | |
); | |
update_option('wbte_gift_card_product_created', true); | |
} | |
} | |
} | |
function add_product_default_image($product_id) { | |
$option_name = 'wt_gc_default_featured_image'; // option name to save $attachment_id | |
$attachment_id = absint(get_option($option_name, 0)); | |
if (0 === $attachment_id) { | |
// upload | |
$attachment_id = do_image_upload($option_name); | |
} elseif (!is_attachment_exists($attachment_id)) { | |
// re-upload | |
$attachment_id = do_image_upload($option_name); | |
} | |
if (false !== $attachment_id) { | |
set_post_thumbnail($product_id, $attachment_id); // set as featured image | |
} | |
} | |
function do_image_upload($option_name) { | |
$file_name = 'default_featured.png'; | |
$file_path = plugin_dir_path(__FILE__) . 'assets/images/' . $file_name; | |
$file_array = array('name' => $file_name); | |
$temp_file_name = wp_tempnam(wp_basename($file_path)); | |
if ($temp_file_name && copy($file_path, $temp_file_name)) { | |
$file_array['tmp_name'] = $temp_file_name; | |
} | |
if (empty($file_array['tmp_name'])) { | |
return false; | |
} | |
$attachment_id = media_handle_sideload($file_array, 0, __('Gift card', 'wt-gift-cards-woocommerce')); | |
if (is_wp_error($attachment_id)) { | |
return false; | |
} | |
update_option($option_name, $attachment_id); // save the attachment id to option | |
return $attachment_id; | |
} | |
function is_attachment_exists($attachment_id) { | |
$attachment = get_post($attachment_id); | |
return ($attachment && 'attachment' === $attachment->post_type); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment