代码之家  ›  专栏  ›  技术社区  ›  Learning WooStoreFront

使用wp_footer钩子添加一些CSS不起作用

  •  1
  • Learning WooStoreFront  · 技术社区  · 2 年前

    为了使WooCommerce产品图像以正确的方式“显示”,我使用CSS wp_footer 钩这样,产品缩略图就可以在结账时正确显示 margin

    代码:

    add_action( 'wp_footer', 'ls_product_image_css_checkout', 900 );
    function ls_product_image_css_checkout() {
    
        // run only on checkout
        if ( is_checkout() ) { ?>
    
            <style>
            .product-item-thumbnail{ float:left; padding-right:20px; }
            .product-item-thumbnail img{ margin:0!important; }
            </style>
    
        <?php
        }
    }
    

    现在,这是我真正的问题,是的,我已经尝试了各种CSS解决方案,因为产品删除有 <a class="" 然而,无论我做什么—— X 用于从签出中删除产品的缩略图显示在缩略图的右侧,紧挨着 Product Name

    我需要 十、 用于放置在 LEFT thumbnail

    这是我正在使用的代码:

    add_filter( 'woocommerce_cart_item_name', 'ls_product_removal_on_the_left_of_product_image_on_checkout', 20, 3 );
    function ls_product_removal_on_the_left_of_product_image_on_checkout( $product_name, $cart_item, $cart_item_key ) {
    
        // only on checkout, of course
        if ( is_checkout() ) {
    
        // get cart
        $cart = WC()->cart->get_cart();
    
            // loop through cart
            foreach ( $cart as $cart_key => $cart_value ) {
    
                if ( $cart_key == $cart_item_key ) {
    
                    $product_id = $cart_item[ 'product_id' ];
                    $_product = $cart_item[ 'data' ];
    
                    // this is part of what I cannot figure out
                    $remove_product = sprintf(
                    '<a class="rpfc" href="%s" title="%s" data-product_id="%s" data-product_sku="%s" data-cart_item_key="%s">&#10005;</a>',
                    esc_url(wc_get_cart_remove_url( $cart_key)), __( 'Delete this product from my order', 'woocommerce' ),
                    esc_attr( $product_id ),
                    esc_attr( $_product->get_sku()),
                    esc_attr( $cart_item_key ));
                }
            }
    
            // get product data
            $product = $cart_item[ 'data' ];
    
            // display the iamge on checkout, 40*40 px
            $thumbnail = $product->get_image( array( 40, 40 ) );
    
            // the HTML
            $image_html = '<div class="product-item-thumbnail">' . $thumbnail . '</div>';
    
            // get the product name and link it. If clicked, re-directed to the product page
            $product_name_link = '<a href="' . $product->get_permalink() . '">' . $product_name . '</a>';
    
            // the removal of the product BEFORE the image
            // REMOVAL (on the left of the image) IMAGE PRODUCT NAME
            $product_name = $remove_product . $image_html . $product_name_link;
        }
    
        return $product_name;
    }
    
    1 回复  |  直到 2 年前
        1
  •  3
  •   LoicTheAztec    2 年前

    对于样式(CSS),永远不要使用页脚,因为CSS永远不会被应用,永远使用页眉。所以你需要更换 'wp_footer' 钩住 'wp_head' 相反它应该能解决问题。

    钩子 'wp_footer' 有时用于添加一些JavaScript(jQuery)代码。


    若要仅在签出页面中运行此代码,请替换:

    if ( is_checkout() ) {
    

    与:

    if ( is_checkout() && ! is_wc_endpoint_url() ) {
    

    这样,代码只在结账页面有效,但在“收到订单”中无效 (谢谢) 而不是在“订单支付”页面。


    如果某些CSS规则不起作用,您可能需要(或尝试):

    • 增加挂钩优先级,如:

      add_action( 'wp_head', 'ls_product_image_css_checkout', 999999 );
      
    • 添加 !important 对每个不起作用的规则,例如: float:none !important;