首页 > 解决方案 > 未设置可下载的 Woocommerce 产品内容

问题描述

我正在尝试以编程方式创建 WooCommerce 产品。除了购买后可以下载的实际文件外,一切正常。图像位于正确的文件夹中,代码实际检索它。但是,在执行时,将不会设置可下载文件。我该如何解决这个问题?


public function add_product(){    
        $product_1 = array(
            'post_title' => "Test product",
            'post_content' => "This is a temporary description",
            'post_status' => 'draft',
            'post_type' => "product"
        );

        $downloadable_file = array(
            'id'   => '1',
            'name'   =>  "Actuall downloadable file",
            'file'   =>  "../wp-content/uploads/2021/08/1149.jpg",
        );
        $downloadPicture = serialize($downloadable_file);

        $post_id = wp_insert_post( $product_1 );
        update_post_meta( $post_id, '_visibility', 'visible' );
        update_post_meta( $post_id, '_stock_status', 'instock');
        update_post_meta( $post_id, 'total_sales', '0' );
        update_post_meta( $post_id, '_downloadable', 'yes' );
        update_post_meta( $post_id, '_virtual', 'yes' );
        update_post_meta( $post_id, '_sku', '54684845' );
        update_post_meta( $post_id, '_regular_price', '75' );
`       update_post_meta( $post_id, '_thumbnail_id', "../wp-content/uploads/2021/08/1149.jpg" );
        update_post_meta( $post_id, '_downloadable_files', $downloadPicture );

    }

标签: phpwordpresswoocommerce

解决方案


推荐阅读