【问题标题】:woocommerce cart custom thumbnail image image in cartwoocommerce 购物车自定义缩略图 购物车中的图像
【发布时间】:2016-09-05 18:47:34
【问题描述】:

我想在购物车中显示自定义缩略图。我用自定义属性制作产品,比如imageurl。

我使用下面的钩子让它工作:

function custom_new_product_image($cart_object) {
    $a = '<img src="imageurlhere" />';
    return $a;
}

add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image' );

如果我用静态 url 代替 "imageurlhere",我的代码运行良好,但我想传递自定义产品属性图像 url。

我可以通过

获取图片网址
$cart_object->cart_contents['wccpf_imageurl']

如何使用自定义产品属性图片网址代替静态网址?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:
    function custom_new_product_image( $_product_img, $cart_item, $cart_item_key ) {
        $a      =   '<img src="'.$cart_item['wccpf_width'].'" />';
        return $a;
    }
    
    add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image', 10, 3 );
    

    【讨论】:

    • 好的,您已经添加了名为 imageurl 的产品属性,其中包含您期望的图像的 URL,对吧??
    • 是的,我想将其显示为购物车中的产品缩略图
    • 什么是 10,3 我刚刚粘贴它似乎仍然无法正常工作我得到空白 src
    • 当我使用 它显示产品 ID
    • 10 是过滤器的优先级,3 是要传递的参数数量!
    【解决方案2】:

    要更改 WooCommerce 购物车页面的缩略图大小,您需要执行以下步骤:

    Step-1:在function.php中创建你需要的大小。

    if ( function_exists( 'add_image_size' ) ) {
        add_image_size( 'custom-thumb', 100, 100 ); // 100 wide and 100 high
    }
    

    第 2 步:在 cart.php 中应该位于 your_tneme\woocommerce\cart 中查找

    $thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
    

    并在 get_image() 中写下您的尺寸名称。在我们的例子中,它将是这样的:get_image('custom-thumb')

    Step-3:查看图片来源,可以看到图片大小发生了变化。但是,您可能需要自定义 CSS 以显示您的预期样式。

    检查了 WooCommerce 最新版本 3.7.1 的工作

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 2020-01-21
      • 2017-08-05
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 2017-01-20
      相关资源
      最近更新 更多