Add compatibility for WooCredits

Issue #820 resolved
Valentina R created an issue

Requested here.

FYI, the old code snippet does not work.

Comments (2)

  1. Željan Topić

    Code snippet your shared should work. Otherwise, here's a simple snippet others can use if needed:

    /**
     * Woo Credits integration class.
     * http://www.woocredits.com/
     */
    class WCA_Woo_Download_Credits {
        function __construct() {
            add_action( 'woocommerce_product_options_general_product_data', array( $this, 'add_custom_general_fields' ) );
            add_action( 'woocommerce_process_product_meta', array($this, 'add_custom_general_fields_save' ) );
            add_action( 'wp_insert_post', array($this, 'product_custom_meta_data_save' ) );
            add_action( 'save_post', array($this, 'product_custom_meta_data_save' ) );
            add_action( 'admin_head', array( $this, 'show_options_at_all_times' ) );
        }
    
        /**
         * Add Woo Credits input to edit product screen.
         *
         * @return void
         */
        public function add_custom_general_fields() {
            global $woocommerce, $post;
            $product_id = $post->ID;
            $_product = wc_get_product( $product_id );
            if ( 'appointment' == $_product->product_type ) {
                echo '<div class="options_group woo-credit-price">';
                woocommerce_wp_text_input(
                    array(
                        'id' => '_credits_amount',
                        'label' => __('Woo Credits Price ', 'your_gettext'),
                        'placeholder' => '',
                        'desc_tip' => 'true',
                        'description' => __('The credits price for this product.', 'your_gettext'),
                        'type' => 'text',
                    )
                );
                echo '</div>';
            }
        }
    
        /**
         * Save option.
         *
         * @return void
         */
        public function add_custom_general_fields_save( $post_id ) {
            $woocommerce_credits_amount = $_POST['_credits_amount'];
            $_product = wc_get_product( $post_id );
            if ( !empty( $woocommerce_credits_amount ) && 'appointment' == $_product->get_type() ) {
                update_post_meta( $post_id, '_credits_amount', esc_attr( $woocommerce_credits_amount ) );
            }
        }
    
        /**
         * Save option.
         *
         * @return void
         */
        public function product_custom_meta_data_save( $post_id ) {
            if ( !isset($_POST['post_type']) || 'product' != $_POST['post_type'] ) {
                return;
            }
            $woocommerce_credits_amount = $_POST['_credits_amount'];
            $_product = wc_get_product( $post_id );
            if ( !empty( $woocommerce_credits_amount ) && 'appointment' == $_product->get_type() ) {
                update_post_meta( $post_id, '_credits_amount', esc_attr( $woocommerce_credits_amount ) );
            }
        }
    
        /**
         * Show Woo Credits input that is hidden by default.
         *
         * @return void
         */
        public function show_options_at_all_times() {
            echo '<style>
                .woocommerce_options_panel .wdc-hide.options_group.woo-credit-price {
                    display: block!important;
                }
            </style>';
        }
    }
    
    new WCA_Woo_Download_Credits();
    
  2. Log in to comment