Video product implemented

parent 06cda840
......@@ -40,5 +40,9 @@ function settings_wcpms_section_email()
echo "<h2>WooCommerce Checkout Email endpoint</h2>";
}
function settings_wcpms_section_prodcat()
{
echo "<h2>WooCommerce auto-roducts for Videos</h2>";
}
?>
......@@ -50,6 +50,12 @@ if(!class_exists('SH_Admin')) {
}
add_settings_section('sexhackme-wcpms-settings', ' ', 'wp_SexHackMe\settings_wcpms_section_email', 'sexhackme-wcpms-settings-email');
register_setting('sexhackme-wcpms-settings', 'sexhack_registration_mail_endpoint');
add_settings_section('sexhackme-wcpms-settings', ' ', 'wp_SexHackMe\settings_wcpms_section_prodcat', 'sexhackme-wcpms-settings-prodcat');
register_setting('sexhackme-wcpms-settings', 'sexhack_wcpms-prodcat');
register_setting('sexhackme-wcpms-settings', 'sexhack_wcpms-prodvisible');
}
// Add Advertising settings
......
......@@ -160,10 +160,14 @@ if(!class_exists('SH_Query')) {
if(!is_integer($id))
return;
$video = SH_Query::get_Video($id, $idtype);
$idtype=sanitize_idtype($idtype);
if(!$idtype) return false;
do_action('sh_delete_video', $video);
$sqlarr = array();
$sqlarr[] = "DELETE FROM {$wpdb->prefix}".SH_PREFIX."videotags_assoc WHERE video_id IN (
SELECT id FROM {$wpdb->prefix}".SH_PREFIX."videos
......
......@@ -33,12 +33,18 @@ if(!class_exists("SH_VideoProducts")) {
{
//add_action('sh_save_video_after_query', array($this, 'sync_product_from_video'), 1, 10);
add_filter('video_before_save', array($this, 'sync_product_from_video'));
add_action('sh_delete_video', array($this, 'delete_video_product'), 1, 10);
}
public function delete_video_product($video)
{
if($video->product_id > 0) return sh_wc_deleteProduct($video->product_id, true);
return false;
}
public function sync_product_from_video($video)
{
sexhack_log("PRODUUUUUUCT");
sexhack_log($video);
$prod = false;
......@@ -66,12 +72,13 @@ if(!class_exists("SH_VideoProducts")) {
// Product status.
if($video->status == 'published')
$prod->set_status('published');
$prod->set_status('publish');
else
$prod->set_status('draft');
// Not visible in catalog
$prod->set_catalog_visibility('hidden');
// Catalog visibility
if(get_option('sexhack_wcpms-prodvisible', false)) $prod->set_catalog_visibility('visible');
else $prod->set_catalog_visibility('hidden');
// Set the product as virtual and downloadable
$prod->set_virtual(true);
......@@ -131,6 +138,23 @@ if(!class_exists("SH_VideoProducts")) {
$prod->set_downloads( $wcdowns );
// Categories. If not configured try to search
// for a category named "Video", if not present
// just take the last one in list, if no categories
// exists just don't set anything and will remain in
// Uncategorized.
$cat_id = get_option('sexhack_wcpms-prodcat', false);
if($cat_id) $prod->set_category_ids(array($cat_id));
else {
$cat = false;
foreach( get_categories(array('taxonomy' => 'product_cat')) as $cat)
{
if($cat->name == 'Video') break;
}
if($cat) $prod->set_category_ids(array($cat->term_id));
}
$prod->save();
$video->product_id = $prod->get_id();
......
......@@ -71,4 +71,61 @@ function get_wc_subscription_products_priced($price, $pid=false)
return $res;
}
// Delete a product by id
/*
* Method to delete Woo Product
*
* @param int $id the product ID.
* @param bool $force true to permanently delete product, false to move to trash.
* @return \WP_Error|boolean
*/
function sh_wc_deleteProduct($id, $force = FALSE)
{
$product = wc_get_product($id);
if(empty($product))
return new WP_Error(999, sprintf(__('No %s is associated with #%d', 'woocommerce'), 'product', $id));
// If we're forcing, then delete permanently.
if ($force)
{
if ($product->is_type('variable'))
{
foreach ($product->get_children() as $child_id)
{
$child = wc_get_product($child_id);
$child->delete(true);
}
}
elseif ($product->is_type('grouped'))
{
foreach ($product->get_children() as $child_id)
{
$child = wc_get_product($child_id);
$child->set_parent_id(0);
$child->save();
}
}
$product->delete(true);
$result = $product->get_id() > 0 ? false : true;
}
else
{
$product->delete();
$result = 'trash' === $product->get_status();
}
if (!$result)
{
return new WP_Error(999, sprintf(__('This %s cannot be deleted', 'woocommerce'), 'product'));
}
// Delete parent product transients.
if ($parent_id = wp_get_post_parent_id($id))
{
wc_delete_product_transients($parent_id);
}
return true;
}
?>
......@@ -78,6 +78,32 @@ $plans = wp_SexHackMe\sh_get_subscription_plans();
</td>
</tr>
</table>
<?php do_settings_sections( 'sexhackme-wcpms-settings-prodcat' ); ?>
<table class="form-table">
<tr align="top">
<td>
<select id="sexhackme_wcpms-prodcat" name="sexhack_wcpms-prodcat" class="widefat">
<option value="-1">Choose...</option>
<?php
$opt=get_option("sexhack_wcpms-prodcat");
foreach( get_categories(array('taxonomy' => 'product_cat')) as $cat)
{
echo '<option value="' . esc_attr( $cat->term_id ) . '"';
if ($opt == $cat->term_id) { echo "selected";}
echo '>' . esc_html( $cat->name ) . ' ( ID: ' . esc_attr( $cat->term_id) . ')' . '</option>';
} ?>
</select>
<p class="description">Select default woocommerce product category for auto-product creation</p>
</td>
</tr>
<tr align="top">
<td>
<input type='checkbox' name='sexhack_wcpms-prodvisible' <?php if(get_option('sexhack_wcpms-prodvisible', false)) echo "checked"?> />
<p class="description">Show products auto-created in shop?</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment