2af3fa10 by Jeff Balicki

create_and_attach_woocommerce_product

Signed-off-by: Jeff <jeff@gotenzing.com>
1 parent ec91ad78
...@@ -142,4 +142,95 @@ function wc_remove_all_quantity_fields( $return, $product ) ...@@ -142,4 +142,95 @@ function wc_remove_all_quantity_fields( $return, $product )
142 add_filter( 'woocommerce_cart_item_thumbnail', 'my_remove_cart_product_image', 10 ); 142 add_filter( 'woocommerce_cart_item_thumbnail', 'my_remove_cart_product_image', 10 );
143 function my_remove_cart_product_image() { 143 function my_remove_cart_product_image() {
144 return __return_false(); 144 return __return_false();
145 }
...\ No newline at end of file ...\ No newline at end of file
145 }
146
147
148 add_action('acf/save_post', 'create_and_attach_woocommerce_product', 1);
149
150
151 function create_and_attach_woocommerce_product( $course_id ) {
152 if ( get_post_type($course_id) == 'sfwd-courses') {
153
154 error_log('create_and_attach_woocommerce_product');
155 // Set the product data
156 $product_data = array(
157 'post_title' => get_the_title( $course_id ),
158 'post_type' => 'product',
159 'post_status' => 'publish'
160 );
161
162 // Insert the product
163 $product_id = wp_insert_post( $product_data );
164 update_post_meta( $course_id, '_woocommerce_product', $product_id );
165 // Set the product type
166 wp_set_object_terms( $product_id, 'course', 'product_type' );
167 wp_set_object_terms( $product_id, 'uncategorized', 'product_cat' );
168 // Set the related_course
169 $related_course = array($course_id);
170 update_post_meta( $product_id, '_related_course', $related_course);
171 // Set the prices
172 update_post_meta( $product_id, '_regular_price', $price );
173 update_post_meta( $product_id, '_price', $price );
174 update_post_meta( $product_id, '_role_based_price',$sp );
175
176 }
177
178 }
179
180
181 add_action( 'wp_after_insert_post', 'my_wp_after_insert_post', 10, 4 );
182
183 function my_wp_after_insert_post( $post_id, $post, $update, $post_before ) {
184 if ( 'publish' !== $post->post_status || ( $post_before && 'publish' === $post_before->post_status ) || wp_is_post_revision( $post_id )) {
185 if ( get_post_type($post_id) == 'sfwd-courses') {
186 error_log('woocommerce_learndash_product');
187 learndash_update_setting($post_id, 'sfwd-courses_course_price_type', 'closed' );
188 $product_id = get_post_meta($post_id, '_woocommerce_product',true);
189 learndash_update_setting($post_id, 'sfwd-courses_certificate', 426 );
190 if($product_id !=""){
191 $courses_custom_button_url = learndash_update_setting($post_id, 'sfwd-courses_custom_button_url', get_site_url().'/cart/?add-to-cart='.$product_id );
192 }
193 $price = get_post_meta($post_id, 'program_info_cost_&_dates_0_cost',true );
194 if($price !=""){
195 learndash_update_setting($post_id, 'sfwd-courses_course_price', $price );
196 }
197 $staff_price = get_post_meta($post_id, 'program_info_cost_&_dates_0_cost_staff',true );
198 if($staff_price !=""){
199 $sp = array('staff' =>array('regular_price' => $staff_price));
200 update_post_meta( $product_id, '_regular_price', $price );
201 update_post_meta( $product_id, '_price', $price );
202 update_post_meta( $product_id, '_role_based_price',$sp );
203 }
204 }
205 }
206
207 }
208
209 // Redirect to All posts dashboard after post update/publish
210 function webroomtech_redirect_to_post_list() {
211 global $pagenow;
212 if (( $pagenow == 'post-new.php' ) && (get_post_type() == 'sfwd-courses')) { ?>
213 <script>
214 // On "publish / update / submit changes" button click, redirect to cpt listing
215 jQuery(document).ready(function($) {
216 // Getting the post type to work for post, pages, custom post types etc.
217 let postType = document.querySelector('form.metabox-base-form input#post_type').value;
218 var url = '/wp-admin/edit.php?post_type=' + postType;
219 setTimeout(function() {
220 $('.editor-post-publish-button__button').on('click', function() {
221 setTimeout(function() {
222 $.ajax({
223 success: function() {
224 window.location.href = url;
225 }
226 });
227 }, 2000);
228 });
229 }, 4000);
230 });
231 </script>
232 <?php
233 }
234 }
235
236 add_action( 'admin_print_footer_scripts', 'webroomtech_redirect_to_post_list' );
...\ No newline at end of file ...\ No newline at end of file
......