这是我第一次使用新的 WooCommerce Blocks。在我网站的主页上,我添加了“ProductBestSellers”块和“ProductOnSale”块。这两个块都显示了最畅销产品或特价产品的网格样式布局。
对于我需要的设计,我必须向 html 添加一些包装器,因此我从这里克隆了存储库;WooCommerce 古腾堡块
添加的 html 确实有效,但现在我需要在这些块中包含产品简短描述。我编辑了 AbstractProductGrid.php,如下所示;
/**
 * Render a single products.
 * Edited: 24/02/2020 
 *
 * Added wrappers to display content with padding borders and other styling
 *
 * @param int $id Product ID.
 * @return string Rendered product output.
 */
public function render_product( $id ) {
    $product = wc_get_product( $id );
    if ( ! $product ) {
        return '';
    }
    $data = (object) array(
        'permalink'     => esc_url( $product->get_permalink() ),
        'description'   => …Run Code Online (Sandbox Code Playgroud) 假设我们必须下载一个文件,检查值是否为 True,如果是,我们需要通过电子邮件提醒管理员。
我想将所有不同的事情分成易于管理的步骤(功能)。然后一一调用各个函数。
这将创建以下函数:
def alert_admin():
    if download_file() == True: # File is downloaded, check if file is present
        if extract_file() == True: # Zip file contains the correct file
            if read_file() == True: # Read the file and the value is there
                if value_in_file() == True:
                    # send the admin an email
                    pass
                else:
                    pass
            else:
                pass
        else:
            pass
    else:
        pass
Run Code Online (Sandbox Code Playgroud)
这感觉不对,有没有更好的方法(更Pythonic)来检查某些函数的结果并继续下一个?