Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 543
MrFeedback Avatar asked Sep 06 '25 20:09

MrFeedback


1 Answers

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';
    }
}
like image 159
Manuel Eduardo Romero Avatar answered Sep 10 '25 01:09

Manuel Eduardo Romero