Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce块-向产品网格添加简短的产品描述_Php_Wordpress_Woocommerce_Gutenberg Blocks - Fatal编程技术网

Php WooCommerce块-向产品网格添加简短的产品描述

Php WooCommerce块-向产品网格添加简短的产品描述,php,wordpress,woocommerce,gutenberg-blocks,Php,Wordpress,Woocommerce,Gutenberg Blocks,所以我第一次使用新的WooCommerce模块。在我的网站主页上,我加入了“产品畅销书”板块和“产品销售”板块。这两个块都显示了畅销产品或在售产品的网格样式布局 对于我需要的设计,我必须向html添加一些包装,因此我从这里克隆了存储库 添加的html确实有效,但现在我需要在这些块中包含产品简短描述。我对AbstractProductGrid.php进行了如下编辑: AbstractProductGrid.php /** *渲染单个产品。 *编辑日期:24/02/2020 * *添加了包装以显示带

所以我第一次使用新的WooCommerce模块。在我的网站主页上,我加入了“产品畅销书”板块和“产品销售”板块。这两个块都显示了畅销产品或在售产品的网格样式布局

对于我需要的设计,我必须向html添加一些包装,因此我从这里克隆了存储库

添加的html确实有效,但现在我需要在这些块中包含产品简短描述。我对AbstractProductGrid.php进行了如下编辑:

AbstractProductGrid.php
/**
*渲染单个产品。
*编辑日期:24/02/2020
*
*添加了包装以显示带有填充边框和其他样式的内容
*
*@param int$id产品id。
*@return字符串呈现产品输出。
*/
公共功能渲染器产品($id){
$product=wc\U get\U product($id);
如果(!$product){
返回“”;
}
$data=(对象)数组(
'permalink'=>esc_url($product->get_permalink()),
'description'=>$this->get\u description\u html($product),$this->get\u image\u html($product),
'title'=>this->get\u title\u html($product),
“评级”=>this->get_rating_html($product),
'price'=>$this->get\u price\u html($product),
“徽章”=>$this->get\u sale\u badge\u html($product),
“按钮”=>this->get_button_html($product),
);
返回应用过滤器(
“woocommerce\u blocks\u product\u grid\u item\u html”,
“
{$data->badge}
{$data->rating}
{$data->description}
{$data->price}
{$data->button}
“,
$data,
$product
);
}
/**
*获取产品的简短描述。
*
*@param\WC_Product$Product。
*@返回字符串
*/
受保护函数获取描述html($product){
if(空($this->attributes['contentVisibility']['description'])){
返回“

简短描述为空

”; } 返回“”。$product->get_short_description()?$product->get_short_description():wc_trim_string($product->get_description(),400)。“”; }

上面的代码返回一个空属性,如何包含新WooCommerce块的简短描述?

我在寻找答案时发现了这篇文章,我想你可能已经解决了这个问题,但这里有一个建议的解决方案供任何人使用

首先,不建议更改WooCommerce或Blocks插件的核心文件,因为如果您更新插件,您的更改将被覆盖。 更好的方法是利用插件为渲染输出公开一个名为“woocommerce\u blocks\u product\u grid\u item\u html”的过滤器

因此,如果将其放在functions.php中,您将在产品标题后获得简短的描述:

/**
 * Add short description to WooCommerce product blocks
 */
add_filter('woocommerce_blocks_product_grid_item_html', 'add_short_desc_woocommerce_blocks_product_grid_item_html', 10 , 3);
function add_short_desc_woocommerce_blocks_product_grid_item_html($content, $data, $product) {
    $short_description = '<div class="wc-block-grid__product-short-description">' . $product->get_short_description() . '</div>';

    // This will inject the short description after the first link (assumed: the default product link).
    $after_link_pos = strpos($content, "</a>");
    $before = substr($content, 0, $after_link_pos + 4);
    $after = substr($content, $after_link_pos + 4);

    $content = $before . $short_description . $after;

    return $content;
}
/**
*向WooCommerce产品块添加简短说明
*/
添加过滤器('woocommerce\u blocks\u product\u grid\u item\u html','add\u short\u desc\u woocommerce\u blocks\u product\u grid\u item\u html',10,3);
函数add_short_desc_woocommerce_blocks_product_grid_item_html($content,$data,$product){
$short_description='.$product->get_short_description();
//这将在第一个链接(假定:默认产品链接)之后插入简短描述。
$after\u link\u pos=strpos($content,“”);
$before=substr($content,0,$after\u link\u pos+4);
$after=substr($content,$after\u link\u pos+4);
$content=$before.$short\u description.$after;
返回$content;
}

这很有趣,我在我的网站上试用过,效果很好-不过我的第一个是在图片之后,这也是一个链接

对我来说,@Pierre的代码最终输出为:

Image
Description
Title
有人对如何使其输出有任何建议吗

Image
Title
Description
(即使图像链接到产品页面)


这将是一个很大的改进,您在哪里找到关于向woocommerce块添加过滤器的文档?看起来常规循环过滤器不起作用……在您提到的文件的第336行,我看到它们在从render_product()返回内容时使用了“apply_filters('woocommerce_blocks_product_grid_item_html',…”,$data,$products),所以我只是连接到了该过滤器。
Image
Title
Description