2af3fa10 by Jeff Balicki

create_and_attach_woocommerce_product

Signed-off-by: Jeff <jeff@gotenzing.com>
1 parent ec91ad78
......@@ -143,3 +143,94 @@ add_filter( 'woocommerce_cart_item_thumbnail', 'my_remove_cart_product_image', 1
function my_remove_cart_product_image() {
return __return_false();
}
add_action('acf/save_post', 'create_and_attach_woocommerce_product', 1);
function create_and_attach_woocommerce_product( $course_id ) {
if ( get_post_type($course_id) == 'sfwd-courses') {
error_log('create_and_attach_woocommerce_product');
// Set the product data
$product_data = array(
'post_title' => get_the_title( $course_id ),
'post_type' => 'product',
'post_status' => 'publish'
);
// Insert the product
$product_id = wp_insert_post( $product_data );
update_post_meta( $course_id, '_woocommerce_product', $product_id );
// Set the product type
wp_set_object_terms( $product_id, 'course', 'product_type' );
wp_set_object_terms( $product_id, 'uncategorized', 'product_cat' );
// Set the related_course
$related_course = array($course_id);
update_post_meta( $product_id, '_related_course', $related_course);
// Set the prices
update_post_meta( $product_id, '_regular_price', $price );
update_post_meta( $product_id, '_price', $price );
update_post_meta( $product_id, '_role_based_price',$sp );
}
}
add_action( 'wp_after_insert_post', 'my_wp_after_insert_post', 10, 4 );
function my_wp_after_insert_post( $post_id, $post, $update, $post_before ) {
if ( 'publish' !== $post->post_status || ( $post_before && 'publish' === $post_before->post_status ) || wp_is_post_revision( $post_id )) {
if ( get_post_type($post_id) == 'sfwd-courses') {
error_log('woocommerce_learndash_product');
learndash_update_setting($post_id, 'sfwd-courses_course_price_type', 'closed' );
$product_id = get_post_meta($post_id, '_woocommerce_product',true);
learndash_update_setting($post_id, 'sfwd-courses_certificate', 426 );
if($product_id !=""){
$courses_custom_button_url = learndash_update_setting($post_id, 'sfwd-courses_custom_button_url', get_site_url().'/cart/?add-to-cart='.$product_id );
}
$price = get_post_meta($post_id, 'program_info_cost_&_dates_0_cost',true );
if($price !=""){
learndash_update_setting($post_id, 'sfwd-courses_course_price', $price );
}
$staff_price = get_post_meta($post_id, 'program_info_cost_&_dates_0_cost_staff',true );
if($staff_price !=""){
$sp = array('staff' =>array('regular_price' => $staff_price));
update_post_meta( $product_id, '_regular_price', $price );
update_post_meta( $product_id, '_price', $price );
update_post_meta( $product_id, '_role_based_price',$sp );
}
}
}
}
// Redirect to All posts dashboard after post update/publish
function webroomtech_redirect_to_post_list() {
global $pagenow;
if (( $pagenow == 'post-new.php' ) && (get_post_type() == 'sfwd-courses')) { ?>
<script>
// On "publish / update / submit changes" button click, redirect to cpt listing
jQuery(document).ready(function($) {
// Getting the post type to work for post, pages, custom post types etc.
let postType = document.querySelector('form.metabox-base-form input#post_type').value;
var url = '/wp-admin/edit.php?post_type=' + postType;
setTimeout(function() {
$('.editor-post-publish-button__button').on('click', function() {
setTimeout(function() {
$.ajax({
success: function() {
window.location.href = url;
}
});
}, 2000);
});
}, 4000);
});
</script>
<?php
}
}
add_action( 'admin_print_footer_scripts', 'webroomtech_redirect_to_post_list' );
\ No newline at end of file
......