简体   繁体   English

如何使用php代码将woocommerce产品添加到自定义Product_type?

[英]How to add a woocommerce product to the custom Product_type using php code?

A WooCommerce product is getting created easily but the product is getting added to the product_type:simple . 可以轻松创建WooCommerce产品,但是将产品添加到product_type:simple I want to add the product to the "custom product_type:test" 我要将产品添加到“ custom product_type:test”

static function createTicket($postId, $productDetails) {
$id = wp_insert_post(array(
    'post_type' => 'product',
    'post_title' => $productDetails['title'],
    'post_content' => $productDetails['description'],
    'post_status' => get_post_status($postId),
        'max_value' => 1,
));
update_post_meta($id, '_price', $productDetails['price']);
update_post_meta($id, '_regular_price', $productDetails['price']);
update_post_meta($id, '_wpws_testID', $postId);
update_post_meta($id, '_sold_individually', "yes");
wp_set_object_terms( $id, 'test', 'product_type' );
return $id;
}

I have added 我已经添加了

wp_set_object_terms( $id, 'test', 'product_type' ); wp_set_object_terms($ id,'test','product_type');

But nothing works 但是什么都没有

在此处输入图片说明

Used product_type_selector hook 二手的product_type_selector挂钩

// add a product type
add_filter( 'product_type_selector', 'add_custom_product_type' );
function add_custom_product_type( $types ){
    $types[ 'test_custom_product_type' ] = __( 'test Product' );
    return $types;
}

Edit 编辑

Please check below links it help you more on this 请检查下面的链接,它可以帮助您更多

Edit 编辑

Above code for older version of WooCommerce 上面的WooCommerce旧版本代码

Used this code for save product with custom product type. 此代码用于保存具有自定义产品类型的产品。 it working with WooCommerce 3.* Check this for more information https://gist.github.com/stirtingale/753ac6ddb076bd551c3261007c9c63a5 它可以与WooCommerce 3. *一起使用。有关更多信息, 检查此https://gist.github.com/stirtingale/753ac6ddb076bd551c3261007c9c63a5

function register_simple_rental_product_type() {
    class WC_Product_Simple_Rental extends WC_Product {

        public function __construct( $product ) {
            $this->product_type = 'simple_rental';
            parent::__construct( $product );
        }
    }
}
add_action( 'init', 'register_simple_rental_product_type' );
function add_simple_rental_product( $types ){
    // Key should be exactly the same as in the class
    $types[ 'simple_rental' ] = __( 'Simple Rental' );
    return $types;
}
add_filter( 'product_type_selector', 'add_simple_rental_product' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM