• Support will be limited until May 8th, as I will be out of the office travelling. Thank you for your patience and understanding.

Question Any way to check via template if a product is in the cart?

Status
Not open for further replies.

Nulumia

Customer
My idea was to place a conditional button under "Purchase", that will display "Go to Checkout" if the item is currently in the cart. I tried dumping the $orderItems var within the product wrapper template to see if I could pull the data, but it came up null in this location.

Wondering if this is possible :unsure:!

Thanks,
 
HTML:
<xf:set var="$orderRepo" value="{{ $xf.app.em.getRepository('DBTech\eCommerce:Order') }}" />

<xf:if is="$xf.visitor.user_id">
    <xf:set var="$order" value="{$orderRepo.findCurrentOrderForUser()}" />
<xf:else />
    <xf:set var="$order" value="{$orderRepo.findCurrentOrderForGuest()}" />
</xf:if>

{{ dump($order.Items) }}
 
HTML:
<xf:set var="$orderRepo" value="{{ $xf.app.em.getRepository('DBTech\eCommerce:Order') }}" />

<xf:if is="$xf.visitor.user_id">
    <xf:set var="$order" value="{$orderRepo.findCurrentOrderForUser()}" />
<xf:else />
    <xf:set var="$order" value="{$orderRepo.findCurrentOrderForGuest()}" />
</xf:if>

{{ dump($order.Items) }}
Thanks Fillip, I am getting [Error] Cannot use object of type DBTech\eCommerce\Finder\Order as array on {{ dump($order.Items) }} but {{ dump($order) }} works: however in the resulting table the order: [] field is empty. Trying to piece it together best I can,
 
Code:
<xf:set var="$orderRepo" value="{{ $xf.app.em.getRepository('DBTech\eCommerce:Order') }}" />

<xf:if is="$xf.visitor.user_id">
    <xf:set var="$orderFinder" value="{$orderRepo.findCurrentOrderForUser()}" />
<xf:else />
    <xf:set var="$orderFinder" value="{$orderRepo.findCurrentOrderForGuest()}" />
</xf:if>

<xf:set var="$order" value="{$orderFinder.fetch()}" />

{{ dump($order.Items) }}

Try that :)
 
That's really close - getting the array now, but $order.Items gives null while just $order gives the following:
Code:
ArrayCollection {#256 ▼
  #entities: array:1 [▼
    5 => Order {#241 ▼
      -_uniqueEntityId: 27
      #rootClass: "DBTech\eCommerce\Entity\Order"
      #_useReplaceInto: false
      #_newValues: []
      #_values: array:23 [▼
        "order_id" => 5
        "user_id" => 4
        "ip_address" => b"B=Iò"
        "order_date" => 1556143761
        "order_state" => "pending"
        "purchase_request_key" => null
        "address_id" => 0
        "shipping_address_id" => 0
        "sales_tax_id" => ""
        "store_credit_amount" => 0
        "coupon_id" => 0
        "sub_total" => "0.00"
        "sale_discounts" => "0.00"
        "coupon_discounts" => "0.00"
        "automatic_discounts" => "0.00"
        "shipping_cost" => "0.00"
        "sales_tax" => "0.00"
        "taxable_order_total" => "0.00"
        "currency" => "USD"
        "cost_amount" => "0.00"
        "has_invoice" => 1
        "sent_reminder" => 0
        "extra_data" => "[]"
      ]
      #_relations: array:2 [▼
        "Address" => null
        "Coupon" => null
      ]
      #_previousValues: []
      #_options: []
      #_deleted: false
      #_readOnly: false
      #_writePending: false
      #_writeRunning: false
      #_errors: []
      #_cascadeSave: []
      #_behaviors: null
    }
  ]
  #populated: true
}
Taking a stab myself over your code to find why ".Items" isn't present :unsure:
 
PHP:
<xf:set var="$orderRepo" value="{{ $xf.app.em.getRepository('DBTech\eCommerce:Order') }}" />

<xf:if is="$xf.visitor.user_id">
    <xf:set var="$orderFinder" value="{$orderRepo.findCurrentOrderForUser()}" />
<xf:else />
    <xf:set var="$orderFinder" value="{$orderRepo.findCurrentOrderForGuest()}" />
</xf:if>

<xf:set var="$order" value="{$orderFinder.fetchOne()}" />

{{ dump($order.Items) }}

Coding without testing is a magical thing indeed
 
Thanks Fillip you rock - the array is now populated and I have the product_id's to work with :LOL:

I should be able to form the checks now against the product being viewed. and display the button when necessary.

I'd be happy to share my results should you ever wish to use it.
 
Yay got it working, thanks for the help! Adding a product to cart ("Purchase") now refreshes with a Checkout button:

add-to-cart-checkout.jpg

Code:
<xf:set var="$orderRepo" value="{{ $xf.app.em.getRepository('DBTech\eCommerce:Order') }}" />
<xf:if is="$xf.visitor.user_id">
    <xf:set var="$orderFinder" value="{$orderRepo.findCurrentOrderForUser()}" />
    <xf:else />
    <xf:set var="$orderFinder" value="{$orderRepo.findCurrentOrderForGuest()}" />
</xf:if>
<xf:set var="$order" value="{$orderFinder.fetchOne()}" />
<xf:foreach loop="$order.Items" value="$orderItem">
    <xf:if is="$product.product_id == $orderItem.product_id">
        <div class="block-body block-row block-row--minor block-row--checkout">
            <xf:button href="{{ link('dbtech-ecommerce/checkout') }}" class="button--cta button--fullWidth" icon="cart" data-cache="false">{{ phrase('dbtech_ecommerce_checkout') }}</xf:button>
        </div>
    </xf:if>
</xf:foreach>

I didn't know if forming an array with the orderItem.product_id's, and than checking against that array would be preferable, so I just stuck with a foreach check.

I am running a custom setup but this should help prevent large padding between the buttons in all instances:

CSS:
.block-body.block-row.block-row--checkout {
    padding-top: 0;
}
 
icon="cart" didn't work for me, you might have some custom extensions to the icon feature. The <xf:button> tag supports this: fa="fa-shopping-cart" which does work :)

I've added an option to turn this off (default: on), and switched the logic of fetching the order into the PHP itself.

You can check this out @ this site just now :)
 
That's awesome, just tried it live and thank you for your consideration! Yes sorry I had forgot to mention I'm running custom button extensions :oops:. There's actually a few which I was surprised aren't included in XF by default..

I know this was a small feature request but perhaps from a user perspective it gives them a nice clear button to checkout compared to just the small icon in the nav.
 
Status
Not open for further replies.

DragonByte eCommerce

XenForo 2.0.6+ XenForo 2.1.x XenForo 2.2.x
Seller
DragonByte Technologies
Release date
Last update
Total downloads
2,454
Customer rating
4.83 star(s) 6 ratings
Back
Top