Reputation: 31
I'm trying to extend the WC_Product class to add "property1" and "property2" to the protected array $data but when I try to run my plugin it give me the next error:
WC_Product not found.
Here is my code:
class WC_MyClass extends WC_Product {
function __construct() {
parent::__construct();
array_push($data, "property1", "property2");
}
}
What I need is to extend WC_Product class in my plugin.
Plugin details: My plugin consist in calculate shipping costs according FedEx table rate for my country. To do this I'm using the Woocommerce Table Rate Shipping plugin. The problem is that FedEx sometimes takes into acount not the real weight but volumetric so I want to do a plugin to determinate which weight is greater and assign it to a variable. The idea is to set a shipping weight to use it to calculate shipping cost and not to modify the product weight specification. This is why I'm trying to extend the WC_Product class to add to it the shipping weight property. I have already done all calculus, now I just need to store the shipping weight to use it with the Woocommerce Table Rate Shipping plugin.
Documentation: WC_Product class - $data property
Upvotes: 3
Views: 5895
Reputation: 5439
If you have the extend function on its own, WooCommerce classes will not yet be created. You have to add the class extension to the init
hook or woocommerce_init
like this:
add_action( 'init', 'register_myclass' );
function register_myclass() {
class WC_MyClass extends WC_Product {
/*now you can override whatever you like*/
}
}
Upvotes: 6
Reputation: 1380
you have to tell php that you want to do that on a class variable
class WC_MyClass extends WC_Product {
function __construct() {
parent::__construct();
$this->data[] = "property1";
$this->data[] = "property2";
}
}
Upvotes: 1