MrFeedback
MrFeedback

Reputation: 111

Woocommerce custom product_type seems not to be saved correctly

I am creating my own plugin for woocommerce and realized, that my custom product_type is not saved when the product has been created or updated. I am sure that the problem is coming from my plugin, but I am not sure where to look at. Function to add the product_type (according to a post)

function extend_woocommerce()
{
    Class WC_Product_Connected_To_General_Product extends WC_Product_Simple
    {
        public function __construct($product)
        {
            $this->product_type = 'connected_to_general_product';
            $this->manage_stock = 'yes';
            parent::__construct($product);
        }
    }
}

Class which is called in the plugin

Class general_stock {

/**
 * general_stock constructor.
 */
function __construct()
{
    if($this->check_if_woocommerce_is_active()){
     add_action('init',[$this, 'add_woocommerce_product_type'])
     add_filter('product_type_selector', [$this,'add_woocommerce_product_type_general_connected_product']);

...
}
/**
 * add_woocommerce_product_type
 */
function add_woocommerce_product_type(){
    extend_woocommerce();
}
/**
 * add_woocommerce_product_type_general_connected_product
 * @param $types
 * @return mixed
 */
function add_woocommerce_product_type_general_connected_product($types){
    $types[$this->product_type_name] = __('Connected to general Product','ln-general-stock');
    return $types;
}

However "everything" works so far: I am able to select the new product type in backend and saving it aswell. (It is selected when I edit the product).

But when I query the Product in frontend and dump it, the value of product_type equals simple which I think should be connected_to_general_product or is this information stored in another value?

Thank you in advance for your help!

Upvotes: 4

Views: 2706

Answers (6)

Robiiiiiiiiiiiin
Robiiiiiiiiiiiin

Reputation: 84

As said on this issue, also make sure that your class that extends to WC_Product is hooked on init.

Upvotes: 0

GCarop
GCarop

Reputation: 11

add_filter( 'woocommerce_product_class', 'custom_woocommerce_product_class', 10, 
2 );
function custom_woocommerce_product_class( $classname, $product_type ) {
    if ( $product_type == 'connected_to_general_product' ) {
        $classname = 'WC_Product_Connected_To_General_Product';
    }
    return $classname;
}

Upvotes: 0

Waleed Afzal
Waleed Afzal

Reputation: 31

Also, add this code if doesn't work.

add_filter( 'woocommerce_product_class', array($this,'load_custom_product_type_class'), 10, 2 );

    public function load_custom_product_type_class( $classname, $product_type ){
        if ( $product_type == 'your_custom_product_type_here' ) {
            $classname = 'YOUR_PRODUCT_TYPE_CLASS_NAME';
        }
        return $classname;
    }

Upvotes: 2

Manuel Eduardo Romero
Manuel Eduardo Romero

Reputation: 891

Remember the extends of product class name has to start with WP_Product_ followed with the producto type. Eg.:

class WC_Product_Moto extends WC_Product {
    protected $product_type = 'moto';

    public function get_type(){
        return 'moto';
    }
}

Upvotes: 9

Deian Motov
Deian Motov

Reputation: 31

I had the same problem and found that I had to define a missing function in my case

if (class_exists('WC_Product_Simple')) {
    class WC_Product_Ebook_Store extends WC_Product_Simple {

    public function __construct( $product ) {

        $this->product_type = 'ebook_store';

        parent::__construct( $product );

    }
    public function get_type() {
        return 'ebook_store';
    }

    }
}

Upvotes: 0

helgatheviking
helgatheviking

Reputation: 26319

I would probably re-arrange the structure of your plugin a bit. I like to load the plugin on the woocommerce_loaded plugin, then you don't even need a conditional check to see if WooCommerce is active. But I think the problem is the array key/value pair that you are adding to the product_type_selector filter.

Class general_stock {

    /**
     * pseudo constructor.
     */
    public static function init()
    {
        include_once( 'path-to/class-wc-product-connected-to-general-product.php' );
         add_filter('product_type_selector', array( __CLASS__, 'add_woocommerce_product_type_general_connected_product' ) );
    }

    ...


    /**
     * add_woocommerce_product_type_general_connected_product
     * @param $types
     * @return mixed
     */
    public static function add_woocommerce_product_type_general_connected_product($types){
        $types['general-product'] = __('Connected to general Product','ln-general-stock');
        return $types;
    }

}

add_action( 'woocommerce_loaded', 'general_stock::init');

Class WC_Product_Connected_To_General_Product extends WC_Product_Simple
{
    public function __construct($product)
    {
        $this->product_type = 'connected_to_general_product';
        $this->manage_stock = 'yes';
        parent::__construct($product);
    }
}

Upvotes: 1

Related Questions