initial commit
This commit is contained in:
674
packages/woocommerce-blocks/LICENSE
Normal file
674
packages/woocommerce-blocks/LICENSE
Normal file
@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
@ -0,0 +1,65 @@
|
||||
/* stylelint-disable block-closing-brace-newline-after */
|
||||
|
||||
// Breakpoints
|
||||
// Forked from https://github.com/Automattic/wp-calypso/blob/46ae24d8800fb85da6acf057a640e60dac988a38/assets/stylesheets/shared/mixins/_breakpoints.scss
|
||||
|
||||
// Think very carefully before adding a new breakpoint.
|
||||
// The list below is based on wp-admin's main breakpoints
|
||||
// See https://github.com/WordPress/gutenberg/tree/master/packages/viewport#usage
|
||||
$breakpoints: 480px, 600px, 782px, 960px, 1280px, 1440px;
|
||||
|
||||
// @todo refactor breakpoints so they use the mixins from Gutenberg
|
||||
// https://github.com/WordPress/gutenberg/blob/master/packages/base-styles/_mixins.scss
|
||||
@mixin breakpoint($sizes...) {
|
||||
@each $size in $sizes {
|
||||
@if type-of($size) == string {
|
||||
$approved-value: 0;
|
||||
@each $breakpoint in $breakpoints {
|
||||
$and-larger: ">" + $breakpoint;
|
||||
$and-smaller: "<" + $breakpoint;
|
||||
|
||||
@if $size == $and-smaller {
|
||||
$approved-value: 1;
|
||||
@media (max-width: $breakpoint) {
|
||||
@content;
|
||||
}
|
||||
}
|
||||
@else {
|
||||
@if $size == $and-larger {
|
||||
$approved-value: 2;
|
||||
@media (min-width: $breakpoint + 1) {
|
||||
@content;
|
||||
}
|
||||
}
|
||||
@else {
|
||||
@each $breakpoint-end in $breakpoints {
|
||||
$range: $breakpoint + "-" + $breakpoint-end;
|
||||
@if $size == $range {
|
||||
$approved-value: 3;
|
||||
@media (min-width: $breakpoint + 1) and (max-width: $breakpoint-end) {
|
||||
@content;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@if $approved-value == 0 {
|
||||
$sizes: "";
|
||||
@each $breakpoint in $breakpoints {
|
||||
$sizes: $sizes + " " + $breakpoint;
|
||||
}
|
||||
@warn "ERROR in breakpoint(#{ $size }) : You can only use these sizes[ #{$sizes} ] using the following syntax [ <#{ nth($breakpoints, 1) } >#{ nth($breakpoints, 1) } #{ nth($breakpoints, 1) }-#{ nth($breakpoints, 2) } ]";
|
||||
}
|
||||
}
|
||||
@else {
|
||||
$sizes: "";
|
||||
@each $breakpoint in $breakpoints {
|
||||
$sizes: $sizes + " " + $breakpoint;
|
||||
}
|
||||
@error "ERROR in breakpoint(#{ $size }) : Please wrap the breakpoint $size in parenthesis. You can use these sizes[ #{$sizes} ] using the following syntax [ <#{ nth($breakpoints, 1) } >#{ nth($breakpoints, 1) } #{ nth($breakpoints, 1) }-#{ nth($breakpoints, 2) } ]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* stylelint-enable */
|
@ -0,0 +1,21 @@
|
||||
@import "node_modules/@wordpress/base-styles/colors";
|
||||
@import "node_modules/@automattic/color-studio/dist/color-variables";
|
||||
|
||||
// Bright colors
|
||||
$no-stock-color: $alert-red;
|
||||
$low-stock-color: $alert-yellow;
|
||||
$in-stock-color: $alert-green;
|
||||
$discount-color: $alert-green;
|
||||
|
||||
$placeholder-color: var(--global--color-primary, $gray-200);
|
||||
$input-border-gray: #50575e;
|
||||
$input-border-dark: rgba(255, 255, 255, 0.4);
|
||||
$input-disabled-dark: rgba(255, 255, 255, 0.3);
|
||||
$controls-border-dark: rgba(255, 255, 255, 0.6);
|
||||
$input-text-active: #2b2d2f;
|
||||
$input-placeholder-dark: rgba(255, 255, 255, 0.6);
|
||||
$input-text-dark: #fff;
|
||||
$input-background-dark: rgba(0, 0, 0, 0.1);
|
||||
$select-dropdown-dark: #1e1e1e;
|
||||
$select-dropdown-light: #fff;
|
||||
$select-item-dark: rgba(0, 0, 0, 0.4);
|
194
packages/woocommerce-blocks/assets/css/abstracts/_mixins.scss
Normal file
194
packages/woocommerce-blocks/assets/css/abstracts/_mixins.scss
Normal file
@ -0,0 +1,194 @@
|
||||
$fontSizes: (
|
||||
"smaller": 0.75,
|
||||
"small": 0.875,
|
||||
"regular": 1,
|
||||
"large": 1.25,
|
||||
"larger": 2,
|
||||
);
|
||||
|
||||
// Maps a named font-size to its predefined size. Units default to em, but can
|
||||
// be changed using the multiplier parameter.
|
||||
@mixin font-size($sizeName, $multiplier: 1em) {
|
||||
font-size: map-get($fontSizes, $sizeName) * $multiplier;
|
||||
}
|
||||
|
||||
@keyframes loading-fade {
|
||||
0% {
|
||||
opacity: 0.7;
|
||||
}
|
||||
50% {
|
||||
opacity: 1;
|
||||
}
|
||||
100% {
|
||||
opacity: 0.7;
|
||||
}
|
||||
}
|
||||
|
||||
// Adds animation to placeholder section
|
||||
@mixin placeholder() {
|
||||
animation: loading-fade 1.2s ease-in-out infinite;
|
||||
background-color: $placeholder-color !important;
|
||||
color: $placeholder-color !important;
|
||||
outline: 0 !important;
|
||||
border: 0 !important;
|
||||
box-shadow: none;
|
||||
pointer-events: none;
|
||||
max-width: 100%;
|
||||
|
||||
// Forces direct descendants to keep layout but lose visibility.
|
||||
> * {
|
||||
visibility: hidden;
|
||||
}
|
||||
|
||||
@media screen and (prefers-reduced-motion: reduce) {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
|
||||
@mixin force-content() {
|
||||
&::after {
|
||||
content: "\00a0";
|
||||
}
|
||||
}
|
||||
|
||||
// Hide an element from sighted users, but available to screen reader users.
|
||||
@mixin visually-hidden() {
|
||||
border: 0;
|
||||
clip: rect(1px, 1px, 1px, 1px);
|
||||
clip-path: inset(50%);
|
||||
height: 1px;
|
||||
width: 1px;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
/* Many screen reader and browser combinations announce broken words as they would appear visually. */
|
||||
overflow-wrap: normal !important;
|
||||
word-wrap: normal !important;
|
||||
padding: 0;
|
||||
position: absolute !important;
|
||||
width: 1px;
|
||||
}
|
||||
|
||||
@mixin visually-hidden-focus-reveal() {
|
||||
background-color: #fff;
|
||||
border-radius: 3px;
|
||||
box-shadow: 0 0 2px 2px rgba(0, 0, 0, 0.6);
|
||||
clip: auto !important;
|
||||
clip-path: none;
|
||||
color: $input-text-active;
|
||||
display: block;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 700;
|
||||
height: auto;
|
||||
left: 5px;
|
||||
line-height: normal;
|
||||
padding: 15px 23px 14px;
|
||||
text-decoration: none;
|
||||
top: 5px;
|
||||
width: auto;
|
||||
z-index: 100000;
|
||||
}
|
||||
|
||||
@mixin reset-box() {
|
||||
border: 0;
|
||||
border-radius: 0;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
vertical-align: baseline;
|
||||
}
|
||||
|
||||
@mixin reset-typography() {
|
||||
color: inherit;
|
||||
font-family: inherit;
|
||||
font-size: inherit;
|
||||
font-style: inherit;
|
||||
font-weight: inherit;
|
||||
letter-spacing: inherit;
|
||||
line-height: inherit;
|
||||
text-decoration: inherit;
|
||||
text-transform: inherit;
|
||||
}
|
||||
|
||||
// Reset <h1>, <h2>, etc. styles as if they were text. Useful for elements that must be headings for a11y but don't need those styles.
|
||||
@mixin text-heading() {
|
||||
@include reset-box();
|
||||
@include reset-typography();
|
||||
box-shadow: none;
|
||||
display: inline;
|
||||
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
// Reset <button> style as if it was text. Useful for elements that must be `<button>` for a11y but don't need those styles.
|
||||
@mixin text-button() {
|
||||
@include reset-box();
|
||||
@include reset-typography();
|
||||
background: transparent;
|
||||
box-shadow: none;
|
||||
display: inline;
|
||||
text-shadow: none;
|
||||
|
||||
&:hover,
|
||||
&:focus,
|
||||
&:active {
|
||||
background: transparent;
|
||||
}
|
||||
}
|
||||
|
||||
// Reset <button> style so we can use link style for action buttons.
|
||||
@mixin link-button() {
|
||||
@include text-button();
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
// Makes sure long words are broken if they overflow the container.
|
||||
@mixin wrap-break-word() {
|
||||
// This is the current standard, works in most browsers.
|
||||
overflow-wrap: anywhere;
|
||||
// Safari supports word-break.
|
||||
word-break: break-word;
|
||||
// IE11 doesn't support overflow-wrap neither word-break: break-word, so we fallback to -ms-work-break: break-all.
|
||||
-ms-word-break: break-all;
|
||||
}
|
||||
|
||||
// Shows a border with the current color and a custom opacity. That can't be achieved
|
||||
// with normal border because `currentColor` doesn't allow tweaking the opacity, and
|
||||
// setting the opacity of the entire element would change the children's opacity too.
|
||||
@mixin with-translucent-border($border-width: 1px, $opacity: 0.3) {
|
||||
position: relative;
|
||||
|
||||
&::after {
|
||||
border-style: solid;
|
||||
border-width: $border-width;
|
||||
bottom: 0;
|
||||
content: "";
|
||||
display: block;
|
||||
left: 0;
|
||||
opacity: $opacity;
|
||||
pointer-events: none;
|
||||
position: absolute;
|
||||
right: 0;
|
||||
top: 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Wraps the content with a media query specially targeting IE11.
|
||||
@mixin ie11() {
|
||||
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
|
||||
@content;
|
||||
}
|
||||
}
|
||||
|
||||
// Converts a px unit to em.
|
||||
@function em($size, $base: 16px) {
|
||||
@return math.div($size, $base) * 1em;
|
||||
}
|
||||
|
||||
// Encodes hex colors so they can be used in URL content.
|
||||
@function encode-color($color) {
|
||||
@if type-of($color) != "color" or str-index(#{$color}, "#") != 1 {
|
||||
@return $color;
|
||||
}
|
||||
|
||||
$hex: str-slice(ie-hex-str($color), 4);
|
||||
@return "%23" + unquote("#{$hex}");
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
@import "node_modules/@wordpress/base-styles/variables";
|
||||
|
||||
$gap-largest: $grid-unit-50;
|
||||
$gap-larger: 4.5 * $grid-unit;
|
||||
$gap-large: $grid-unit-30;
|
||||
$gap: $grid-unit-20;
|
||||
$gap-small: $grid-unit-15;
|
||||
$gap-smaller: $grid-unit-10;
|
||||
$gap-smallest: $grid-unit-05;
|
||||
|
||||
// Cart block
|
||||
$cart-image-width: 5rem;
|
74
packages/woocommerce-blocks/assets/css/editor.scss
Normal file
74
packages/woocommerce-blocks/assets/css/editor.scss
Normal file
@ -0,0 +1,74 @@
|
||||
// Import the woocommerce components stylesheet
|
||||
@import "~@woocommerce/components/build-style/style.css";
|
||||
|
||||
// Hack to hide preview overflow.
|
||||
.editor-block-preview__content {
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
// Align the block icons in edit mode
|
||||
.components-placeholder__label .gridicon,
|
||||
.components-placeholder__label .material-icon {
|
||||
margin-right: 1ch;
|
||||
fill: currentColor;
|
||||
}
|
||||
|
||||
// Remove the list styling, which is added back by core GB styles.
|
||||
.editor-styles-wrapper {
|
||||
.wc-block-grid {
|
||||
.wc-block-grid__products {
|
||||
list-style: none;
|
||||
margin: 0 (-$gap*0.5) $gap;
|
||||
padding: 0;
|
||||
|
||||
.wc-block-grid__product {
|
||||
margin: 0 0 $gap-large 0;
|
||||
}
|
||||
}
|
||||
|
||||
&.components-placeholder {
|
||||
padding: 2em 1em;
|
||||
}
|
||||
|
||||
&.is-loading,
|
||||
&.is-not-found {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Style inline notices in the inspector.
|
||||
.components-base-control {
|
||||
+ .wc-block-base-control-notice {
|
||||
margin: -$gap 0 $gap;
|
||||
}
|
||||
|
||||
& + .wc-block-base-control-notice:last-child {
|
||||
margin: -$gap 0 $gap-small;
|
||||
}
|
||||
}
|
||||
|
||||
// Selectors with extra specificity to override some editor styles.
|
||||
.woocommerce-search-list__list.woocommerce-search-list__list {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.woocommerce-search-list__selected.woocommerce-search-list__selected > ul {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.theme-twentytwenty {
|
||||
.wp-block {
|
||||
.wc-block-grid__product-title,
|
||||
.wc-block-active-filters__title,
|
||||
.wc-block-attribute-filter__title,
|
||||
.wc-block-price-filter__title,
|
||||
.wc-block-stock-filter__title {
|
||||
@include font-size(regular);
|
||||
}
|
||||
}
|
||||
}
|
325
packages/woocommerce-blocks/assets/css/style.scss
Normal file
325
packages/woocommerce-blocks/assets/css/style.scss
Normal file
@ -0,0 +1,325 @@
|
||||
.wc-block-link-button {
|
||||
@include link-button();
|
||||
}
|
||||
|
||||
.wc-block-suspense-placeholder {
|
||||
@include placeholder();
|
||||
@include force-content();
|
||||
}
|
||||
|
||||
// These styles are for the server side rendered product grid blocks.
|
||||
.wc-block-grid__products .wc-block-grid__product-image {
|
||||
text-decoration: none;
|
||||
display: block;
|
||||
position: relative;
|
||||
|
||||
a {
|
||||
text-decoration: none;
|
||||
border: 0;
|
||||
outline: 0;
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
img {
|
||||
height: auto;
|
||||
width: 100%;
|
||||
|
||||
&[hidden] {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
.edit-post-visual-editor .editor-block-list__block .wc-block-grid__product-title,
|
||||
.editor-styles-wrapper .wc-block-grid__product-title,
|
||||
.wc-block-grid__product-title {
|
||||
font-family: inherit;
|
||||
line-height: 1.2em;
|
||||
font-weight: 700;
|
||||
padding: 0;
|
||||
color: inherit;
|
||||
font-size: inherit;
|
||||
display: block;
|
||||
}
|
||||
.wc-block-grid__product-price {
|
||||
display: block;
|
||||
|
||||
.wc-block-grid__product-price__regular {
|
||||
margin-right: 0.5em;
|
||||
}
|
||||
}
|
||||
.wc-block-grid__product-add-to-cart.wp-block-button {
|
||||
word-break: break-word;
|
||||
white-space: normal;
|
||||
.wp-block-button__link {
|
||||
word-break: break-word;
|
||||
white-space: normal;
|
||||
margin: 0 auto !important;
|
||||
display: inline-flex;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
// Set button font size and padding so it inherits from parent.
|
||||
padding: 0.5em 1em;
|
||||
font-size: 1em;
|
||||
|
||||
&.loading {
|
||||
opacity: 0.25;
|
||||
}
|
||||
|
||||
&.added::after {
|
||||
font-family: WooCommerce; /* stylelint-disable-line */
|
||||
content: "\e017";
|
||||
margin-left: 0.5em;
|
||||
display: inline-block;
|
||||
width: auto;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
&.loading::after {
|
||||
font-family: WooCommerce; /* stylelint-disable-line */
|
||||
content: "\e031";
|
||||
animation: spin 2s linear infinite;
|
||||
margin-left: 0.5em;
|
||||
display: inline-block;
|
||||
width: auto;
|
||||
height: auto;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Remove button sugar if unlikely to fit.
|
||||
.has-5-columns:not(.alignfull),
|
||||
.has-6-columns,
|
||||
.has-7-columns,
|
||||
.has-8-columns,
|
||||
.has-9-columns {
|
||||
.wc-block-grid__product-add-to-cart.wp-block-button .wp-block-button__link::after {
|
||||
content: "";
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.wc-block-grid__product-rating {
|
||||
display: block;
|
||||
|
||||
.wc-block-grid__product-rating__stars,
|
||||
.star-rating {
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
width: 5.3em;
|
||||
height: 1.618em;
|
||||
line-height: 1.618;
|
||||
font-size: 1em;
|
||||
/* stylelint-disable-next-line font-family-no-missing-generic-family-keyword */
|
||||
font-family: star;
|
||||
font-weight: 400;
|
||||
margin: 0 auto;
|
||||
text-align: left;
|
||||
|
||||
&::before {
|
||||
content: "\53\53\53\53\53";
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
position: absolute;
|
||||
opacity: 0.5;
|
||||
color: #aaa;
|
||||
white-space: nowrap;
|
||||
}
|
||||
span {
|
||||
overflow: hidden;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
position: absolute;
|
||||
padding-top: 1.5em;
|
||||
}
|
||||
span::before {
|
||||
content: "\53\53\53\53\53";
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
position: absolute;
|
||||
color: #000;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
}
|
||||
.wc-block-grid__product-onsale {
|
||||
@include font-size(small);
|
||||
padding: em($gap-smallest) em($gap-small);
|
||||
display: inline-block;
|
||||
width: auto;
|
||||
border: 1px solid #43454b;
|
||||
border-radius: 3px;
|
||||
color: #43454b;
|
||||
background: #fff;
|
||||
text-align: center;
|
||||
text-transform: uppercase;
|
||||
font-weight: 600;
|
||||
z-index: 9;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
// Element spacing.
|
||||
.wc-block-grid__product {
|
||||
.wc-block-grid__product-image,
|
||||
.wc-block-grid__product-title {
|
||||
margin: 0 0 $gap-small;
|
||||
}
|
||||
// If centered when toggling alignment on, use auto margins to prevent flexbox stretching it.
|
||||
.wc-block-grid__product-price,
|
||||
.wc-block-grid__product-rating,
|
||||
.wc-block-grid__product-add-to-cart,
|
||||
.wc-block-grid__product-onsale {
|
||||
margin: 0 auto $gap-small;
|
||||
}
|
||||
}
|
||||
|
||||
.theme-twentysixteen {
|
||||
.wc-block-grid {
|
||||
// Prevent white theme styles.
|
||||
.price ins {
|
||||
color: #77a464;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.theme-twentynineteen {
|
||||
.wc-block-grid__product {
|
||||
font-size: 0.88889em;
|
||||
}
|
||||
// Change the title font to match headings.
|
||||
.wc-block-grid__product-title,
|
||||
.wc-block-grid__product-onsale,
|
||||
.wc-block-components-product-title,
|
||||
.wc-block-components-product-sale-badge {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
|
||||
}
|
||||
.wc-block-grid__product-title::before {
|
||||
display: none;
|
||||
}
|
||||
.wc-block-grid__product-onsale,
|
||||
.wc-block-components-product-sale-badge {
|
||||
line-height: 1;
|
||||
}
|
||||
.editor-styles-wrapper .wp-block-button .wp-block-button__link:not(.has-text-color) {
|
||||
color: #fff;
|
||||
}
|
||||
}
|
||||
|
||||
.theme-twentytwenty {
|
||||
$twentytwenty-headings: -apple-system, blinkmacsystemfont, "Helvetica Neue", helvetica, sans-serif;
|
||||
$twentytwenty-highlights-color: #cd2653;
|
||||
|
||||
.wc-block-grid__product-link {
|
||||
color: #000;
|
||||
}
|
||||
|
||||
.wc-block-grid__product-title,
|
||||
.wc-block-components-product-title {
|
||||
font-family: $twentytwenty-headings;
|
||||
color: $twentytwenty-highlights-color;
|
||||
@include font-size(regular);
|
||||
}
|
||||
|
||||
.wp-block-columns .wc-block-components-product-title {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.wc-block-grid__product-price,
|
||||
.wc-block-components-product-price {
|
||||
&__value,
|
||||
.woocommerce-Price-amount {
|
||||
font-family: $twentytwenty-headings;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
|
||||
del {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
ins {
|
||||
text-decoration: none;
|
||||
}
|
||||
}
|
||||
|
||||
.wc-block-grid__product-rating,
|
||||
.star-rating {
|
||||
font-size: 0.7em;
|
||||
|
||||
.wc-block-grid__product-rating__stars,
|
||||
.wc-block-components-product-rating__stars {
|
||||
line-height: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.wc-block-grid__product-add-to-cart > .wp-block-button__link,
|
||||
.wc-block-components-product-button > .wp-block-button__link {
|
||||
font-family: $twentytwenty-headings;
|
||||
}
|
||||
|
||||
.wc-block-grid__products .wc-block-grid__product-onsale,
|
||||
.wc-block-components-product-sale-badge {
|
||||
background: $twentytwenty-highlights-color;
|
||||
color: #fff;
|
||||
font-family: $twentytwenty-headings;
|
||||
font-weight: 700;
|
||||
letter-spacing: -0.02em;
|
||||
line-height: 1.2;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
// Override style from WC Core that set its position to absolute.
|
||||
// These rulesets can be removed once https://github.com/woocommerce/woocommerce/pull/26516 is released.
|
||||
.wc-block-grid__products .wc-block-components-product-sale-badge {
|
||||
position: static;
|
||||
}
|
||||
.wc-block-grid__products .wc-block-grid__product-image .wc-block-components-product-sale-badge {
|
||||
position: absolute;
|
||||
}
|
||||
|
||||
// These styles are not applied to the All Products atomic block, so it can be positioned normally.
|
||||
.wc-block-grid__products .wc-block-grid__product-onsale:not(.wc-block-components-product-sale-badge) {
|
||||
position: absolute;
|
||||
right: 4px;
|
||||
top: 4px;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.wc-block-active-filters__title,
|
||||
.wc-block-attribute-filter__title,
|
||||
.wc-block-price-filter__title,
|
||||
.wc-block-stock-filter__title {
|
||||
@include font-size(regular);
|
||||
}
|
||||
|
||||
.wc-block-active-filters .wc-block-active-filters__clear-all {
|
||||
@include font-size(smaller);
|
||||
}
|
||||
|
||||
.wc-block-grid__product-add-to-cart.wp-block-button .wp-block-button__link {
|
||||
@include font-size(smaller);
|
||||
padding: em($gap-smaller);
|
||||
}
|
||||
|
||||
@media only screen and (min-width: 768px) {
|
||||
.wc-block-grid__products .wc-block-grid__product-onsale {
|
||||
@include font-size(small);
|
||||
padding: em($gap-smaller);
|
||||
}
|
||||
}
|
||||
|
||||
@media only screen and (min-width: 1168px) {
|
||||
.wc-block-grid__products .wc-block-grid__product-onsale {
|
||||
@include font-size(small);
|
||||
padding: em($gap-smaller);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Default screen-reader styles. Included as a fallback for themes that don't have support.
|
||||
.screen-reader-text {
|
||||
@include visually-hidden();
|
||||
}
|
||||
.screen-reader-text:focus {
|
||||
@include visually-hidden-focus-reveal();
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerBlockComponent } from '@woocommerce/blocks-registry';
|
||||
import { lazy } from '@wordpress/element';
|
||||
import { WC_BLOCKS_BUILD_URL } from '@woocommerce/block-settings';
|
||||
|
||||
// Modify webpack publicPath at runtime based on location of WordPress Plugin.
|
||||
// eslint-disable-next-line no-undef,camelcase
|
||||
__webpack_public_path__ = WC_BLOCKS_BUILD_URL;
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-price',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/price" */ './product-elements/price/block'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-image',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/image" */ './product-elements/image/frontend'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-title',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/title" */ './product-elements/title/frontend'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-rating',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/rating" */ './product-elements/rating/block'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-button',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/button" */ './product-elements/button/block'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-summary',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/summary" */ './product-elements/summary/block'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-sale-badge',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/sale-badge" */ './product-elements/sale-badge/block'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-sku',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/sku" */ './product-elements/sku/block'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-category-list',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/category-list" */ './product-elements/category-list/block'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-tag-list',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/tag-list" */ './product-elements/tag-list/block'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-stock-indicator',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/stock-indicator" */ './product-elements/stock-indicator/block'
|
||||
)
|
||||
),
|
||||
} );
|
||||
|
||||
registerBlockComponent( {
|
||||
blockName: 'woocommerce/product-add-to-cart',
|
||||
component: lazy( () =>
|
||||
import(
|
||||
/* webpackChunkName: "atomic-block-components/add-to-cart" */ './product-elements/add-to-cart/frontend'
|
||||
)
|
||||
),
|
||||
} );
|
15
packages/woocommerce-blocks/assets/js/atomic/blocks/index.js
Normal file
15
packages/woocommerce-blocks/assets/js/atomic/blocks/index.js
Normal file
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './product-elements/title';
|
||||
import './product-elements/price';
|
||||
import './product-elements/image';
|
||||
import './product-elements/rating';
|
||||
import './product-elements/button';
|
||||
import './product-elements/summary';
|
||||
import './product-elements/sale-badge';
|
||||
import './product-elements/sku';
|
||||
import './product-elements/category-list';
|
||||
import './product-elements/tag-list';
|
||||
import './product-elements/stock-indicator';
|
||||
import './product-elements/add-to-cart';
|
@ -0,0 +1,12 @@
|
||||
export const blockAttributes = {
|
||||
showFormElements: {
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
},
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,87 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import PropTypes from 'prop-types';
|
||||
import classnames from 'classnames';
|
||||
import {
|
||||
AddToCartFormContextProvider,
|
||||
useAddToCartFormContext,
|
||||
} from '@woocommerce/base-context';
|
||||
import { useProductDataContext } from '@woocommerce/shared-context';
|
||||
import { isEmpty } from 'lodash';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
import { AddToCartButton } from './shared';
|
||||
import {
|
||||
SimpleProductForm,
|
||||
VariableProductForm,
|
||||
ExternalProductForm,
|
||||
GroupedProductForm,
|
||||
} from './product-types';
|
||||
|
||||
/**
|
||||
* Product Add to Form Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @param {boolean} [props.showFormElements] Should form elements be shown?
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( { className, showFormElements } ) => {
|
||||
const { product } = useProductDataContext();
|
||||
const componentClass = classnames(
|
||||
className,
|
||||
'wc-block-components-product-add-to-cart',
|
||||
{
|
||||
'wc-block-components-product-add-to-cart--placeholder': isEmpty(
|
||||
product
|
||||
),
|
||||
}
|
||||
);
|
||||
|
||||
return (
|
||||
<AddToCartFormContextProvider
|
||||
product={ product }
|
||||
showFormElements={ showFormElements }
|
||||
>
|
||||
<div className={ componentClass }>
|
||||
<AddToCartForm />
|
||||
</div>
|
||||
</AddToCartFormContextProvider>
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Renders the add to cart form using useAddToCartFormContext.
|
||||
*/
|
||||
const AddToCartForm = () => {
|
||||
const { showFormElements, productType } = useAddToCartFormContext();
|
||||
|
||||
if ( showFormElements ) {
|
||||
if ( productType === 'variable' ) {
|
||||
return <VariableProductForm />;
|
||||
}
|
||||
if ( productType === 'grouped' ) {
|
||||
return <GroupedProductForm />;
|
||||
}
|
||||
if ( productType === 'external' ) {
|
||||
return <ExternalProductForm />;
|
||||
}
|
||||
if ( productType === 'simple' || productType === 'variation' ) {
|
||||
return <SimpleProductForm />;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return <AddToCartButton />;
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { cart, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __( 'Add to Cart', 'woocommerce' );
|
||||
export const BLOCK_ICON = <Icon srcElement={ cart } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Displays an add to cart button. Optionally displays other add to cart form elements.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import EditProductLink from '@woocommerce/editor-components/edit-product-link';
|
||||
import { useProductDataContext } from '@woocommerce/shared-context';
|
||||
import classnames from 'classnames';
|
||||
import {
|
||||
Disabled,
|
||||
PanelBody,
|
||||
ToggleControl,
|
||||
Notice,
|
||||
} from '@wordpress/components';
|
||||
import { InspectorControls } from '@wordpress/block-editor';
|
||||
import { productSupportsAddToCartForm } from '@woocommerce/base-utils';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes, setAttributes } ) => {
|
||||
const { product } = useProductDataContext();
|
||||
const { className, showFormElements } = attributes;
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-add-to-cart'
|
||||
) }
|
||||
>
|
||||
<EditProductLink productId={ product.id } />
|
||||
<InspectorControls>
|
||||
<PanelBody
|
||||
title={ __( 'Layout', 'woocommerce' ) }
|
||||
>
|
||||
{ productSupportsAddToCartForm( product ) ? (
|
||||
<ToggleControl
|
||||
label={ __(
|
||||
'Display form elements',
|
||||
'woocommerce'
|
||||
) }
|
||||
help={ __(
|
||||
'Depending on product type, allow customers to select a quantity, variations etc.',
|
||||
'woocommerce'
|
||||
) }
|
||||
checked={ showFormElements }
|
||||
onChange={ () =>
|
||||
setAttributes( {
|
||||
showFormElements: ! showFormElements,
|
||||
} )
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<Notice
|
||||
className="wc-block-components-product-add-to-cart-notice"
|
||||
isDismissible={ false }
|
||||
status="info"
|
||||
>
|
||||
{ __(
|
||||
'This product does not support the block based add to cart form. A link to the product page will be shown instead.',
|
||||
'woocommerce'
|
||||
) }
|
||||
</Notice>
|
||||
) }
|
||||
</PanelBody>
|
||||
</InspectorControls>
|
||||
<Disabled>
|
||||
<Block { ...attributes } />
|
||||
</Disabled>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its add to cart form.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import withFilteredAttributes from '@woocommerce/base-hocs/with-filtered-attributes';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import attributes from './attributes';
|
||||
|
||||
export default withFilteredAttributes( attributes )( Block );
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerExperimentalBlockType } from '@woocommerce/block-settings';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import edit from './edit';
|
||||
import attributes from './attributes';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
edit,
|
||||
attributes,
|
||||
};
|
||||
|
||||
registerExperimentalBlockType( 'woocommerce/product-add-to-cart', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import AddToCartButton from '../shared/add-to-cart-button';
|
||||
|
||||
/**
|
||||
* External Product Add To Cart Form
|
||||
*/
|
||||
const External = () => {
|
||||
return <AddToCartButton />;
|
||||
};
|
||||
|
||||
export default External;
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { Placeholder } from 'wordpress-components';
|
||||
|
||||
const GroupedProducts = () => {
|
||||
return (
|
||||
<Placeholder className="wc-block-components-product-add-to-cart-group-list">
|
||||
This is a placeholder for the grouped products form element.
|
||||
</Placeholder>
|
||||
);
|
||||
};
|
||||
|
||||
export default GroupedProducts;
|
@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import GroupList from './group-list';
|
||||
|
||||
/**
|
||||
* Grouped Product Add To Cart Form
|
||||
*/
|
||||
const Grouped = () => {
|
||||
return <GroupList />;
|
||||
};
|
||||
|
||||
export default Grouped;
|
@ -0,0 +1,4 @@
|
||||
export { default as SimpleProductForm } from './simple';
|
||||
export { default as VariableProductForm } from './variable/index';
|
||||
export { default as ExternalProductForm } from './external';
|
||||
export { default as GroupedProductForm } from './grouped/index';
|
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { useAddToCartFormContext } from '@woocommerce/base-context';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { AddToCartButton, QuantityInput, ProductUnavailable } from '../shared';
|
||||
|
||||
/**
|
||||
* Simple Product Add To Cart Form
|
||||
*/
|
||||
const Simple = () => {
|
||||
const {
|
||||
product,
|
||||
quantity,
|
||||
minQuantity,
|
||||
maxQuantity,
|
||||
dispatchActions,
|
||||
isDisabled,
|
||||
} = useAddToCartFormContext();
|
||||
|
||||
if ( product.id && ! product.is_purchasable ) {
|
||||
return <ProductUnavailable />;
|
||||
}
|
||||
|
||||
if ( product.id && ! product.is_in_stock ) {
|
||||
return (
|
||||
<ProductUnavailable
|
||||
reason={ __(
|
||||
'This product is currently out of stock and cannot be purchased.',
|
||||
'woocommerce'
|
||||
) }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<QuantityInput
|
||||
value={ quantity }
|
||||
min={ minQuantity }
|
||||
max={ maxQuantity }
|
||||
disabled={ isDisabled }
|
||||
onChange={ dispatchActions.setQuantity }
|
||||
/>
|
||||
<AddToCartButton />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Simple;
|
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { useAddToCartFormContext } from '@woocommerce/base-context';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import {
|
||||
AddToCartButton,
|
||||
QuantityInput,
|
||||
ProductUnavailable,
|
||||
} from '../../shared';
|
||||
import VariationAttributes from './variation-attributes';
|
||||
|
||||
/**
|
||||
* Variable Product Add To Cart Form
|
||||
*/
|
||||
const Variable = () => {
|
||||
const {
|
||||
product,
|
||||
quantity,
|
||||
minQuantity,
|
||||
maxQuantity,
|
||||
dispatchActions,
|
||||
isDisabled,
|
||||
} = useAddToCartFormContext();
|
||||
|
||||
if ( product.id && ! product.is_purchasable ) {
|
||||
return <ProductUnavailable />;
|
||||
}
|
||||
|
||||
if ( product.id && ! product.is_in_stock ) {
|
||||
return (
|
||||
<ProductUnavailable
|
||||
reason={ __(
|
||||
'This product is currently out of stock and cannot be purchased.',
|
||||
'woocommerce'
|
||||
) }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<VariationAttributes
|
||||
product={ product }
|
||||
dispatchers={ dispatchActions }
|
||||
/>
|
||||
<QuantityInput
|
||||
value={ quantity }
|
||||
min={ minQuantity }
|
||||
max={ maxQuantity }
|
||||
disabled={ isDisabled }
|
||||
onChange={ dispatchActions.setQuantity }
|
||||
/>
|
||||
<AddToCartButton />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default Variable;
|
@ -0,0 +1,116 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { useState, useEffect, useMemo } from '@wordpress/element';
|
||||
import { useShallowEqual } from '@woocommerce/base-hooks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import AttributeSelectControl from './attribute-select-control';
|
||||
import {
|
||||
getVariationMatchingSelectedAttributes,
|
||||
getActiveSelectControlOptions,
|
||||
getDefaultAttributes,
|
||||
} from './utils';
|
||||
|
||||
/**
|
||||
* AttributePicker component.
|
||||
*
|
||||
* @param {*} props Component props.
|
||||
*/
|
||||
const AttributePicker = ( {
|
||||
attributes,
|
||||
variationAttributes,
|
||||
setRequestParams,
|
||||
} ) => {
|
||||
const currentAttributes = useShallowEqual( attributes );
|
||||
const currentVariationAttributes = useShallowEqual( variationAttributes );
|
||||
const [ variationId, setVariationId ] = useState( 0 );
|
||||
const [ selectedAttributes, setSelectedAttributes ] = useState( {} );
|
||||
const [ hasSetDefaults, setHasSetDefaults ] = useState( false );
|
||||
|
||||
// Get options for each attribute picker.
|
||||
const filteredAttributeOptions = useMemo( () => {
|
||||
return getActiveSelectControlOptions(
|
||||
currentAttributes,
|
||||
currentVariationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
}, [ selectedAttributes, currentAttributes, currentVariationAttributes ] );
|
||||
|
||||
// Set default attributes as selected.
|
||||
useEffect( () => {
|
||||
if ( ! hasSetDefaults ) {
|
||||
const defaultAttributes = getDefaultAttributes( attributes );
|
||||
if ( defaultAttributes ) {
|
||||
setSelectedAttributes( {
|
||||
...defaultAttributes,
|
||||
} );
|
||||
}
|
||||
setHasSetDefaults( true );
|
||||
}
|
||||
}, [ selectedAttributes, attributes, hasSetDefaults ] );
|
||||
|
||||
// Select variations when selections are change.
|
||||
useEffect( () => {
|
||||
const hasSelectedAllAttributes =
|
||||
Object.values( selectedAttributes ).filter(
|
||||
( selected ) => selected !== ''
|
||||
).length === Object.keys( currentAttributes ).length;
|
||||
|
||||
if ( hasSelectedAllAttributes ) {
|
||||
setVariationId(
|
||||
getVariationMatchingSelectedAttributes(
|
||||
currentAttributes,
|
||||
currentVariationAttributes,
|
||||
selectedAttributes
|
||||
)
|
||||
);
|
||||
} else if ( variationId > 0 ) {
|
||||
// Unset variation when form is incomplete.
|
||||
setVariationId( 0 );
|
||||
}
|
||||
}, [
|
||||
selectedAttributes,
|
||||
variationId,
|
||||
currentAttributes,
|
||||
currentVariationAttributes,
|
||||
] );
|
||||
|
||||
// Set requests params as variation ID and data changes.
|
||||
useEffect( () => {
|
||||
setRequestParams( {
|
||||
id: variationId,
|
||||
variation: Object.keys( selectedAttributes ).map(
|
||||
( attributeName ) => {
|
||||
return {
|
||||
attribute: attributeName,
|
||||
value: selectedAttributes[ attributeName ],
|
||||
};
|
||||
}
|
||||
),
|
||||
} );
|
||||
}, [ setRequestParams, variationId, selectedAttributes ] );
|
||||
|
||||
return (
|
||||
<div className="wc-block-components-product-add-to-cart-attribute-picker">
|
||||
{ Object.keys( currentAttributes ).map( ( attributeName ) => (
|
||||
<AttributeSelectControl
|
||||
key={ attributeName }
|
||||
attributeName={ attributeName }
|
||||
options={ filteredAttributeOptions[ attributeName ] }
|
||||
value={ selectedAttributes[ attributeName ] }
|
||||
onChange={ ( selected ) => {
|
||||
setSelectedAttributes( {
|
||||
...selectedAttributes,
|
||||
[ attributeName ]: selected,
|
||||
} );
|
||||
} }
|
||||
/>
|
||||
) ) }
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default AttributePicker;
|
@ -0,0 +1,91 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { decodeEntities } from '@wordpress/html-entities';
|
||||
import { SelectControl } from 'wordpress-components';
|
||||
import { useEffect } from 'react';
|
||||
import classnames from 'classnames';
|
||||
import {
|
||||
ValidationInputError,
|
||||
useValidationContext,
|
||||
} from '@woocommerce/base-context';
|
||||
|
||||
// Default option for select boxes.
|
||||
const selectAnOption = {
|
||||
value: '',
|
||||
label: __( 'Select an option', 'woocommerce' ),
|
||||
};
|
||||
|
||||
/**
|
||||
* VariationAttributeSelect component.
|
||||
*
|
||||
* @param {*} props Component props.
|
||||
*/
|
||||
const AttributeSelectControl = ( {
|
||||
attributeName,
|
||||
options = [],
|
||||
value = '',
|
||||
onChange = () => {},
|
||||
errorMessage = __(
|
||||
'Please select a value.',
|
||||
'woocommerce'
|
||||
),
|
||||
} ) => {
|
||||
const {
|
||||
getValidationError,
|
||||
setValidationErrors,
|
||||
clearValidationError,
|
||||
} = useValidationContext();
|
||||
const errorId = attributeName;
|
||||
const error = getValidationError( errorId ) || {};
|
||||
|
||||
useEffect( () => {
|
||||
if ( value ) {
|
||||
clearValidationError( errorId );
|
||||
} else {
|
||||
setValidationErrors( {
|
||||
[ errorId ]: {
|
||||
message: errorMessage,
|
||||
hidden: true,
|
||||
},
|
||||
} );
|
||||
}
|
||||
}, [
|
||||
value,
|
||||
errorId,
|
||||
errorMessage,
|
||||
clearValidationError,
|
||||
setValidationErrors,
|
||||
] );
|
||||
|
||||
// Remove validation errors when unmounted.
|
||||
useEffect( () => () => void clearValidationError( errorId ), [
|
||||
errorId,
|
||||
clearValidationError,
|
||||
] );
|
||||
|
||||
return (
|
||||
<div className="wc-block-components-product-add-to-cart-attribute-picker__container">
|
||||
<SelectControl
|
||||
label={ decodeEntities( attributeName ) }
|
||||
value={ value || '' }
|
||||
options={ [ selectAnOption, ...options ] }
|
||||
onChange={ onChange }
|
||||
required={ true }
|
||||
className={ classnames(
|
||||
'wc-block-components-product-add-to-cart-attribute-picker__select',
|
||||
{
|
||||
'has-error': error.message && ! error.hidden,
|
||||
}
|
||||
) }
|
||||
/>
|
||||
<ValidationInputError
|
||||
propertyName={ errorId }
|
||||
elementId={ errorId }
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default AttributeSelectControl;
|
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
import AttributePicker from './attribute-picker';
|
||||
import { getAttributes, getVariationAttributes } from './utils';
|
||||
|
||||
/**
|
||||
* VariationAttributes component.
|
||||
*
|
||||
* @param {Object} props Incoming props
|
||||
* @param {Object} props.product Product
|
||||
* @param {Object} props.dispatchers An object where values are dispatching functions.
|
||||
*/
|
||||
const VariationAttributes = ( { product, dispatchers } ) => {
|
||||
const attributes = getAttributes( product.attributes );
|
||||
const variationAttributes = getVariationAttributes( product.variations );
|
||||
if (
|
||||
Object.keys( attributes ).length === 0 ||
|
||||
variationAttributes.length === 0
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<AttributePicker
|
||||
attributes={ attributes }
|
||||
variationAttributes={ variationAttributes }
|
||||
setRequestParams={ dispatchers.setRequestParams }
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default VariationAttributes;
|
@ -0,0 +1,33 @@
|
||||
.wc-block-components-product-add-to-cart-attribute-picker {
|
||||
margin: 0;
|
||||
flex-basis: 100%;
|
||||
|
||||
label {
|
||||
display: block;
|
||||
@include font-size(regular);
|
||||
}
|
||||
|
||||
.wc-block-components-product-add-to-cart-attribute-picker__container {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.wc-block-components-product-add-to-cart-attribute-picker__select {
|
||||
margin: 0 0 em($gap-small) 0;
|
||||
|
||||
select {
|
||||
min-width: 60%;
|
||||
min-height: 1.75em;
|
||||
}
|
||||
|
||||
&.has-error {
|
||||
margin-bottom: $gap-large;
|
||||
|
||||
select {
|
||||
border-color: $alert-red;
|
||||
&:focus {
|
||||
outline-color: $alert-red;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,479 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import {
|
||||
getAttributes,
|
||||
getVariationAttributes,
|
||||
getVariationsMatchingSelectedAttributes,
|
||||
getVariationMatchingSelectedAttributes,
|
||||
getActiveSelectControlOptions,
|
||||
getDefaultAttributes,
|
||||
} from '../utils';
|
||||
|
||||
const rawAttributeData = [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Color',
|
||||
taxonomy: 'pa_color',
|
||||
has_variations: true,
|
||||
terms: [
|
||||
{
|
||||
id: 22,
|
||||
name: 'Blue',
|
||||
slug: 'blue',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
id: 23,
|
||||
name: 'Green',
|
||||
slug: 'green',
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
id: 24,
|
||||
name: 'Red',
|
||||
slug: 'red',
|
||||
default: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 0,
|
||||
name: 'Logo',
|
||||
taxonomy: null,
|
||||
has_variations: true,
|
||||
terms: [
|
||||
{
|
||||
id: 0,
|
||||
name: 'Yes',
|
||||
slug: 'Yes',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
id: 0,
|
||||
name: 'No',
|
||||
slug: 'No',
|
||||
default: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 0,
|
||||
name: 'Non-variable attribute',
|
||||
taxonomy: null,
|
||||
has_variations: false,
|
||||
terms: [
|
||||
{
|
||||
id: 0,
|
||||
name: 'Test',
|
||||
slug: 'Test',
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
id: 0,
|
||||
name: 'Test 2',
|
||||
slug: 'Test 2',
|
||||
default: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const rawVariations = [
|
||||
{
|
||||
id: 35,
|
||||
attributes: [
|
||||
{
|
||||
name: 'Color',
|
||||
value: 'blue',
|
||||
},
|
||||
{
|
||||
name: 'Logo',
|
||||
value: 'Yes',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 28,
|
||||
attributes: [
|
||||
{
|
||||
name: 'Color',
|
||||
value: 'red',
|
||||
},
|
||||
{
|
||||
name: 'Logo',
|
||||
value: 'No',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 29,
|
||||
attributes: [
|
||||
{
|
||||
name: 'Color',
|
||||
value: 'green',
|
||||
},
|
||||
{
|
||||
name: 'Logo',
|
||||
value: 'No',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 30,
|
||||
attributes: [
|
||||
{
|
||||
name: 'Color',
|
||||
value: 'blue',
|
||||
},
|
||||
{
|
||||
name: 'Logo',
|
||||
value: 'No',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
const formattedAttributes = {
|
||||
Color: {
|
||||
id: 1,
|
||||
name: 'Color',
|
||||
taxonomy: 'pa_color',
|
||||
has_variations: true,
|
||||
terms: [
|
||||
{
|
||||
id: 22,
|
||||
name: 'Blue',
|
||||
slug: 'blue',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
id: 23,
|
||||
name: 'Green',
|
||||
slug: 'green',
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
id: 24,
|
||||
name: 'Red',
|
||||
slug: 'red',
|
||||
default: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
Size: {
|
||||
id: 2,
|
||||
name: 'Size',
|
||||
taxonomy: 'pa_size',
|
||||
has_variations: true,
|
||||
terms: [
|
||||
{
|
||||
id: 25,
|
||||
name: 'Large',
|
||||
slug: 'large',
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
id: 26,
|
||||
name: 'Medium',
|
||||
slug: 'medium',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
id: 27,
|
||||
name: 'Small',
|
||||
slug: 'small',
|
||||
default: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
describe( 'Testing utils', () => {
|
||||
describe( 'Testing getAttributes()', () => {
|
||||
it( 'returns empty object if there are no attributes', () => {
|
||||
const attributes = getAttributes( null );
|
||||
expect( attributes ).toStrictEqual( {} );
|
||||
} );
|
||||
it( 'returns list of attributes when given valid data', () => {
|
||||
const attributes = getAttributes( rawAttributeData );
|
||||
expect( attributes ).toStrictEqual( {
|
||||
Color: {
|
||||
id: 1,
|
||||
name: 'Color',
|
||||
taxonomy: 'pa_color',
|
||||
has_variations: true,
|
||||
terms: [
|
||||
{
|
||||
id: 22,
|
||||
name: 'Blue',
|
||||
slug: 'blue',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
id: 23,
|
||||
name: 'Green',
|
||||
slug: 'green',
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
id: 24,
|
||||
name: 'Red',
|
||||
slug: 'red',
|
||||
default: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
Logo: {
|
||||
id: 0,
|
||||
name: 'Logo',
|
||||
taxonomy: null,
|
||||
has_variations: true,
|
||||
terms: [
|
||||
{
|
||||
id: 0,
|
||||
name: 'Yes',
|
||||
slug: 'Yes',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
id: 0,
|
||||
name: 'No',
|
||||
slug: 'No',
|
||||
default: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
describe( 'Testing getVariationAttributes()', () => {
|
||||
it( 'returns empty object if there are no variations', () => {
|
||||
const variationAttributes = getVariationAttributes( null );
|
||||
expect( variationAttributes ).toStrictEqual( {} );
|
||||
} );
|
||||
it( 'returns list of attribute names and value pairs when given valid data', () => {
|
||||
const variationAttributes = getVariationAttributes( rawVariations );
|
||||
expect( variationAttributes ).toStrictEqual( {
|
||||
'id:35': {
|
||||
id: 35,
|
||||
attributes: {
|
||||
Color: 'blue',
|
||||
Logo: 'Yes',
|
||||
},
|
||||
},
|
||||
'id:28': {
|
||||
id: 28,
|
||||
attributes: {
|
||||
Color: 'red',
|
||||
Logo: 'No',
|
||||
},
|
||||
},
|
||||
'id:29': {
|
||||
id: 29,
|
||||
attributes: {
|
||||
Color: 'green',
|
||||
Logo: 'No',
|
||||
},
|
||||
},
|
||||
'id:30': {
|
||||
id: 30,
|
||||
attributes: {
|
||||
Color: 'blue',
|
||||
Logo: 'No',
|
||||
},
|
||||
},
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
describe( 'Testing getVariationsMatchingSelectedAttributes()', () => {
|
||||
const attributes = getAttributes( rawAttributeData );
|
||||
const variationAttributes = getVariationAttributes( rawVariations );
|
||||
|
||||
it( 'returns all variations, in the correct order, if no selections have been made yet', () => {
|
||||
const selectedAttributes = {};
|
||||
const matches = getVariationsMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( matches ).toStrictEqual( [ 35, 28, 29, 30 ] );
|
||||
} );
|
||||
|
||||
it( 'returns correct subset of variations after a selection', () => {
|
||||
const selectedAttributes = {
|
||||
Color: 'blue',
|
||||
};
|
||||
const matches = getVariationsMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( matches ).toStrictEqual( [ 35, 30 ] );
|
||||
} );
|
||||
|
||||
it( 'returns correct subset of variations after all selections', () => {
|
||||
const selectedAttributes = {
|
||||
Color: 'blue',
|
||||
Logo: 'No',
|
||||
};
|
||||
const matches = getVariationsMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( matches ).toStrictEqual( [ 30 ] );
|
||||
} );
|
||||
|
||||
it( 'returns no results if selection does not match or is invalid', () => {
|
||||
const selectedAttributes = {
|
||||
Color: 'brown',
|
||||
};
|
||||
const matches = getVariationsMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( matches ).toStrictEqual( [] );
|
||||
} );
|
||||
} );
|
||||
describe( 'Testing getVariationMatchingSelectedAttributes()', () => {
|
||||
const attributes = getAttributes( rawAttributeData );
|
||||
const variationAttributes = getVariationAttributes( rawVariations );
|
||||
|
||||
it( 'returns first match if no selections have been made yet', () => {
|
||||
const selectedAttributes = {};
|
||||
const matches = getVariationMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( matches ).toStrictEqual( 35 );
|
||||
} );
|
||||
|
||||
it( 'returns first match after single selection', () => {
|
||||
const selectedAttributes = {
|
||||
Color: 'blue',
|
||||
};
|
||||
const matches = getVariationMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( matches ).toStrictEqual( 35 );
|
||||
} );
|
||||
|
||||
it( 'returns correct match after all selections', () => {
|
||||
const selectedAttributes = {
|
||||
Color: 'blue',
|
||||
Logo: 'No',
|
||||
};
|
||||
const matches = getVariationMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( matches ).toStrictEqual( 30 );
|
||||
} );
|
||||
|
||||
it( 'returns no match if invalid', () => {
|
||||
const selectedAttributes = {
|
||||
Color: 'brown',
|
||||
};
|
||||
const matches = getVariationMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( matches ).toStrictEqual( 0 );
|
||||
} );
|
||||
} );
|
||||
describe( 'Testing getActiveSelectControlOptions()', () => {
|
||||
const attributes = getAttributes( rawAttributeData );
|
||||
const variationAttributes = getVariationAttributes( rawVariations );
|
||||
|
||||
it( 'returns all possible options if no selections have been made yet', () => {
|
||||
const selectedAttributes = {};
|
||||
const controlOptions = getActiveSelectControlOptions(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( controlOptions ).toStrictEqual( {
|
||||
Color: [
|
||||
{
|
||||
value: 'blue',
|
||||
label: 'Blue',
|
||||
},
|
||||
{
|
||||
value: 'green',
|
||||
label: 'Green',
|
||||
},
|
||||
{
|
||||
value: 'red',
|
||||
label: 'Red',
|
||||
},
|
||||
],
|
||||
Logo: [
|
||||
{
|
||||
value: 'Yes',
|
||||
label: 'Yes',
|
||||
},
|
||||
{
|
||||
value: 'No',
|
||||
label: 'No',
|
||||
},
|
||||
],
|
||||
} );
|
||||
} );
|
||||
|
||||
it( 'returns only valid options if color is selected', () => {
|
||||
const selectedAttributes = {
|
||||
Color: 'green',
|
||||
};
|
||||
const controlOptions = getActiveSelectControlOptions(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
expect( controlOptions ).toStrictEqual( {
|
||||
Color: [
|
||||
{
|
||||
value: 'blue',
|
||||
label: 'Blue',
|
||||
},
|
||||
{
|
||||
value: 'green',
|
||||
label: 'Green',
|
||||
},
|
||||
{
|
||||
value: 'red',
|
||||
label: 'Red',
|
||||
},
|
||||
],
|
||||
Logo: [
|
||||
{
|
||||
value: 'No',
|
||||
label: 'No',
|
||||
},
|
||||
],
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
describe( 'Testing getDefaultAttributes()', () => {
|
||||
const defaultAttributes = getDefaultAttributes( formattedAttributes );
|
||||
|
||||
it( 'should return default attributes in the format that is ready for setting state', () => {
|
||||
expect( defaultAttributes ).toStrictEqual( {
|
||||
Color: 'blue',
|
||||
Size: 'medium',
|
||||
} );
|
||||
} );
|
||||
|
||||
it( 'should return an empty object if given unexpected values', () => {
|
||||
expect( getDefaultAttributes( [] ) ).toStrictEqual( {} );
|
||||
expect( getDefaultAttributes( null ) ).toStrictEqual( {} );
|
||||
expect( getDefaultAttributes( undefined ) ).toStrictEqual( {} );
|
||||
} );
|
||||
} );
|
||||
} );
|
@ -0,0 +1,240 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { keyBy } from 'lodash';
|
||||
import { decodeEntities } from '@wordpress/html-entities';
|
||||
import { isObject } from '@woocommerce/types';
|
||||
|
||||
/**
|
||||
* Key an array of attributes by name,
|
||||
*
|
||||
* @param {Object} attributes Attributes array.
|
||||
*/
|
||||
export const getAttributes = ( attributes ) => {
|
||||
return attributes
|
||||
? keyBy(
|
||||
Object.values( attributes ).filter(
|
||||
( { has_variations: hasVariations } ) => hasVariations
|
||||
),
|
||||
'name'
|
||||
)
|
||||
: {};
|
||||
};
|
||||
|
||||
/**
|
||||
* Format variations from the API into a map of just the attribute names and values.
|
||||
*
|
||||
* Note, each item is keyed by the variation ID with an id: prefix. This is to prevent the object
|
||||
* being reordered when iterated.
|
||||
*
|
||||
* @param {Object} variations List of Variation objects and attributes keyed by variation ID.
|
||||
*/
|
||||
export const getVariationAttributes = ( variations ) => {
|
||||
if ( ! variations ) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const attributesMap = {};
|
||||
|
||||
variations.forEach( ( { id, attributes } ) => {
|
||||
attributesMap[ `id:${ id }` ] = {
|
||||
id,
|
||||
attributes: attributes.reduce( ( acc, { name, value } ) => {
|
||||
acc[ name ] = value;
|
||||
return acc;
|
||||
}, {} ),
|
||||
};
|
||||
} );
|
||||
|
||||
return attributesMap;
|
||||
};
|
||||
|
||||
/**
|
||||
* Given a list of variations and a list of attribute values, return variations which match.
|
||||
*
|
||||
* Allows an attribute to be excluded by name. This is used to filter displayed options for
|
||||
* individual attribute selects.
|
||||
*
|
||||
* @param {Object} attributes List of attribute names and terms.
|
||||
* @param {Object} variationAttributes Attributes for each variation keyed by variation ID.
|
||||
* @param {Object} selectedAttributes Attribute Name Value pairs of current selections by the user.
|
||||
* @return {Array} List of matching variation IDs.
|
||||
*/
|
||||
export const getVariationsMatchingSelectedAttributes = (
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
) => {
|
||||
const variationIds = Object.values( variationAttributes ).map(
|
||||
( { id: variationId } ) => {
|
||||
return variationId;
|
||||
}
|
||||
);
|
||||
|
||||
// If nothing is selected yet, just return all variations.
|
||||
if (
|
||||
Object.values( selectedAttributes ).every( ( value ) => value === '' )
|
||||
) {
|
||||
return variationIds;
|
||||
}
|
||||
|
||||
const attributeNames = Object.keys( attributes );
|
||||
|
||||
return variationIds.filter( ( variationId ) =>
|
||||
attributeNames.every( ( attributeName ) => {
|
||||
const selectedAttribute = selectedAttributes[ attributeName ] || '';
|
||||
const variationAttribute =
|
||||
variationAttributes[ 'id:' + variationId ].attributes[
|
||||
attributeName
|
||||
];
|
||||
|
||||
// If there is no selected attribute, consider this a match.
|
||||
if ( selectedAttribute === '' ) {
|
||||
return true;
|
||||
}
|
||||
// If the variation attributes for this attribute are set to null, it matches all values.
|
||||
if ( variationAttribute === null ) {
|
||||
return true;
|
||||
}
|
||||
// Otherwise, only match if the selected values are the same.
|
||||
return variationAttribute === selectedAttribute;
|
||||
} )
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Given a list of variations and a list of attribute values, returns the first matched variation ID.
|
||||
*
|
||||
* @param {Object} attributes List of attribute names and terms.
|
||||
* @param {Object} variationAttributes Attributes for each variation keyed by variation ID.
|
||||
* @param {Object} selectedAttributes Attribute Name Value pairs of current selections by the user.
|
||||
* @return {number} Variation ID.
|
||||
*/
|
||||
export const getVariationMatchingSelectedAttributes = (
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
) => {
|
||||
const matchingVariationIds = getVariationsMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
);
|
||||
return matchingVariationIds[ 0 ] || 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Given a list of terms, filter them and return valid options for the select boxes.
|
||||
*
|
||||
* @see getActiveSelectControlOptions
|
||||
* @param {Object} attributeTerms List of attribute term objects.
|
||||
* @param {?Array} validAttributeTerms Valid values if selections have been made already.
|
||||
* @return {Array} Value/Label pairs of select box options.
|
||||
*/
|
||||
const getValidSelectControlOptions = (
|
||||
attributeTerms,
|
||||
validAttributeTerms = null
|
||||
) => {
|
||||
return Object.values( attributeTerms )
|
||||
.map( ( { name, slug } ) => {
|
||||
if (
|
||||
validAttributeTerms === null ||
|
||||
validAttributeTerms.includes( null ) ||
|
||||
validAttributeTerms.includes( slug )
|
||||
) {
|
||||
return {
|
||||
value: slug,
|
||||
label: decodeEntities( name ),
|
||||
};
|
||||
}
|
||||
return null;
|
||||
} )
|
||||
.filter( Boolean );
|
||||
};
|
||||
|
||||
/**
|
||||
* Given a list of terms, filter them and return active options for the select boxes. This factors in
|
||||
* which options should be hidden due to current selections.
|
||||
*
|
||||
* @param {Object} attributes List of attribute names and terms.
|
||||
* @param {Object} variationAttributes Attributes for each variation keyed by variation ID.
|
||||
* @param {Object} selectedAttributes Attribute Name Value pairs of current selections by the user.
|
||||
* @return {Object} Select box options.
|
||||
*/
|
||||
export const getActiveSelectControlOptions = (
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributes
|
||||
) => {
|
||||
const options = {};
|
||||
const attributeNames = Object.keys( attributes );
|
||||
const hasSelectedAttributes =
|
||||
Object.values( selectedAttributes ).filter( Boolean ).length > 0;
|
||||
|
||||
attributeNames.forEach( ( attributeName ) => {
|
||||
const currentAttribute = attributes[ attributeName ];
|
||||
const selectedAttributesExcludingCurrentAttribute = {
|
||||
...selectedAttributes,
|
||||
[ attributeName ]: null,
|
||||
};
|
||||
// This finds matching variations for selected attributes apart from this one. This will be
|
||||
// used to get valid attribute terms of the current attribute narrowed down by those matching
|
||||
// variation IDs. For example, if I had Large Blue Shirts and Medium Red Shirts, I want to only
|
||||
// show Red shirts if Medium is selected.
|
||||
const matchingVariationIds = hasSelectedAttributes
|
||||
? getVariationsMatchingSelectedAttributes(
|
||||
attributes,
|
||||
variationAttributes,
|
||||
selectedAttributesExcludingCurrentAttribute
|
||||
)
|
||||
: null;
|
||||
// Uses the above matching variation IDs to get the attributes from just those variations.
|
||||
const validAttributeTerms =
|
||||
matchingVariationIds !== null
|
||||
? matchingVariationIds.map(
|
||||
( varId ) =>
|
||||
variationAttributes[ 'id:' + varId ].attributes[
|
||||
attributeName
|
||||
]
|
||||
)
|
||||
: null;
|
||||
// Intersects attributes with valid attributes.
|
||||
options[ attributeName ] = getValidSelectControlOptions(
|
||||
currentAttribute.terms,
|
||||
validAttributeTerms
|
||||
);
|
||||
} );
|
||||
|
||||
return options;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return the default values of the given attributes in a format ready to be set in state.
|
||||
*
|
||||
* @param {Object} attributes List of attribute names and terms.
|
||||
* @return {Object} Default attributes.
|
||||
*/
|
||||
export const getDefaultAttributes = ( attributes = {} ) => {
|
||||
if ( ! isObject( attributes ) ) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const attributeNames = Object.keys( attributes );
|
||||
const defaultsToSet = {};
|
||||
|
||||
if ( attributeNames.length === 0 ) {
|
||||
return defaultsToSet;
|
||||
}
|
||||
|
||||
attributeNames.forEach( ( attributeName ) => {
|
||||
const currentAttribute = attributes[ attributeName ];
|
||||
const defaultValue = currentAttribute.terms.filter(
|
||||
( term ) => term.default
|
||||
);
|
||||
if ( defaultValue.length > 0 ) {
|
||||
defaultsToSet[ currentAttribute.name ] = defaultValue[ 0 ]?.slug;
|
||||
}
|
||||
} );
|
||||
|
||||
return defaultsToSet;
|
||||
};
|
@ -0,0 +1,174 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __, _n, sprintf } from '@wordpress/i18n';
|
||||
import Button from '@woocommerce/base-components/button';
|
||||
import { Icon, done as doneIcon } from '@woocommerce/icons';
|
||||
import { useState, useEffect } from '@wordpress/element';
|
||||
import { useAddToCartFormContext } from '@woocommerce/base-context';
|
||||
import {
|
||||
useStoreEvents,
|
||||
useStoreAddToCart,
|
||||
} from '@woocommerce/base-context/hooks';
|
||||
import { useInnerBlockLayoutContext } from '@woocommerce/shared-context';
|
||||
|
||||
/**
|
||||
* Add to Cart Form Button Component.
|
||||
*/
|
||||
const AddToCartButton = () => {
|
||||
const {
|
||||
showFormElements,
|
||||
productIsPurchasable,
|
||||
productHasOptions,
|
||||
product,
|
||||
productType,
|
||||
isDisabled,
|
||||
isProcessing,
|
||||
eventRegistration,
|
||||
hasError,
|
||||
dispatchActions,
|
||||
} = useAddToCartFormContext();
|
||||
const { parentName } = useInnerBlockLayoutContext();
|
||||
const { dispatchStoreEvent } = useStoreEvents();
|
||||
const { cartQuantity } = useStoreAddToCart( product.id || 0 );
|
||||
const [ addedToCart, setAddedToCart ] = useState( false );
|
||||
const addToCartButtonData = product.add_to_cart || {
|
||||
url: '',
|
||||
text: '',
|
||||
};
|
||||
|
||||
// Subscribe to emitter for after processing.
|
||||
useEffect( () => {
|
||||
const onSuccess = () => {
|
||||
if ( ! hasError ) {
|
||||
setAddedToCart( true );
|
||||
}
|
||||
return true;
|
||||
};
|
||||
const unsubscribeProcessing = eventRegistration.onAddToCartAfterProcessingWithSuccess(
|
||||
onSuccess,
|
||||
0
|
||||
);
|
||||
return () => {
|
||||
unsubscribeProcessing();
|
||||
};
|
||||
}, [ eventRegistration, hasError ] );
|
||||
|
||||
/**
|
||||
* We can show a real button if we are:
|
||||
*
|
||||
* a) Showing a full add to cart form.
|
||||
* b) The product doesn't have options and can therefore be added directly to the cart.
|
||||
* c) The product is purchasable.
|
||||
*
|
||||
* Otherwise we show a link instead.
|
||||
*/
|
||||
const showButton =
|
||||
( showFormElements ||
|
||||
( ! productHasOptions && productType === 'simple' ) ) &&
|
||||
productIsPurchasable;
|
||||
|
||||
return showButton ? (
|
||||
<ButtonComponent
|
||||
className="wc-block-components-product-add-to-cart-button"
|
||||
quantityInCart={ cartQuantity }
|
||||
isDisabled={ isDisabled }
|
||||
isProcessing={ isProcessing }
|
||||
isDone={ addedToCart }
|
||||
onClick={ () => {
|
||||
dispatchActions.submitForm();
|
||||
dispatchStoreEvent( 'cart-add-item', {
|
||||
product,
|
||||
listName: parentName,
|
||||
} );
|
||||
} }
|
||||
/>
|
||||
) : (
|
||||
<LinkComponent
|
||||
className="wc-block-components-product-add-to-cart-button"
|
||||
href={ addToCartButtonData.url }
|
||||
text={
|
||||
addToCartButtonData.text ||
|
||||
__( 'View Product', 'woocommerce' )
|
||||
}
|
||||
onClick={ () => {
|
||||
dispatchStoreEvent( 'product-view-link', {
|
||||
product,
|
||||
listName: parentName,
|
||||
} );
|
||||
} }
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Button component for non-purchasable products.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} props.className Css classnames.
|
||||
* @param {string} props.href Link for button.
|
||||
* @param {string} props.text Text content for button.
|
||||
* @param {function():any} props.onClick Callback to execute when button is clicked.
|
||||
*/
|
||||
const LinkComponent = ( { className, href, text, onClick } ) => {
|
||||
return (
|
||||
<Button
|
||||
className={ className }
|
||||
href={ href }
|
||||
onClick={ onClick }
|
||||
rel="nofollow"
|
||||
>
|
||||
{ text }
|
||||
</Button>
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Button for purchasable products.
|
||||
*
|
||||
* @param {Object} props Incoming props for component
|
||||
* @param {string} props.className Incoming css class name.
|
||||
* @param {number} props.quantityInCart Quantity of item in cart.
|
||||
* @param {boolean} props.isProcessing Whether processing action is occurring.
|
||||
* @param {boolean} props.isDisabled Whether the button is disabled or not.
|
||||
* @param {boolean} props.isDone Whether processing is done.
|
||||
* @param {function():any} props.onClick Callback to execute when button is clicked.
|
||||
*/
|
||||
const ButtonComponent = ( {
|
||||
className,
|
||||
quantityInCart,
|
||||
isProcessing,
|
||||
isDisabled,
|
||||
isDone,
|
||||
onClick,
|
||||
} ) => {
|
||||
return (
|
||||
<Button
|
||||
className={ className }
|
||||
disabled={ isDisabled }
|
||||
showSpinner={ isProcessing }
|
||||
onClick={ onClick }
|
||||
>
|
||||
{ isDone && quantityInCart > 0
|
||||
? sprintf(
|
||||
/* translators: %s number of products in cart. */
|
||||
_n(
|
||||
'%d in cart',
|
||||
'%d in cart',
|
||||
quantityInCart,
|
||||
'woocommerce'
|
||||
),
|
||||
quantityInCart
|
||||
)
|
||||
: __( 'Add to cart', 'woocommerce' ) }
|
||||
{ !! isDone && (
|
||||
<Icon
|
||||
srcElement={ doneIcon }
|
||||
alt={ __( 'Done', 'woocommerce' ) }
|
||||
/>
|
||||
) }
|
||||
</Button>
|
||||
);
|
||||
};
|
||||
|
||||
export default AddToCartButton;
|
@ -0,0 +1,3 @@
|
||||
export { default as AddToCartButton } from './add-to-cart-button';
|
||||
export { default as QuantityInput } from './quantity-input';
|
||||
export { default as ProductUnavailable } from './product-unavailable';
|
@ -0,0 +1,19 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
|
||||
const ProductUnavailable = ( {
|
||||
reason = __(
|
||||
'Sorry, this product cannot be purchased.',
|
||||
'woocommerce'
|
||||
),
|
||||
} ) => {
|
||||
return (
|
||||
<div className="wc-block-components-product-add-to-cart-unavailable">
|
||||
{ reason }
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ProductUnavailable;
|
@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Quantity Input Component.
|
||||
*
|
||||
* @param {Object} props Incoming props for component
|
||||
* @param {boolean} props.disabled Whether input is disabled or not.
|
||||
* @param {number} props.min Minimum value for input.
|
||||
* @param {number} props.max Maximum value for input.
|
||||
* @param {number} props.value Value for input.
|
||||
* @param {function():any} props.onChange Function to call on input change event.
|
||||
*/
|
||||
const QuantityInput = ( { disabled, min, max, value, onChange } ) => {
|
||||
return (
|
||||
<input
|
||||
className="wc-block-components-product-add-to-cart-quantity"
|
||||
type="number"
|
||||
value={ value }
|
||||
min={ min }
|
||||
max={ max }
|
||||
hidden={ max === 1 }
|
||||
disabled={ disabled }
|
||||
onChange={ ( e ) => {
|
||||
onChange( e.target.value );
|
||||
} }
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default QuantityInput;
|
@ -0,0 +1,49 @@
|
||||
.wc-block-components-product-add-to-cart {
|
||||
margin: 0;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
|
||||
.wc-block-components-product-add-to-cart-button {
|
||||
margin: 0 0 em($gap-small) 0;
|
||||
|
||||
.wc-block-components-button__text {
|
||||
display: block;
|
||||
|
||||
> svg {
|
||||
fill: currentColor;
|
||||
vertical-align: top;
|
||||
width: 1.5em;
|
||||
height: 1.5em;
|
||||
margin: -0.25em 0 -0.25em 0.5em;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.wc-block-components-product-add-to-cart-quantity {
|
||||
margin: 0 1em em($gap-small) 0;
|
||||
flex-basis: 5em;
|
||||
padding: 0.618em;
|
||||
background: $white;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 2px;
|
||||
color: #43454b;
|
||||
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.125);
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
|
||||
.is-loading .wc-block-components-product-add-to-cart,
|
||||
.wc-block-components-product-add-to-cart--placeholder {
|
||||
.wc-block-components-product-add-to-cart-quantity,
|
||||
.wc-block-components-product-add-to-cart-button {
|
||||
@include placeholder();
|
||||
}
|
||||
}
|
||||
|
||||
.wc-block-grid .wc-block-components-product-add-to-cart {
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.wc-block-components-product-add-to-cart-notice {
|
||||
margin: 0;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
export const blockAttributes = {
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,145 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import PropTypes from 'prop-types';
|
||||
import classnames from 'classnames';
|
||||
import { __, _n, sprintf } from '@wordpress/i18n';
|
||||
import {
|
||||
useStoreEvents,
|
||||
useStoreAddToCart,
|
||||
} from '@woocommerce/base-context/hooks';
|
||||
import { decodeEntities } from '@wordpress/html-entities';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product Button Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( { className } ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
'wp-block-button',
|
||||
'wc-block-components-product-button',
|
||||
{
|
||||
[ `${ parentClassName }__product-add-to-cart` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
>
|
||||
{ product.id ? (
|
||||
<AddToCartButton product={ product } />
|
||||
) : (
|
||||
<AddToCartButtonPlaceholder />
|
||||
) }
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const AddToCartButton = ( { product } ) => {
|
||||
const {
|
||||
id,
|
||||
permalink,
|
||||
add_to_cart: productCartDetails,
|
||||
has_options: hasOptions,
|
||||
is_purchasable: isPurchasable,
|
||||
is_in_stock: isInStock,
|
||||
} = product;
|
||||
const { dispatchStoreEvent } = useStoreEvents();
|
||||
const { cartQuantity, addingToCart, addToCart } = useStoreAddToCart( id );
|
||||
|
||||
const addedToCart = Number.isFinite( cartQuantity ) && cartQuantity > 0;
|
||||
const allowAddToCart = ! hasOptions && isPurchasable && isInStock;
|
||||
const buttonAriaLabel = decodeEntities(
|
||||
productCartDetails?.description || ''
|
||||
);
|
||||
const buttonText = addedToCart
|
||||
? sprintf(
|
||||
/* translators: %s number of products in cart. */
|
||||
_n(
|
||||
'%d in cart',
|
||||
'%d in cart',
|
||||
cartQuantity,
|
||||
'woocommerce'
|
||||
),
|
||||
cartQuantity
|
||||
)
|
||||
: decodeEntities(
|
||||
productCartDetails?.text ||
|
||||
__( 'Add to cart', 'woocommerce' )
|
||||
);
|
||||
|
||||
const ButtonTag = allowAddToCart ? 'button' : 'a';
|
||||
const buttonProps = {};
|
||||
|
||||
if ( ! allowAddToCart ) {
|
||||
buttonProps.href = permalink;
|
||||
buttonProps.rel = 'nofollow';
|
||||
buttonProps.onClick = () => {
|
||||
dispatchStoreEvent( 'product-view-link', {
|
||||
product,
|
||||
} );
|
||||
};
|
||||
} else {
|
||||
buttonProps.onClick = () => {
|
||||
addToCart();
|
||||
dispatchStoreEvent( 'cart-add-item', {
|
||||
product,
|
||||
} );
|
||||
};
|
||||
}
|
||||
|
||||
return (
|
||||
<ButtonTag
|
||||
aria-label={ buttonAriaLabel }
|
||||
className={ classnames(
|
||||
'wp-block-button__link',
|
||||
'add_to_cart_button',
|
||||
'wc-block-components-product-button__button',
|
||||
{
|
||||
loading: addingToCart,
|
||||
added: addedToCart,
|
||||
}
|
||||
) }
|
||||
disabled={ addingToCart }
|
||||
{ ...buttonProps }
|
||||
>
|
||||
{ buttonText }
|
||||
</ButtonTag>
|
||||
);
|
||||
};
|
||||
|
||||
const AddToCartButtonPlaceholder = () => {
|
||||
return (
|
||||
<button
|
||||
className={ classnames(
|
||||
'wp-block-button__link',
|
||||
'add_to_cart_button',
|
||||
'wc-block-components-product-button__button',
|
||||
'wc-block-components-product-button__button--placeholder'
|
||||
) }
|
||||
disabled={ true }
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { cart, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'Add to Cart Button',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ cart } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display a call to action button which either adds the product to the cart, or links to the product page.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,29 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { Disabled } from '@wordpress/components';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes } ) => {
|
||||
return (
|
||||
<Disabled>
|
||||
<Block { ...attributes } />
|
||||
</Disabled>
|
||||
);
|
||||
};
|
||||
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its add to cart button.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerBlockType } from '@wordpress/blocks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import attributes from './attributes';
|
||||
import edit from './edit';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerBlockType( 'woocommerce/product-button', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,36 @@
|
||||
.wp-block-button.wc-block-components-product-button {
|
||||
word-break: break-word;
|
||||
white-space: normal;
|
||||
margin-top: 0;
|
||||
margin-bottom: $gap-small;
|
||||
|
||||
.wc-block-components-product-button__button {
|
||||
word-break: break-word;
|
||||
white-space: normal;
|
||||
margin: 0 auto;
|
||||
display: inline-flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.wc-block-components-product-button__button--placeholder {
|
||||
@include placeholder();
|
||||
min-width: 8em;
|
||||
min-height: 3em;
|
||||
}
|
||||
}
|
||||
|
||||
.is-loading .wc-block-components-product-button > .wc-block-components-product-button__button {
|
||||
@include placeholder();
|
||||
min-width: 8em;
|
||||
min-height: 3em;
|
||||
}
|
||||
|
||||
|
||||
.theme-twentytwentyone {
|
||||
// Prevent buttons appearing disabled in the editor.
|
||||
.editor-styles-wrapper .wc-block-components-product-button .wp-block-button__link {
|
||||
background-color: var(--button--color-background);
|
||||
color: var(--button--color-text);
|
||||
border-color: var(--button--color-background);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
export const blockAttributes = {
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import PropTypes from 'prop-types';
|
||||
import classnames from 'classnames';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { isEmpty } from 'lodash';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product Category Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( { className } ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
|
||||
if ( isEmpty( product.categories ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-category-list',
|
||||
{
|
||||
[ `${ parentClassName }__product-category-list` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
>
|
||||
{ __( 'Categories:', 'woocommerce' ) }{ ' ' }
|
||||
<ul>
|
||||
{ Object.values( product.categories ).map(
|
||||
( { name, link, slug } ) => {
|
||||
return (
|
||||
<li key={ `category-list-item-${ slug }` }>
|
||||
<a href={ link }>{ name }</a>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
) }
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { folder, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'Product Category List',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ folder } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display a list of categories belonging to a product.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { Disabled } from '@wordpress/components';
|
||||
import EditProductLink from '@woocommerce/editor-components/edit-product-link';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes } ) => {
|
||||
return (
|
||||
<>
|
||||
<EditProductLink />
|
||||
<Disabled>
|
||||
<Block { ...attributes } />
|
||||
</Disabled>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its categories.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerExperimentalBlockType } from '@woocommerce/block-settings';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import attributes from './attributes';
|
||||
import edit from './edit';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerExperimentalBlockType( 'woocommerce/product-category-list', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,23 @@
|
||||
.wc-block-components-product-category-list {
|
||||
margin-top: 0;
|
||||
margin-bottom: em($gap-small);
|
||||
|
||||
ul {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: inline;
|
||||
|
||||
li {
|
||||
display: inline;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
li::after {
|
||||
content: ", ";
|
||||
}
|
||||
|
||||
li:last-child::after {
|
||||
content: "";
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
export const blockAttributes = {
|
||||
showProductLink: {
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
},
|
||||
showSaleBadge: {
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
},
|
||||
saleBadgeAlign: {
|
||||
type: 'string',
|
||||
default: 'right',
|
||||
},
|
||||
imageSizing: {
|
||||
type: 'string',
|
||||
default: 'full-size',
|
||||
},
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,144 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import PropTypes from 'prop-types';
|
||||
import { useState, Fragment } from '@wordpress/element';
|
||||
import { __, sprintf } from '@wordpress/i18n';
|
||||
import classnames from 'classnames';
|
||||
import { PLACEHOLDER_IMG_SRC } from '@woocommerce/settings';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
import { useStoreEvents } from '@woocommerce/base-context/hooks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import ProductSaleBadge from './../sale-badge/block';
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product Image Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @param {string} [props.imageSizing] Size of image to use.
|
||||
* @param {boolean} [props.showProductLink] Whether or not to display a link to the product page.
|
||||
* @param {boolean} [props.showSaleBadge] Whether or not to display the on sale badge.
|
||||
* @param {string} [props.saleBadgeAlign] How should the sale badge be aligned if displayed.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
export const Block = ( {
|
||||
className,
|
||||
imageSizing = 'full-size',
|
||||
showProductLink = true,
|
||||
showSaleBadge,
|
||||
saleBadgeAlign = 'right',
|
||||
} ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
const [ imageLoaded, setImageLoaded ] = useState( false );
|
||||
const { dispatchStoreEvent } = useStoreEvents();
|
||||
|
||||
if ( ! product.id ) {
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-image',
|
||||
'wc-block-components-product-image--placeholder',
|
||||
{
|
||||
[ `${ parentClassName }__product-image` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
>
|
||||
<ImagePlaceholder />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
const hasProductImages = !! product.images.length;
|
||||
const image = hasProductImages ? product.images[ 0 ] : null;
|
||||
const ParentComponent = showProductLink ? 'a' : Fragment;
|
||||
const anchorLabel = sprintf(
|
||||
/* translators: %s is referring to the product name */
|
||||
__( 'Link to %s', 'woocommerce' ),
|
||||
product.name
|
||||
);
|
||||
const anchorProps = {
|
||||
href: product.permalink,
|
||||
rel: 'nofollow',
|
||||
...( ! hasProductImages && { 'aria-label': anchorLabel } ),
|
||||
onClick: () => {
|
||||
dispatchStoreEvent( 'product-view-link', {
|
||||
product,
|
||||
} );
|
||||
},
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-image',
|
||||
{
|
||||
[ `${ parentClassName }__product-image` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
>
|
||||
<ParentComponent { ...( showProductLink && anchorProps ) }>
|
||||
{ !! showSaleBadge && (
|
||||
<ProductSaleBadge
|
||||
align={ saleBadgeAlign }
|
||||
product={ product }
|
||||
/>
|
||||
) }
|
||||
<Image
|
||||
fallbackAlt={ product.name }
|
||||
image={ image }
|
||||
onLoad={ () => setImageLoaded( true ) }
|
||||
loaded={ imageLoaded }
|
||||
showFullSize={ imageSizing !== 'cropped' }
|
||||
/>
|
||||
</ParentComponent>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const ImagePlaceholder = () => {
|
||||
return (
|
||||
<img src={ PLACEHOLDER_IMG_SRC } alt="" width={ 500 } height={ 500 } />
|
||||
);
|
||||
};
|
||||
|
||||
const Image = ( { image, onLoad, loaded, showFullSize, fallbackAlt } ) => {
|
||||
const { thumbnail, src, srcset, sizes, alt } = image || {};
|
||||
const imageProps = {
|
||||
alt: alt || fallbackAlt,
|
||||
onLoad,
|
||||
hidden: ! loaded,
|
||||
src: thumbnail,
|
||||
...( showFullSize && { src, srcSet: srcset, sizes } ),
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{ imageProps.src && (
|
||||
/* eslint-disable-next-line jsx-a11y/alt-text */
|
||||
<img data-testid="product-image" { ...imageProps } />
|
||||
) }
|
||||
{ ! loaded && <ImagePlaceholder /> }
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
fallbackAlt: PropTypes.string,
|
||||
showProductLink: PropTypes.bool,
|
||||
showSaleBadge: PropTypes.bool,
|
||||
saleBadgeAlign: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { image, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'Product Image',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ image } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display the main product image',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,159 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { Disabled, PanelBody, ToggleControl } from '@wordpress/components';
|
||||
import { InspectorControls } from '@wordpress/block-editor';
|
||||
import { createInterpolateElement } from '@wordpress/element';
|
||||
import ToggleButtonControl from '@woocommerce/editor-components/toggle-button-control';
|
||||
import { getAdminLink } from '@woocommerce/settings';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes, setAttributes } ) => {
|
||||
const {
|
||||
showProductLink,
|
||||
imageSizing,
|
||||
showSaleBadge,
|
||||
saleBadgeAlign,
|
||||
} = attributes;
|
||||
|
||||
return (
|
||||
<>
|
||||
<InspectorControls>
|
||||
<PanelBody
|
||||
title={ __( 'Content', 'woocommerce' ) }
|
||||
>
|
||||
<ToggleControl
|
||||
label={ __(
|
||||
'Link to Product Page',
|
||||
'woocommerce'
|
||||
) }
|
||||
help={ __(
|
||||
'Links the image to the single product listing.',
|
||||
'woocommerce'
|
||||
) }
|
||||
checked={ showProductLink }
|
||||
onChange={ () =>
|
||||
setAttributes( {
|
||||
showProductLink: ! showProductLink,
|
||||
} )
|
||||
}
|
||||
/>
|
||||
<ToggleControl
|
||||
label={ __(
|
||||
'Show On-Sale Badge',
|
||||
'woocommerce'
|
||||
) }
|
||||
help={ __(
|
||||
'Overlay a "sale" badge if the product is on-sale.',
|
||||
'woocommerce'
|
||||
) }
|
||||
checked={ showSaleBadge }
|
||||
onChange={ () =>
|
||||
setAttributes( {
|
||||
showSaleBadge: ! showSaleBadge,
|
||||
} )
|
||||
}
|
||||
/>
|
||||
{ showSaleBadge && (
|
||||
<ToggleButtonControl
|
||||
label={ __(
|
||||
'Sale Badge Alignment',
|
||||
'woocommerce'
|
||||
) }
|
||||
value={ saleBadgeAlign }
|
||||
options={ [
|
||||
{
|
||||
label: __(
|
||||
'Left',
|
||||
'woocommerce'
|
||||
),
|
||||
value: 'left',
|
||||
},
|
||||
{
|
||||
label: __(
|
||||
'Center',
|
||||
'woocommerce'
|
||||
),
|
||||
value: 'center',
|
||||
},
|
||||
{
|
||||
label: __(
|
||||
'Right',
|
||||
'woocommerce'
|
||||
),
|
||||
value: 'right',
|
||||
},
|
||||
] }
|
||||
onChange={ ( value ) =>
|
||||
setAttributes( { saleBadgeAlign: value } )
|
||||
}
|
||||
/>
|
||||
) }
|
||||
<ToggleButtonControl
|
||||
label={ __(
|
||||
'Image Sizing',
|
||||
'woocommerce'
|
||||
) }
|
||||
help={ createInterpolateElement(
|
||||
__(
|
||||
'Product image cropping can be modified in the <a>Customizer</a>.',
|
||||
'woocommerce'
|
||||
),
|
||||
{
|
||||
a: (
|
||||
// eslint-disable-next-line jsx-a11y/anchor-has-content
|
||||
<a
|
||||
href={ `${ getAdminLink(
|
||||
'customize.php'
|
||||
) }?autofocus[panel]=woocommerce&autofocus[section]=woocommerce_product_images` }
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
/>
|
||||
),
|
||||
}
|
||||
) }
|
||||
value={ imageSizing }
|
||||
options={ [
|
||||
{
|
||||
label: __(
|
||||
'Full Size',
|
||||
'woocommerce'
|
||||
),
|
||||
value: 'full-size',
|
||||
},
|
||||
{
|
||||
label: __(
|
||||
'Cropped',
|
||||
'woocommerce'
|
||||
),
|
||||
value: 'cropped',
|
||||
},
|
||||
] }
|
||||
onChange={ ( value ) =>
|
||||
setAttributes( { imageSizing: value } )
|
||||
}
|
||||
/>
|
||||
</PanelBody>
|
||||
</InspectorControls>
|
||||
<Disabled>
|
||||
<Block { ...attributes } />
|
||||
</Disabled>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its image.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import withFilteredAttributes from '@woocommerce/base-hocs/with-filtered-attributes';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import attributes from './attributes';
|
||||
|
||||
export default withFilteredAttributes( attributes )( Block );
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerBlockType } from '@wordpress/blocks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import attributes from './attributes';
|
||||
import edit from './edit';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerBlockType( 'woocommerce/product-image', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,53 @@
|
||||
.editor-styles-wrapper .wc-block-grid__products .wc-block-grid__product .wc-block-components-product-image,
|
||||
.wc-block-components-product-image {
|
||||
margin-top: 0;
|
||||
margin-bottom: $gap-small;
|
||||
text-decoration: none;
|
||||
display: block;
|
||||
position: relative;
|
||||
|
||||
a {
|
||||
text-decoration: none;
|
||||
border: 0;
|
||||
outline: 0;
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
img {
|
||||
vertical-align: middle;
|
||||
width: 100%;
|
||||
|
||||
&[hidden] {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.wc-block-components-product-sale-badge {
|
||||
&--align-left {
|
||||
position: absolute;
|
||||
left: $gap-smaller*0.5;
|
||||
top: $gap-smaller*0.5;
|
||||
right: auto;
|
||||
margin: 0;
|
||||
}
|
||||
&--align-center {
|
||||
position: absolute;
|
||||
top: $gap-smaller*0.5;
|
||||
left: 50%;
|
||||
right: auto;
|
||||
transform: translateX(-50%);
|
||||
margin: 0;
|
||||
}
|
||||
&--align-right {
|
||||
position: absolute;
|
||||
right: $gap-smaller*0.5;
|
||||
top: $gap-smaller*0.5;
|
||||
left: auto;
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.is-loading .wc-block-components-product-image {
|
||||
@include placeholder();
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { render, fireEvent } from '@testing-library/react';
|
||||
import { ProductDataContextProvider } from '@woocommerce/shared-context';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { Block } from '../block';
|
||||
|
||||
jest.mock( '@woocommerce/block-settings', () => ( {
|
||||
__esModule: true,
|
||||
PLACEHOLDER_IMG_SRC: 'placeholder.jpg',
|
||||
} ) );
|
||||
|
||||
const productWithoutImages = {
|
||||
name: 'Test product',
|
||||
id: 1,
|
||||
fallbackAlt: 'Test product',
|
||||
permalink: 'http://test.com/product/test-product/',
|
||||
images: [],
|
||||
};
|
||||
|
||||
const productWithImages = {
|
||||
name: 'Test product',
|
||||
id: 1,
|
||||
fallbackAlt: 'Test product',
|
||||
permalink: 'http://test.com/product/test-product/',
|
||||
images: [
|
||||
{
|
||||
id: 56,
|
||||
src: 'logo-1.jpg',
|
||||
thumbnail: 'logo-1-324x324.jpg',
|
||||
srcset:
|
||||
'logo-1.jpg 800w, logo-1-300x300.jpg 300w, logo-1-150x150.jpg 150w, logo-1-768x767.jpg 768w, logo-1-324x324.jpg 324w, logo-1-416x415.jpg 416w, logo-1-100x100.jpg 100w',
|
||||
sizes: '(max-width: 800px) 100vw, 800px',
|
||||
name: 'logo-1.jpg',
|
||||
alt: '',
|
||||
},
|
||||
{
|
||||
id: 55,
|
||||
src: 'beanie-with-logo-1.jpg',
|
||||
thumbnail: 'beanie-with-logo-1-324x324.jpg',
|
||||
srcset:
|
||||
'beanie-with-logo-1.jpg 800w, beanie-with-logo-1-300x300.jpg 300w, beanie-with-logo-1-150x150.jpg 150w, beanie-with-logo-1-768x768.jpg 768w, beanie-with-logo-1-324x324.jpg 324w, beanie-with-logo-1-416x416.jpg 416w, beanie-with-logo-1-100x100.jpg 100w',
|
||||
sizes: '(max-width: 800px) 100vw, 800px',
|
||||
name: 'beanie-with-logo-1.jpg',
|
||||
alt: '',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
describe( 'Product Image Block', () => {
|
||||
describe( 'with product link', () => {
|
||||
test( 'should render an anchor with the product image', () => {
|
||||
const component = render(
|
||||
<ProductDataContextProvider product={ productWithImages }>
|
||||
<Block showProductLink={ true } />
|
||||
</ProductDataContextProvider>
|
||||
);
|
||||
|
||||
// use testId as alt is added after image is loaded
|
||||
const image = component.getByTestId( 'product-image' );
|
||||
fireEvent.load( image );
|
||||
|
||||
const productImage = component.getByAltText(
|
||||
productWithImages.name
|
||||
);
|
||||
expect( productImage.getAttribute( 'src' ) ).toBe(
|
||||
productWithImages.images[ 0 ].src
|
||||
);
|
||||
|
||||
const anchor = productImage.closest( 'a' );
|
||||
expect( anchor.getAttribute( 'href' ) ).toBe(
|
||||
productWithImages.permalink
|
||||
);
|
||||
} );
|
||||
|
||||
test( 'should render an anchor with the placeholder image', () => {
|
||||
const component = render(
|
||||
<ProductDataContextProvider product={ productWithoutImages }>
|
||||
<Block showProductLink={ true } />
|
||||
</ProductDataContextProvider>
|
||||
);
|
||||
|
||||
const placeholderImage = component.getByAltText( '' );
|
||||
expect( placeholderImage.getAttribute( 'src' ) ).toBe(
|
||||
'placeholder.jpg'
|
||||
);
|
||||
|
||||
const anchor = placeholderImage.closest( 'a' );
|
||||
expect( anchor.getAttribute( 'href' ) ).toBe(
|
||||
productWithoutImages.permalink
|
||||
);
|
||||
expect( anchor.getAttribute( 'aria-label' ) ).toBe(
|
||||
`Link to ${ productWithoutImages.name }`
|
||||
);
|
||||
} );
|
||||
} );
|
||||
|
||||
describe( 'without product link', () => {
|
||||
test( 'should render the product image without an anchor wrapper', () => {
|
||||
const component = render(
|
||||
<ProductDataContextProvider product={ productWithImages }>
|
||||
<Block showProductLink={ false } />
|
||||
</ProductDataContextProvider>
|
||||
);
|
||||
const image = component.getByTestId( 'product-image' );
|
||||
fireEvent.load( image );
|
||||
|
||||
const productImage = component.getByAltText(
|
||||
productWithImages.name
|
||||
);
|
||||
expect( productImage.getAttribute( 'src' ) ).toBe(
|
||||
productWithImages.images[ 0 ].src
|
||||
);
|
||||
|
||||
const anchor = productImage.closest( 'a' );
|
||||
expect( anchor ).toBe( null );
|
||||
} );
|
||||
|
||||
test( 'should render the placeholder image without an anchor wrapper', () => {
|
||||
const component = render(
|
||||
<ProductDataContextProvider product={ productWithoutImages }>
|
||||
<Block showProductLink={ false } />
|
||||
</ProductDataContextProvider>
|
||||
);
|
||||
|
||||
const placeholderImage = component.getByAltText( '' );
|
||||
expect( placeholderImage.getAttribute( 'src' ) ).toBe(
|
||||
'placeholder.jpg'
|
||||
);
|
||||
|
||||
const anchor = placeholderImage.closest( 'a' );
|
||||
expect( anchor ).toBe( null );
|
||||
} );
|
||||
} );
|
||||
} );
|
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
|
||||
|
||||
let blockAttributes = {
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
if ( isFeaturePluginBuild() ) {
|
||||
blockAttributes = {
|
||||
...blockAttributes,
|
||||
align: {
|
||||
type: 'string',
|
||||
},
|
||||
fontSize: {
|
||||
type: 'string',
|
||||
},
|
||||
customFontSize: {
|
||||
type: 'number',
|
||||
},
|
||||
saleFontSize: {
|
||||
type: 'string',
|
||||
},
|
||||
customSaleFontSize: {
|
||||
type: 'number',
|
||||
},
|
||||
color: {
|
||||
type: 'string',
|
||||
},
|
||||
saleColor: {
|
||||
type: 'string',
|
||||
},
|
||||
customColor: {
|
||||
type: 'string',
|
||||
},
|
||||
customSaleColor: {
|
||||
type: 'string',
|
||||
},
|
||||
};
|
||||
}
|
||||
export default blockAttributes;
|
@ -0,0 +1,135 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import PropTypes from 'prop-types';
|
||||
import classnames from 'classnames';
|
||||
import ProductPrice from '@woocommerce/base-components/product-price';
|
||||
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { getColorClassName, getFontSizeClass } from '@wordpress/block-editor';
|
||||
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Product Price Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @param {string} [props.align] Text alignment.
|
||||
* @param {string} [props.fontSize] Normal Price font size name.
|
||||
* @param {number} [props.customFontSize] Normal Price custom font size.
|
||||
* @param {string} [props.saleFontSize] Original Price font size name.
|
||||
* @param {number} [props.customSaleFontSize] Original Price custom font size.
|
||||
* @param {string} [props.color] Normal Price text color.
|
||||
* @param {string} [props.customColor] Normal Price custom text color.
|
||||
* @param {string} [props.saleColor] Original Price text color.
|
||||
* @param {string} [props.customSaleColor] Original Price custom text color.
|
||||
* context will be used if this is not provided.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( {
|
||||
className,
|
||||
align,
|
||||
fontSize,
|
||||
customFontSize,
|
||||
saleFontSize,
|
||||
customSaleFontSize,
|
||||
color,
|
||||
customColor,
|
||||
saleColor,
|
||||
customSaleColor,
|
||||
} ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
|
||||
const wrapperClassName = classnames( className, {
|
||||
[ `${ parentClassName }__product-price` ]: parentClassName,
|
||||
} );
|
||||
|
||||
if ( ! product.id ) {
|
||||
return <ProductPrice align={ align } className={ wrapperClassName } />;
|
||||
}
|
||||
|
||||
const colorClass = getColorClassName( 'color', color );
|
||||
const fontSizeClass = getFontSizeClass( fontSize );
|
||||
const saleColorClass = getColorClassName( 'color', saleColor );
|
||||
const saleFontSizeClass = getFontSizeClass( saleFontSize );
|
||||
|
||||
const classes = classnames( {
|
||||
'has-text-color': color || customColor,
|
||||
'has-font-size': fontSize || customFontSize,
|
||||
[ colorClass ]: colorClass,
|
||||
[ fontSizeClass ]: fontSizeClass,
|
||||
} );
|
||||
|
||||
const saleClasses = classnames( {
|
||||
'has-text-color': saleColor || customSaleColor,
|
||||
'has-font-size': saleFontSize || customSaleFontSize,
|
||||
[ saleColorClass ]: saleColorClass,
|
||||
[ saleFontSizeClass ]: saleFontSizeClass,
|
||||
} );
|
||||
|
||||
const style = {
|
||||
color: customColor,
|
||||
fontSize: customFontSize,
|
||||
};
|
||||
|
||||
const saleStyle = {
|
||||
color: customSaleColor,
|
||||
fontSize: customSaleFontSize,
|
||||
};
|
||||
|
||||
const prices = product.prices;
|
||||
const currency = getCurrencyFromPriceResponse( prices );
|
||||
const isOnSale = prices.price !== prices.regular_price;
|
||||
const priceClassName = isOnSale
|
||||
? classnames( {
|
||||
[ `${ parentClassName }__product-price__value` ]: parentClassName,
|
||||
[ saleClasses ]: isFeaturePluginBuild(),
|
||||
} )
|
||||
: classnames( {
|
||||
[ `${ parentClassName }__product-price__value` ]: parentClassName,
|
||||
[ classes ]: isFeaturePluginBuild(),
|
||||
} );
|
||||
const priceStyle = isOnSale ? saleStyle : style;
|
||||
|
||||
return (
|
||||
<ProductPrice
|
||||
align={ align }
|
||||
className={ wrapperClassName }
|
||||
currency={ currency }
|
||||
price={ prices.price }
|
||||
priceClassName={ priceClassName }
|
||||
priceStyle={ isFeaturePluginBuild() ? priceStyle : {} }
|
||||
// Range price props
|
||||
minPrice={ prices?.price_range?.min_amount }
|
||||
maxPrice={ prices?.price_range?.max_amount }
|
||||
// This is the regular or original price when the `price` value is a sale price.
|
||||
regularPrice={ prices.regular_price }
|
||||
regularPriceClassName={ classnames( {
|
||||
[ `${ parentClassName }__product-price__regular` ]: parentClassName,
|
||||
[ classes ]: isFeaturePluginBuild(),
|
||||
} ) }
|
||||
regularPriceStyle={ isFeaturePluginBuild() ? style : {} }
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
product: PropTypes.object,
|
||||
align: PropTypes.string,
|
||||
fontSize: PropTypes.string,
|
||||
customFontSize: PropTypes.number,
|
||||
saleFontSize: PropTypes.string,
|
||||
customSaleFontSize: PropTypes.number,
|
||||
color: PropTypes.string,
|
||||
customColor: PropTypes.string,
|
||||
saleColor: PropTypes.string,
|
||||
customSaleColor: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { bill, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'Product Price',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ bill } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display the price of a product.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,133 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { PanelBody, BaseControl } from '@wordpress/components';
|
||||
import { compose } from '@wordpress/compose';
|
||||
import {
|
||||
InspectorControls,
|
||||
BlockControls,
|
||||
AlignmentToolbar,
|
||||
withColors,
|
||||
ColorPalette,
|
||||
FontSizePicker,
|
||||
withFontSizes,
|
||||
} from '@wordpress/block-editor';
|
||||
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const TextControl = ( {
|
||||
fontSize,
|
||||
setFontSize,
|
||||
color,
|
||||
setColor,
|
||||
colorLabel,
|
||||
} ) => (
|
||||
<>
|
||||
<FontSizePicker value={ fontSize.size } onChange={ setFontSize } />
|
||||
{ /* ColorPalette doesn't accept an id. */
|
||||
/* eslint-disable-next-line @wordpress/no-base-control-with-label-without-id */ }
|
||||
<BaseControl label={ colorLabel }>
|
||||
<ColorPalette
|
||||
value={ color.color }
|
||||
onChange={ setColor }
|
||||
label={ __( 'Color' ) }
|
||||
/>
|
||||
</BaseControl>
|
||||
</>
|
||||
);
|
||||
const PriceEdit = ( {
|
||||
fontSize,
|
||||
saleFontSize,
|
||||
setFontSize,
|
||||
setSaleFontSize,
|
||||
color,
|
||||
saleColor,
|
||||
setColor,
|
||||
setSaleColor,
|
||||
attributes,
|
||||
setAttributes,
|
||||
} ) => {
|
||||
const { align } = attributes;
|
||||
return (
|
||||
<>
|
||||
{ isFeaturePluginBuild() && (
|
||||
<BlockControls>
|
||||
<AlignmentToolbar
|
||||
value={ align }
|
||||
onChange={ ( nextAlign ) => {
|
||||
setAttributes( { align: nextAlign } );
|
||||
} }
|
||||
/>
|
||||
</BlockControls>
|
||||
) }
|
||||
<InspectorControls>
|
||||
{ isFeaturePluginBuild() && (
|
||||
<>
|
||||
<PanelBody
|
||||
title={ __(
|
||||
'Price',
|
||||
'woocommerce'
|
||||
) }
|
||||
>
|
||||
<TextControl
|
||||
color={ color }
|
||||
setColor={ setColor }
|
||||
fontSize={ fontSize }
|
||||
setFontSize={ setFontSize }
|
||||
colorLabel={ __(
|
||||
'Color',
|
||||
'woocommerce'
|
||||
) }
|
||||
/>
|
||||
</PanelBody>
|
||||
<PanelBody
|
||||
title={ __(
|
||||
'Sale price',
|
||||
'woocommerce'
|
||||
) }
|
||||
>
|
||||
<TextControl
|
||||
color={ saleColor }
|
||||
setColor={ setSaleColor }
|
||||
fontSize={ saleFontSize }
|
||||
setFontSize={ setSaleFontSize }
|
||||
colorLabel={ __(
|
||||
'Color',
|
||||
'woocommerce'
|
||||
) }
|
||||
/>
|
||||
</PanelBody>
|
||||
</>
|
||||
) }
|
||||
</InspectorControls>
|
||||
<Block { ...attributes } />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
const Price = isFeaturePluginBuild()
|
||||
? compose( [
|
||||
withFontSizes( 'fontSize' ),
|
||||
withFontSizes( 'saleFontSize' ),
|
||||
withFontSizes( 'originalFontSize' ),
|
||||
withColors( 'color', { textColor: 'color' } ),
|
||||
withColors( 'saleColor', { textColor: 'saleColor' } ),
|
||||
withColors( 'originalColor', { textColor: 'originalColor' } ),
|
||||
withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its price.',
|
||||
'woocommerce'
|
||||
),
|
||||
} ),
|
||||
] )( PriceEdit )
|
||||
: PriceEdit;
|
||||
|
||||
export default Price;
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerBlockType } from '@wordpress/blocks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import edit from './edit';
|
||||
import attributes from './attributes';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerBlockType( 'woocommerce/product-price', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,8 @@
|
||||
export const blockAttributes = {
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,78 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import PropTypes from 'prop-types';
|
||||
import { __, sprintf } from '@wordpress/i18n';
|
||||
import classnames from 'classnames';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product Rating Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( { className } ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
const rating = getAverageRating( product );
|
||||
|
||||
if ( ! rating ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const starStyle = {
|
||||
width: ( rating / 5 ) * 100 + '%',
|
||||
};
|
||||
|
||||
const ratingText = sprintf(
|
||||
/* translators: %f is referring to the average rating value */
|
||||
__( 'Rated %f out of 5', 'woocommerce' ),
|
||||
rating
|
||||
);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-rating',
|
||||
{
|
||||
[ `${ parentClassName }__product-rating` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
>
|
||||
<div
|
||||
className={ classnames(
|
||||
'wc-block-components-product-rating__stars',
|
||||
`${ parentClassName }__product-rating__stars`
|
||||
) }
|
||||
role="img"
|
||||
aria-label={ ratingText }
|
||||
>
|
||||
<span style={ starStyle }>{ ratingText }</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const getAverageRating = ( product ) => {
|
||||
const rating = parseFloat( product.average_rating );
|
||||
|
||||
return Number.isFinite( rating ) && rating > 0 ? rating : 0;
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { star, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'Product Rating',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ star } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display the average rating of a product.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes } ) => {
|
||||
return <Block { ...attributes } />;
|
||||
};
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its rating.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerBlockType } from '@wordpress/blocks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import attributes from './attributes';
|
||||
import edit from './edit';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerBlockType( 'woocommerce/product-rating', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,52 @@
|
||||
.wc-block-components-product-rating {
|
||||
display: block;
|
||||
margin-top: 0;
|
||||
margin-bottom: $gap-small;
|
||||
|
||||
&__stars {
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
width: 5.3em;
|
||||
height: 1.618em;
|
||||
line-height: 1.618;
|
||||
font-size: 1em;
|
||||
/* stylelint-disable-next-line font-family-no-missing-generic-family-keyword */
|
||||
font-family: star;
|
||||
font-weight: 400;
|
||||
margin: 0 auto;
|
||||
text-align: left;
|
||||
|
||||
&::before {
|
||||
content: "\53\53\53\53\53";
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
position: absolute;
|
||||
opacity: 0.5;
|
||||
color: #aaa;
|
||||
white-space: nowrap;
|
||||
}
|
||||
span {
|
||||
overflow: hidden;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
position: absolute;
|
||||
padding-top: 1.5em;
|
||||
}
|
||||
span::before {
|
||||
content: "\53\53\53\53\53";
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
position: absolute;
|
||||
color: #000;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
}
|
||||
.wc-block-single-product {
|
||||
.wc-block-components-product-rating__stars {
|
||||
margin: 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
export const blockAttributes = {
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,67 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import PropTypes from 'prop-types';
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import classnames from 'classnames';
|
||||
import Label from '@woocommerce/base-components/label';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product Sale Badge Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @param {string} [props.align] Alignment of the badge.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( { className, align } ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
|
||||
if ( ! product.id || ! product.on_sale ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const alignClass =
|
||||
typeof align === 'string'
|
||||
? `wc-block-components-product-sale-badge--align-${ align }`
|
||||
: '';
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
'wc-block-components-product-sale-badge',
|
||||
className,
|
||||
alignClass,
|
||||
{
|
||||
[ `${ parentClassName }__product-onsale` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
>
|
||||
<Label
|
||||
label={ __( 'Sale', 'woocommerce' ) }
|
||||
screenReaderLabel={ __(
|
||||
'Product on sale',
|
||||
'woocommerce'
|
||||
) }
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
align: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { tag, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'On-Sale Badge',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ tag } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Displays an on-sale badge if the product is on-sale.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes } ) => {
|
||||
return <Block { ...attributes } />;
|
||||
};
|
||||
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its sale-badge.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerBlockType } from '@wordpress/blocks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import attributes from './attributes';
|
||||
import edit from './edit';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
supports: {
|
||||
html: false,
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerBlockType( 'woocommerce/product-sale-badge', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,16 @@
|
||||
.wc-block-components-product-sale-badge {
|
||||
margin: 0 auto $gap-small;
|
||||
@include font-size(small);
|
||||
padding: em($gap-smallest) em($gap-small);
|
||||
display: inline-block;
|
||||
width: auto;
|
||||
border: 1px solid #43454b;
|
||||
border-radius: 3px;
|
||||
color: #43454b;
|
||||
background: #fff;
|
||||
text-align: center;
|
||||
text-transform: uppercase;
|
||||
font-weight: 600;
|
||||
z-index: 9;
|
||||
position: static;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import classnames from 'classnames';
|
||||
|
||||
const save = ( { attributes } ) => {
|
||||
return (
|
||||
<div className={ classnames( 'is-loading', attributes.className ) } />
|
||||
);
|
||||
};
|
||||
|
||||
export default save;
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { Icon, grid } from '@woocommerce/icons';
|
||||
import { isExperimentalBuild } from '@woocommerce/block-settings';
|
||||
import type { BlockConfiguration } from '@wordpress/blocks';
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import save from '../save';
|
||||
|
||||
/**
|
||||
* Holds default config for this collection of blocks.
|
||||
* attributes and title are omitted here as these are added on an individual block level.
|
||||
*/
|
||||
const sharedConfig: Omit< BlockConfiguration, 'attributes' | 'title' > = {
|
||||
category: 'woocommerce-product-elements',
|
||||
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ],
|
||||
icon: {
|
||||
src: <Icon srcElement={ grid } />,
|
||||
foreground: '#96588a',
|
||||
},
|
||||
supports: {
|
||||
html: false,
|
||||
},
|
||||
parent: isExperimentalBuild()
|
||||
? undefined
|
||||
: [ '@woocommerce/all-products', '@woocommerce/single-product' ],
|
||||
save,
|
||||
deprecated: [
|
||||
{
|
||||
attributes: {},
|
||||
save(): null {
|
||||
return null;
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export default sharedConfig;
|
@ -0,0 +1,11 @@
|
||||
.wc-atomic-blocks-product__selection {
|
||||
width: 100%;
|
||||
}
|
||||
.wc-atomic-blocks-product__edit-card {
|
||||
padding: 16px;
|
||||
border-top: 1px solid $gray-200;
|
||||
|
||||
.wc-atomic-blocks-product__edit-card-title {
|
||||
margin: 0 0 $gap;
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { useState } from '@wordpress/element';
|
||||
import ProductControl from '@woocommerce/editor-components/product-control';
|
||||
import { Placeholder, Button, ToolbarGroup } from '@wordpress/components';
|
||||
import { BlockControls } from '@wordpress/block-editor';
|
||||
import TextToolbarButton from '@woocommerce/editor-components/text-toolbar-button';
|
||||
import { useProductDataContext } from '@woocommerce/shared-context';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './editor.scss';
|
||||
|
||||
/**
|
||||
* This HOC shows a product selection interface if context is not present in the editor.
|
||||
*
|
||||
* @param {Object} selectorArgs Options for the selector.
|
||||
*
|
||||
*/
|
||||
const withProductSelector = ( selectorArgs ) => ( OriginalComponent ) => {
|
||||
return ( props ) => {
|
||||
const productDataContext = useProductDataContext();
|
||||
const { attributes, setAttributes } = props;
|
||||
const { productId } = attributes;
|
||||
const [ isEditing, setIsEditing ] = useState( ! productId );
|
||||
|
||||
if ( productDataContext.hasContext ) {
|
||||
return <OriginalComponent { ...props } />;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{ isEditing ? (
|
||||
<Placeholder
|
||||
icon={ selectorArgs.icon || '' }
|
||||
label={ selectorArgs.label || '' }
|
||||
className="wc-atomic-blocks-product"
|
||||
>
|
||||
{ !! selectorArgs.description && (
|
||||
<div>{ selectorArgs.description }</div>
|
||||
) }
|
||||
<div className="wc-atomic-blocks-product__selection">
|
||||
<ProductControl
|
||||
selected={ productId || 0 }
|
||||
showVariations
|
||||
onChange={ ( value = [] ) => {
|
||||
setAttributes( {
|
||||
productId: value[ 0 ]
|
||||
? value[ 0 ].id
|
||||
: 0,
|
||||
} );
|
||||
} }
|
||||
/>
|
||||
<Button
|
||||
isSecondary
|
||||
disabled={ ! productId }
|
||||
onClick={ () => {
|
||||
setIsEditing( false );
|
||||
} }
|
||||
>
|
||||
{ __( 'Done', 'woocommerce' ) }
|
||||
</Button>
|
||||
</div>
|
||||
</Placeholder>
|
||||
) : (
|
||||
<>
|
||||
<BlockControls>
|
||||
<ToolbarGroup>
|
||||
<TextToolbarButton
|
||||
onClick={ () => setIsEditing( true ) }
|
||||
>
|
||||
{ __(
|
||||
'Switch product…',
|
||||
'woocommerce'
|
||||
) }
|
||||
</TextToolbarButton>
|
||||
</ToolbarGroup>
|
||||
</BlockControls>
|
||||
<OriginalComponent { ...props } />
|
||||
</>
|
||||
) }
|
||||
</>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
export default withProductSelector;
|
@ -0,0 +1,8 @@
|
||||
export const blockAttributes = {
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import PropTypes from 'prop-types';
|
||||
import classnames from 'classnames';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product SKU Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( { className } ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
const sku = product.sku;
|
||||
|
||||
if ( ! sku ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-sku',
|
||||
{
|
||||
[ `${ parentClassName }__product-sku` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
>
|
||||
{ __( 'SKU:', 'woocommerce' ) }{ ' ' }
|
||||
<strong>{ sku }</strong>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { barcode, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __( 'Product SKU', 'woocommerce' );
|
||||
export const BLOCK_ICON = <Icon srcElement={ barcode } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display the SKU of a product.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import EditProductLink from '@woocommerce/editor-components/edit-product-link';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes } ) => {
|
||||
return (
|
||||
<>
|
||||
<EditProductLink />
|
||||
<Block { ...attributes } />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its SKU.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerExperimentalBlockType } from '@woocommerce/block-settings';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import attributes from './attributes';
|
||||
import edit from './edit';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerExperimentalBlockType( 'woocommerce/product-sku', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,7 @@
|
||||
.wc-block-components-product-sku {
|
||||
margin-top: 0;
|
||||
margin-bottom: $gap-small;
|
||||
display: block;
|
||||
text-transform: uppercase;
|
||||
@include font-size(small);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
export const blockAttributes = {
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,80 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __, sprintf } from '@wordpress/i18n';
|
||||
import PropTypes from 'prop-types';
|
||||
import classnames from 'classnames';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product Stock Indicator Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( { className } ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
|
||||
if ( ! product.id || ! product.is_purchasable ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const inStock = !! product.is_in_stock;
|
||||
const lowStock = product.low_stock_remaining;
|
||||
const isBackordered = product.is_on_backorder;
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-stock-indicator',
|
||||
{
|
||||
[ `${ parentClassName }__stock-indicator` ]: parentClassName,
|
||||
'wc-block-components-product-stock-indicator--in-stock': inStock,
|
||||
'wc-block-components-product-stock-indicator--out-of-stock': ! inStock,
|
||||
'wc-block-components-product-stock-indicator--low-stock': !! lowStock,
|
||||
'wc-block-components-product-stock-indicator--available-on-backorder': !! isBackordered,
|
||||
}
|
||||
) }
|
||||
>
|
||||
{ lowStock
|
||||
? lowStockText( lowStock )
|
||||
: stockText( inStock, isBackordered ) }
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const lowStockText = ( lowStock ) => {
|
||||
return sprintf(
|
||||
/* translators: %d stock amount (number of items in stock for product) */
|
||||
__( '%d left in stock', 'woocommerce' ),
|
||||
lowStock
|
||||
);
|
||||
};
|
||||
|
||||
const stockText = ( inStock, isBackordered ) => {
|
||||
if ( isBackordered ) {
|
||||
return __( 'Available on backorder', 'woocommerce' );
|
||||
}
|
||||
|
||||
return inStock
|
||||
? __( 'In Stock', 'woocommerce' )
|
||||
: __( 'Out of Stock', 'woocommerce' );
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { box, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'Product Stock Indicator',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ box } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display product stock status.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import EditProductLink from '@woocommerce/editor-components/edit-product-link';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes } ) => {
|
||||
return (
|
||||
<>
|
||||
<EditProductLink />
|
||||
<Block { ...attributes } />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its stock.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerExperimentalBlockType } from '@woocommerce/block-settings';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import attributes from './attributes';
|
||||
import edit from './edit';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerExperimentalBlockType( 'woocommerce/product-stock-indicator', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,17 @@
|
||||
.wc-block-components-product-stock-indicator {
|
||||
margin-top: 0;
|
||||
margin-bottom: em($gap-small);
|
||||
display: block;
|
||||
@include font-size(small);
|
||||
|
||||
&--in-stock {
|
||||
color: $in-stock-color;
|
||||
}
|
||||
&--out-of-stock {
|
||||
color: $no-stock-color;
|
||||
}
|
||||
&--low-stock,
|
||||
&--available-on-backorder {
|
||||
color: $low-stock-color;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
export const blockAttributes = {
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import PropTypes from 'prop-types';
|
||||
import classnames from 'classnames';
|
||||
import Summary from '@woocommerce/base-components/summary';
|
||||
import { blocksConfig } from '@woocommerce/block-settings';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product Summary Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( { className } ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
|
||||
if ( ! product ) {
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
`wc-block-components-product-summary`,
|
||||
{
|
||||
[ `${ parentClassName }__product-summary` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const source = product.short_description
|
||||
? product.short_description
|
||||
: product.description;
|
||||
|
||||
if ( ! source ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Summary
|
||||
className={ classnames(
|
||||
className,
|
||||
`wc-block-components-product-summary`,
|
||||
{
|
||||
[ `${ parentClassName }__product-summary` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
source={ source }
|
||||
maxLength={ 150 }
|
||||
countType={ blocksConfig.wordCountType || 'words' }
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { notes, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'Product Summary',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ notes } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display a short description about a product.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes } ) => {
|
||||
return <Block { ...attributes } />;
|
||||
};
|
||||
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its short description.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerBlockType } from '@wordpress/blocks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import attributes from './attributes';
|
||||
import edit from './edit';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerBlockType( 'woocommerce/product-summary', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,11 @@
|
||||
.wc-block-components-product-summary {
|
||||
margin-top: 0;
|
||||
margin-bottom: $gap-small;
|
||||
}
|
||||
.is-loading .wc-block-components-product-summary::before {
|
||||
@include placeholder();
|
||||
content: ".";
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 6em;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
export const blockAttributes = {
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
export default blockAttributes;
|
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import PropTypes from 'prop-types';
|
||||
import classnames from 'classnames';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { isEmpty } from 'lodash';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product Tag List Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
const Block = ( { className } ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
|
||||
if ( isEmpty( product.tags ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-tag-list',
|
||||
{
|
||||
[ `${ parentClassName }__product-tag-list` ]: parentClassName,
|
||||
}
|
||||
) }
|
||||
>
|
||||
{ __( 'Tags:', 'woocommerce' ) }{ ' ' }
|
||||
<ul>
|
||||
{ Object.values( product.tags ).map(
|
||||
( { name, link, slug } ) => {
|
||||
return (
|
||||
<li key={ `tag-list-item-${ slug }` }>
|
||||
<a href={ link }>{ name }</a>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
) }
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { tag, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'Product Tag List',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ tag } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display a list of tags belonging to a product.',
|
||||
'woocommerce'
|
||||
);
|
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { Disabled } from '@wordpress/components';
|
||||
import EditProductLink from '@woocommerce/editor-components/edit-product-link';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Block from './block';
|
||||
import withProductSelector from '../shared/with-product-selector';
|
||||
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
|
||||
|
||||
const Edit = ( { attributes } ) => {
|
||||
return (
|
||||
<>
|
||||
<EditProductLink />
|
||||
<Disabled>
|
||||
<Block { ...attributes } />
|
||||
</Disabled>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default withProductSelector( {
|
||||
icon: BLOCK_ICON,
|
||||
label: BLOCK_TITLE,
|
||||
description: __(
|
||||
'Choose a product to display its tags.',
|
||||
'woocommerce'
|
||||
),
|
||||
} )( Edit );
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { registerExperimentalBlockType } from '@woocommerce/block-settings';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import sharedConfig from '../shared/config';
|
||||
import attributes from './attributes';
|
||||
import edit from './edit';
|
||||
import {
|
||||
BLOCK_TITLE as title,
|
||||
BLOCK_ICON as icon,
|
||||
BLOCK_DESCRIPTION as description,
|
||||
} from './constants';
|
||||
|
||||
const blockConfig = {
|
||||
title,
|
||||
description,
|
||||
icon: {
|
||||
src: icon,
|
||||
foreground: '#874FB9',
|
||||
},
|
||||
attributes,
|
||||
edit,
|
||||
};
|
||||
|
||||
registerExperimentalBlockType( 'woocommerce/product-tag-list', {
|
||||
...sharedConfig,
|
||||
...blockConfig,
|
||||
} );
|
@ -0,0 +1,23 @@
|
||||
.wc-block-components-product-tag-list {
|
||||
margin-top: 0;
|
||||
margin-bottom: em($gap-small);
|
||||
|
||||
ul {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: inline;
|
||||
|
||||
li {
|
||||
display: inline;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
li::after {
|
||||
content: ", ";
|
||||
}
|
||||
|
||||
li:last-child::after {
|
||||
content: "";
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
|
||||
|
||||
let blockAttributes = {
|
||||
headingLevel: {
|
||||
type: 'number',
|
||||
default: 2,
|
||||
},
|
||||
showProductLink: {
|
||||
type: 'boolean',
|
||||
default: true,
|
||||
},
|
||||
productId: {
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
};
|
||||
|
||||
if ( isFeaturePluginBuild() ) {
|
||||
blockAttributes = {
|
||||
...blockAttributes,
|
||||
align: {
|
||||
type: 'string',
|
||||
},
|
||||
color: {
|
||||
type: 'string',
|
||||
},
|
||||
customColor: {
|
||||
type: 'string',
|
||||
},
|
||||
fontSize: {
|
||||
type: 'string',
|
||||
},
|
||||
customFontSize: {
|
||||
type: 'number',
|
||||
},
|
||||
};
|
||||
}
|
||||
export default blockAttributes;
|
@ -0,0 +1,130 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import PropTypes from 'prop-types';
|
||||
import classnames from 'classnames';
|
||||
import {
|
||||
useInnerBlockLayoutContext,
|
||||
useProductDataContext,
|
||||
} from '@woocommerce/shared-context';
|
||||
import { getColorClassName, getFontSizeClass } from '@wordpress/block-editor';
|
||||
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
|
||||
import { gatedStyledText } from '@woocommerce/atomic-utils';
|
||||
import { withProductDataContext } from '@woocommerce/shared-hocs';
|
||||
import ProductName from '@woocommerce/base-components/product-name';
|
||||
import { useStoreEvents } from '@woocommerce/base-context/hooks';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Product Title Block Component.
|
||||
*
|
||||
* @param {Object} props Incoming props.
|
||||
* @param {string} [props.className] CSS Class name for the component.
|
||||
* @param {number} [props.headingLevel] Heading level (h1, h2 etc)
|
||||
* @param {boolean} [props.showProductLink] Whether or not to display a link to the product page.
|
||||
* @param {string} [props.align] Title alignment.
|
||||
* @param {string} [props.color] Title color name.
|
||||
* @param {string} [props.customColor] Custom title color value.
|
||||
* @param {string} [props.fontSize] Title font size name.
|
||||
* @param {number } [props.customFontSize] Custom font size value.
|
||||
* will be used if this is not provided.
|
||||
* @return {*} The component.
|
||||
*/
|
||||
export const Block = ( {
|
||||
className,
|
||||
headingLevel = 2,
|
||||
showProductLink = true,
|
||||
align,
|
||||
color,
|
||||
customColor,
|
||||
fontSize,
|
||||
customFontSize,
|
||||
} ) => {
|
||||
const { parentClassName } = useInnerBlockLayoutContext();
|
||||
const { product } = useProductDataContext();
|
||||
const { dispatchStoreEvent } = useStoreEvents();
|
||||
const TagName = `h${ headingLevel }`;
|
||||
|
||||
const colorClass = getColorClassName( 'color', color );
|
||||
const fontSizeClass = getFontSizeClass( fontSize );
|
||||
|
||||
const titleClasses = classnames( {
|
||||
'has-text-color': color || customColor,
|
||||
'has-font-size': fontSize || customFontSize,
|
||||
[ colorClass ]: colorClass,
|
||||
[ fontSizeClass ]: fontSizeClass,
|
||||
} );
|
||||
|
||||
if ( ! product.id ) {
|
||||
return (
|
||||
<TagName
|
||||
// @ts-ignore
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-title',
|
||||
{
|
||||
[ `${ parentClassName }__product-title` ]: parentClassName,
|
||||
[ `wc-block-components-product-title--align-${ align }` ]:
|
||||
align && isFeaturePluginBuild(),
|
||||
[ titleClasses ]: isFeaturePluginBuild(),
|
||||
}
|
||||
) }
|
||||
style={ gatedStyledText( {
|
||||
color: customColor,
|
||||
fontSize: customFontSize,
|
||||
} ) }
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
// @ts-ignore
|
||||
<TagName
|
||||
className={ classnames(
|
||||
className,
|
||||
'wc-block-components-product-title',
|
||||
{
|
||||
[ `${ parentClassName }__product-title` ]: parentClassName,
|
||||
[ `wc-block-components-product-title--align-${ align }` ]:
|
||||
align && isFeaturePluginBuild(),
|
||||
}
|
||||
) }
|
||||
>
|
||||
<ProductName
|
||||
className={ classnames( {
|
||||
[ titleClasses ]: isFeaturePluginBuild(),
|
||||
} ) }
|
||||
disabled={ ! showProductLink }
|
||||
name={ product.name }
|
||||
permalink={ product.permalink }
|
||||
rel={ showProductLink ? 'nofollow' : null }
|
||||
style={ gatedStyledText( {
|
||||
color: customColor,
|
||||
fontSize: customFontSize,
|
||||
} ) }
|
||||
onClick={ () => {
|
||||
dispatchStoreEvent( 'product-view-link', {
|
||||
product,
|
||||
} );
|
||||
} }
|
||||
/>
|
||||
</TagName>
|
||||
);
|
||||
};
|
||||
|
||||
Block.propTypes = {
|
||||
className: PropTypes.string,
|
||||
headingLevel: PropTypes.number,
|
||||
showProductLink: PropTypes.bool,
|
||||
align: PropTypes.string,
|
||||
color: PropTypes.string,
|
||||
customColor: PropTypes.string,
|
||||
fontSize: PropTypes.string,
|
||||
customFontSize: PropTypes.number,
|
||||
};
|
||||
|
||||
export default withProductDataContext( Block );
|
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { bookmark, Icon } from '@woocommerce/icons';
|
||||
|
||||
export const BLOCK_TITLE = __(
|
||||
'Product Title',
|
||||
'woocommerce'
|
||||
);
|
||||
export const BLOCK_ICON = <Icon srcElement={ bookmark } />;
|
||||
export const BLOCK_DESCRIPTION = __(
|
||||
'Display the title of a product.',
|
||||
'woocommerce'
|
||||
);
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user