【问题标题】:Get Wordpress Post ID From Product On WooCommerce Checkout Page?从 WooCommerce 结帐页面上的产品获取 Wordpress 帖子 ID?
【发布时间】:2014-10-07 23:07:08
【问题描述】:

我有一些自定义字段,例如“清洁费”和“保证金”,我想在结帐页面上添加到项目中,我认为这很简单,但我看不出两者之间有任何关联项目和项目派生自的自定义帖子。我必须以某种方式从项目中获取帖子 ID 才能使用这样的行...

<?php $thisItemsSecurityDeposit = get_post_meta($cart_item['the post id'], 'accommodation_security_deposit', true ); echo $thisItemsSecurityDeposit ; ?>

...并在商品价格之后包含押金。

我在想也许有一些对象或会话变量数组包含将产品与产品来自的帖子 ID 相关联的键。

看... http://www.dreamhomevacationrentals.com/hotels/spanish-modern-retreat/ 然后,单击“可用性”,然后单击“立即预订”以查看购物车的运行情况。 非常感谢任何帮助。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    购物车对象 (WC()-&gt;cart-&gt;cart_contents) 的 cart_contents 变量包含购物车中每个商品的信息数组...我的示例购物车内容的 var_dump 如下所示:

    ["cart_contents"]=> array(1) {
        ["7cbbc409ec990f19c78c75bd1e06f215"]=>
            array(5) {
            ["product_id"]=> int(70)
            ["variation_id"]=> string(0) ""
            ["variation"]=> string(0) ""
            ["quantity"]=> int(1)
            ["data"]=> object(WC_Product_Simple)#530 (3) {
                ["id"]=> int(70)
                ["post"]=> object(WP_Post)#532 (24) {
                    ["ID"]=> int(70)
                    ["post_author"]=> string(1) "1"
                    ["post_date"]=> string(19) "2013-06-07 11:25:01"
                    ["post_date_gmt"]=> string(19) "2013-06-07 11:25:01"
                    ["post_content"]=> string(278) "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo."
                    ["post_title"]=> string(12) "Flying Ninja"
                    ["post_excerpt"]=> string(278) "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo."
                    ["post_status"]=> string(7) "publish"
                    ["comment_status"]=> string(4) "open"
                    ["ping_status"]=> string(6) "closed"
                    ["post_password"]=> string(0) ""
                    ["post_name"]=> string(12) "flying-ninja"
                    ["to_ping"]=> string(0) ""
                    ["pinged"]=> string(0) ""
                    ["post_modified"]=> string(19) "2014-10-06 16:53:48"
                    ["post_modified_gmt"]=> string(19) "2014-10-06 16:53:48"
                    ["post_content_filtered"]=> string(0) ""
                    ["post_parent"]=> int(0)
                    ["guid"]=> string(67) "http://demo2.woothemes.com/woocommerce/?post_type=product&amp;p=70"
                    ["menu_order"]=> int(0)
                    ["post_type"]=> string(7) "product"
                    ["post_mime_type"]=> string(0) ""
                    ["comment_count"]=> string(1) "4"
                    ["filter"]=> string(3) "raw"
                }
                ["product_type"]=>    string(6) "simple"
            }
        }
    }
    

    因此,在购物车内容的 foreach() 循环中,产品 ID 将被抓取,如下所示:

    $contents = WC()->cart->cart_contents;
    if( $contents ) foreach ( $contents as $cart_item ){
      echo $cart_item['product_id'];
    }
    

    如果您已经拥有特定的 $cart_item 变量(例如:您在购物车模板中并且已经在 Woo 的循环中),那么您只需访问 product_id 数组。 $cart_item['product_id']; 我总是发现使用 var_dump()print_r() 来了解我可以使用哪些变量或数组键很有帮助。

    顺便说一句,您可能想看看Product Add-ons,它完全符合您的描述。

    【讨论】:

    • 非常感谢您的输入 Helga 但我只需要 woo 商务产品源自的帖子的 post_id。我能够在帖子表上运行与帖子标题相同的产品标题的自定义 wp 查询,并且我还查询了帖子类型和帖子状态,以便我可以获得帖子 ID。请参阅下面的代码...
    • 全局 $wpdb; //使用产品标题,找到帖子ID。 $querystr = " 选择 $wpdb->posts.ID 从 $wpdb->posts WHERE $wpdb->posts.post_title = '$this_products_title' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts。 post_type = '住宿' AND $wpdb->posts.post_date get_row($querystr, ARRAY_A); //print_r($pageposts); //echo $pageposts[ID];
    • 代码太多,无法在 cmets 中阅读。您能否编辑您的问题并正确格式化代码以便我阅读?但是,除非您使用 Sequential Order Numbers 插件,否则产品 ID 是帖子 ID。因此,$cart_item['product_id']; 是购物车中特定商品的帖子 ID。
    • 以上内容不适用于 woo 商务。它完全根据上帝知道什么来分配自己的产品ID。我试图缩进代码,但由于它在评论框中,行为与这是主要帖子不同。
    • 无论如何。我已经超越了这个问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2017-03-09
    相关资源
    最近更新 更多