Hide Downloads section under Toggle for product variations (#44152)

* Create DownloadableProductTrait for common downloadable blocks creation

* Use DownloadableProductTrait in SimpleProductTemplate

* Use DownloadableProductTrait in ProductVariationTemplate

* Remeber downloadable files in product variations

* Add changelog file

* Fix php linter error

* Unlink downloads and downloadable product props so they can be managed separately

* Remove invalid margin bottom from the uploader label

* Add changelog file
This commit is contained in:
Maikel Perez 2024-02-02 10:04:42 -03:00 committed by GitHub
parent f383284ba6
commit eaa791f7da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 124 additions and 96 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Unlink downloads and downloadable product props so they can be managed separately

View File

@ -52,11 +52,6 @@ export function DownloadBlockEdit( {
context: { postType },
}: ProductEditorBlockEditProps< UploadsBlockAttributes > ) {
const blockProps = useWooBlockProps( attributes );
const [ , setDownloadable ] = useEntityProp< Product[ 'downloadable' ] >(
'postType',
postType,
'downloadable'
);
const [ downloads, setDownloads ] = useEntityProp< Product[ 'downloads' ] >(
'postType',
postType,
@ -122,10 +117,6 @@ export function DownloadBlockEdit( {
}
if ( newFiles.length ) {
if ( ! downloads.length ) {
setDownloadable( true );
}
const uploadedFiles = newFiles.map( ( file ) => ( {
id: stringifyId( file.id ),
file: file.url,
@ -153,10 +144,6 @@ export function DownloadBlockEdit( {
return;
}
if ( ! downloads.length ) {
setDownloadable( true );
}
const uploadedFile = {
id: stringifyId( files[ 0 ].id ),
file: files[ 0 ].url,
@ -191,10 +178,6 @@ export function DownloadBlockEdit( {
[]
);
if ( ! otherDownloads.length ) {
setDownloadable( false );
}
setDownloads( otherDownloads );
}

View File

@ -8,6 +8,10 @@ $fixed-section-height: 224px;
.wp-block-woocommerce-product-downloads-field {
&__body {
position: relative;
.woocommerce-media-uploader .woocommerce-media-uploader__label {
margin-bottom: 0;
}
}
&__table {

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Hide Downloads section under Toggle for product variations

View File

@ -65,6 +65,30 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
);
}
/**
* Get the downloads for a product variation.
*
* @param WC_Product_Variation $product Product variation instance.
* @param string $context Context of the request: 'view' or 'edit'.
*
* @return array
*/
protected function get_downloads( $product, $context = 'view' ) {
$downloads = array();
if ( $product->is_downloadable() || 'edit' === $context ) {
foreach ( $product->get_downloads() as $file_id => $file ) {
$downloads[] = array(
'id' => $file_id, // MD5 hash.
'name' => $file['name'],
'file' => $file['file'],
);
}
}
return $downloads;
}
/**
* Prepare a single variation output for response.
*
@ -95,7 +119,7 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
'purchasable' => $object->is_purchasable(),
'virtual' => $object->is_virtual(),
'downloadable' => $object->is_downloadable(),
'downloads' => $this->get_downloads( $object ),
'downloads' => $this->get_downloads( $object, $context ),
'download_limit' => '' !== $object->get_download_limit() ? (int) $object->get_download_limit() : -1,
'download_expiry' => '' !== $object->get_download_expiry() ? (int) $object->get_download_expiry() : -1,
'tax_status' => $object->get_tax_status(),

View File

@ -0,0 +1,78 @@
<?php
/**
* DownloadableProductTrait
*/
namespace Automattic\WooCommerce\Internal\Admin\Features\ProductBlockEditor\ProductTemplates;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\GroupInterface;
/**
* Downloadable Product Trait.
*/
trait DownloadableProductTrait {
/**
* Adds downloadable blocks to the given parent block.
*
* @param GroupInterface $parent_block The parent block.
*/
private function add_downloadable_product_blocks( $parent_block ) {
// Downloads section.
if ( Features::is_enabled( 'product-virtual-downloadable' ) ) {
$product_downloads_section_group = $parent_block->add_section(
array(
'id' => 'product-downloads-section-group',
'order' => 50,
'attributes' => array(
'blockGap' => 'unit-40',
),
'hideConditions' => array(
array(
'expression' => 'postType === "product" && editedProduct.type !== "simple"',
),
),
)
);
$product_downloads_section_group->add_block(
array(
'id' => 'product-downloadable',
'blockName' => 'woocommerce/product-checkbox-field',
'order' => 10,
'attributes' => array(
'property' => 'downloadable',
'label' => __( 'Include downloads', 'woocommerce' ),
),
)
);
$product_downloads_section_group->add_section(
array(
'id' => 'product-downloads-section',
'order' => 20,
'attributes' => array(
'title' => __( 'Downloads', 'woocommerce' ),
'description' => sprintf(
/* translators: %1$s: Downloads settings link opening tag. %2$s: Downloads settings link closing tag. */
__( 'Add any files you\'d like to make available for the customer to download after purchasing, such as instructions or warranty info. Store-wide updates can be managed in your %1$sproduct settings%2$s.', 'woocommerce' ),
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products&section=downloadable' ) . '" target="_blank" rel="noreferrer">',
'</a>'
),
),
'hideConditions' => array(
array(
'expression' => 'editedProduct.downloadable !== true',
),
),
)
)->add_block(
array(
'id' => 'product-downloads',
'blockName' => 'woocommerce/product-downloads-field',
'order' => 10,
)
);
}
}
}

View File

@ -7,11 +7,14 @@ namespace Automattic\WooCommerce\Internal\Admin\Features\ProductBlockEditor\Prod
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\ProductFormTemplateInterface;
use Automattic\WooCommerce\Internal\Admin\Features\ProductBlockEditor\ProductTemplates\DownloadableProductTrait;
/**
* Product Variation Template.
*/
class ProductVariationTemplate extends AbstractProductFormTemplate implements ProductFormTemplateInterface {
use DownloadableProductTrait;
/**
* The context name used to identify the editor.
*/
@ -185,29 +188,7 @@ class ProductVariationTemplate extends AbstractProductFormTemplate implements Pr
);
// Downloads section.
if ( Features::is_enabled( 'product-virtual-downloadable' ) ) {
$general_group->add_section(
array(
'id' => 'product-variation-downloads-section',
'order' => 40,
'attributes' => array(
'title' => __( 'Downloads', 'woocommerce' ),
'description' => sprintf(
/* translators: %1$s: Downloads settings link opening tag. %2$s: Downloads settings link closing tag. */
__( 'Add any files you\'d like to make available for the customer to download after purchasing, such as instructions or warranty info. Store-wide updates can be managed in your %1$sproduct settings%2$s.', 'woocommerce' ),
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products&section=downloadable' ) . '" target="_blank" rel="noreferrer">',
'</a>'
),
),
)
)->add_block(
array(
'id' => 'product-variation-downloads',
'blockName' => 'woocommerce/product-downloads-field',
'order' => 10,
)
);
}
$this->add_downloadable_product_blocks( $general_group );
}
/**

View File

@ -7,11 +7,14 @@ namespace Automattic\WooCommerce\Internal\Admin\Features\ProductBlockEditor\Prod
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\ProductFormTemplateInterface;
use Automattic\WooCommerce\Internal\Admin\Features\ProductBlockEditor\ProductTemplates\DownloadableProductTrait;
/**
* Simple Product Template.
*/
class SimpleProductTemplate extends AbstractProductFormTemplate implements ProductFormTemplateInterface {
use DownloadableProductTrait;
/**
* The context name used to identify the editor.
*/
@ -459,62 +462,9 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
),
)
);
// Downloads section.
if ( Features::is_enabled( 'product-virtual-downloadable' ) ) {
$product_downloads_section_group = $general_group->add_section(
array(
'id' => 'product-downloads-section-group',
'order' => 50,
'attributes' => array(
'blockGap' => 'unit-40',
),
'hideConditions' => array(
array(
'expression' => 'editedProduct.type !== "simple"',
),
),
)
);
$product_downloads_section_group->add_block(
array(
'id' => 'product-downloadable',
'blockName' => 'woocommerce/product-checkbox-field',
'order' => 10,
'attributes' => array(
'property' => 'downloadable',
'label' => __( 'Include downloads', 'woocommerce' ),
),
)
);
$product_downloads_section_group->add_section(
array(
'id' => 'product-downloads-section',
'order' => 20,
'attributes' => array(
'title' => __( 'Downloads', 'woocommerce' ),
'description' => sprintf(
/* translators: %1$s: Downloads settings link opening tag. %2$s: Downloads settings link closing tag. */
__( 'Add any files you\'d like to make available for the customer to download after purchasing, such as instructions or warranty info. Store-wide updates can be managed in your %1$sproduct settings%2$s.', 'woocommerce' ),
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products&section=downloadable' ) . '" target="_blank" rel="noreferrer">',
'</a>'
),
),
'hideConditions' => array(
array(
'expression' => 'editedProduct.downloadable !== true',
),
),
)
)->add_block(
array(
'id' => 'product-downloads',
'blockName' => 'woocommerce/product-downloads-field',
'order' => 10,
)
);
}
$this->add_downloadable_product_blocks( $general_group );
}
/**